variable/constant
The variable statement can occur either at the root level with the stylesheet or within any valid block.
Variables can be declared with a single value or a block.
Note that the term variable in the XSLT is a misnomer for those who are used to more conventional computer
languages. Once a variable has been set within a particular scope it cannot be reset.
Because of the XSLT concept of variables being set only once, Rexsel offers an alternative keyword, constant,
to better reflect the actual operational type, see the example below.
Syntax
<variable> ::= ( “variable” | “constant” ) <name>
(
( <quote> <xpath expression> <quote> ) |
( “{”
( <block template> )+
“}”
)
)
Options
None.
Elements
Only one of the two following options
disable-output-escaping
|
An XPath expression that returns a value to assign to the variable. |
statement block
|
A block of statements that returns a value to assign to the variable. |
Examples
Consider a set of variables at a global level.
stylesheet {
version "1.0"
variable var-1 "'Some text'"
variable var-2 {
text "Some text"
}
variable var-3 {
value "$var-1"
}
}
The variables var-1, var-2 and var-3
would all be set the same value "Some text". The same declarations using the
alternative approach using constant.
stylesheet {
version "1.0"
constant var-1 "'Some text'"
constant var-2 {
text "Some text"
}
constant var-3 {
value "$var-1"
}
}
giving identical results. It is left to users to decide if the approach is useful. These compile to
<?xml version="1.0" encoding="UTF-8"?>
<stylesheet
xmlns="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<variable name="var-1" select="'Some text'"/>
<variable name="var-2">
<text>Some text</text>
</variable>
<variable name="var-3">
<value-of select="$var-1"/>
</variable>
</stylesheet>
Errors
The following gives a variaty of errors that may occur.
stylesheet {
version "1.0"
// -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
// Invalid statements
// Missing name
constant
// Simple parameter declaration, no value
variable valid-var-1
// Misssing name
variable "'Some text'"
// Blank block which is quasi valid (translates to valid XSLT)
variable invalid-var-1 {
}
// Cannnot have value and block
variable invalid-var-2 "'Some text'" { }
// Duplicate name
variable valid-var-1 "'Some duplicate text'"
// Duplicate name + undefined symbol
variable valid-var-2 {
value "$valid-var-4"
}
}
will give the following list of errors
**** (115) Expected name after "variable" in line 8
Insert name.
**** (115) Expected name after "variable" in line 14
Insert name.
**** (129) There must be either a simple/default value or enclosed templates in line 17
Supply either expression/text or enclosed templates.
**** (151) "variable" requires one or more "message, number, copy-of, copy, element, if,
variable, processing-instruction, text, attribute-set, foreach, fallback,
apply-imports, value, attribute, call, choose, comment, apply-templates" in line 17
Check syntax requirements for "variable" or insert in block
**** (105) Unexpected symbol "{" found in "variable" in line 21
Check spelling, missing expression, bracket or quote?
**** (138) Missing or empty block in 21
Supply block
**** (136) Unmatched brackets in 1
Too many open brackets?
Copyright 2024 Hugh Field-Richards. All Rights Reserved.