Declares a variable and binds a value to that variable.


<xsl:variable name="variable_name" 
  [select = "expression2"] 
  [expr = "expression3"]>



The name attribute is required, and it must be a string. The value of the name attribute is a qualified name.

The value that you bind to a variable can be an object of any of the types that are returned by expressions. You can specify the value of the variable in several ways:

  • Specify the select attribute. The value of the select attribute must be an expression. The XSLT processor evaluates the expression, and the result is the value of the variable. If you specify the select attribute, you must not specify any contents for the xsl:variable instruction. In other words, do not specify template_body.
  • Specify the expr attribute. It is interpreted as an attribute value template. It allows computation of the value expression.

The expr attribute of the xsl:variable instruction is an extension of the XSLT standard. If you want to use an XSLT processor other than the Stylus Studio processor, you cannot specify the expr attribute in your stylesheet.

  • Specify template_body. The XSLT processor instantiates this template to obtain the value of the variable. If you specify template_body, you must not specify the select attribute.
  • Specify none of the above. In this case, the value of the variable is an empty string.

The difference between the xsl:param and xsl:variable instructions is that xsl:param defines a default value while xsl:variable defines a fixed value.


For any use of the xsl:variable element, there is a region of the stylesheet tree within which the binding is visible. This region includes the siblings that follow the xsl:variable instruction together with their descendants. Within this region, any binding of the variable that is visible on the xsl:variable element itself is hidden. Thus, only the innermost binding of a variable is visible. The set of variable bindings in scope for an expression consists of those bindings that are visible at the point in the stylesheet where the expression occurs.

The xsl:variable instruction can be a top-level element. If it is, it declares a global variable that is visible to the entire stylesheet. When the XSLT processor evaluates the select or expr attribute in a top-level xsl:variable instruction, the current node is the root node of the document. The xsl:variable instruction is also allowed anywhere in a template that an XSLT instruction is allowed.

Free Stylus Studio XML Training:
W3C Member