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.


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; only true() and 1 evaluate to a true boolean; everything else evaluates to a false one (this holds true provided you have specified the variable type as xs:boolean)
  • xs:integer for an integer
  • xs:float for decimals
  • xs: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 of 5 to the variable
  • select="'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 of 11 to the variable
  • select="/Books/@language" to extract the value of the language attribute of the root tag (Books) from the asssociated XML data and assign it to the variable
  • select="$otherVariable" to assign the value of a previously declared variable $otherVariable to this variable


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&apos;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>

results matching ""

    No results matching ""