Variables
In XSL
, you declare a variable as follows:
<xsl:variable as="xs:integer" name="age" select="5" />
This way, you have created a variable of type integer
with value 5
that can be accessed as $age
anywhere in the code.
Type
It is highly advisable that you specify the type via the as
attribute. Otherwise, it can be quite difficult to establish with certainty what type XSL
automatically picks for your variable. The most common types are:
xs:boolean
for booleans; onlytrue()
and1
evaluate to atrue
boolean; everything else evaluates to afalse
one (this holds true provided you have specified the variable type asxs:boolean
)xs:integer
for an integerxs:float
for decimalsxs:string
for string values
For a more complete reference and thorough discussion regarding types, I recommend the XSD Tutorial on Tutorials Point.
Important: Even though BI Publisher claims to be compliant with the XSLT 2.0 draft, it does not seem to me to be the case, mostly. Type setting is an example of this issue. Even though it might not have any effect on the actual execution, I still think it is a good idea to declare a type of a variable. Stating the intended type makes the code a bit easier to read.
Name & Value
The name
attribute specifies the variable name and it is, quite naturally, compulsory.
The select
attribute defines what value should be assigned to the declared variable. It expects an expression
which could be one of the following:
select="5"
to assign a value of5
to the variableselect="'Variable value'"
to assign a string Variable value to the variable (note the single quotes the value is enclosed in, they are compulsory)select="5 + 6"
to assign a value of11
to the variableselect="/Books/@language"
to extract the value of thelanguage
attribute of the root tag (Books
) from the asssociatedXML
data and assign it to the variableselect="$otherVariable"
to assign the value of a previously declared variable$otherVariable
to this variable
Context
When referencing variables, it is important to pay attention to the context. In some contexts, variables are expected, in others, you need to notify the processor of your intentions.
The select
attribute is the most common place where a variable is expected. Therefore, it is sufficient to just prepend a dollar sign $
to its name and voilà, its value is queried. On the other hand, if you'd like to pass in a direct value rather than a variable, you should enclose it in single quotes '
. Note that numbers do not require this treatment. Indeed, enclosing a number in single quotes causes it to be parsed as a string value.
<!-- variable declaration -->
<xsl:variable as="xs:string" name="personName" select="'John Doe'" />
<!-- pass in a value directly -->
<xsl:value-of select="'Person's name:'">
<!-- query a variable -->
<xsl:value-of select="$personName" />
In many other contexts, however, a variable is not exptected. Such cases require you to enclose the value of the attribute in curly braces {}
to indicate you are passing in a variable.
<!-- variable declaration -->
<xsl:variable as="xs:integer" name="marginTop" select="10" />
<!-- pass in a value directly -->
<fo:block margin-top="10">Coded in margin.</fo:block>
<!-- query a variable -->
<fo:block margin-top="{$marginTop}">Margin from a variable.</fo:block>