SAXONICA

Saxon Documentation
Full Contents
About Saxon
Changes in this Release
Conditions of Use
Using XSLT 2.0
Using XQuery
Handling Source Documents
XML Schema Processing
The XPath API
Standards Conformance
Extensibility
Saxon Extensions
Sample Saxon Applications
The Saxon SQL Extension
XSLT Elements
| Introduction
| xsl:analyze-string
| xsl:apply-imports
| xsl:apply-templates
| xsl:attribute
| xsl:attribute-set
| xsl:call-template
| xsl:character-map
| xsl:choose
| xsl:comment
| xsl:copy
| xsl:copy-of
| xsl:decimal-format
| xsl:document
| xsl:element
| xsl:fallback
| xsl:for-each
| xsl:for-each-group
| xsl:function
| xsl:if
| xsl:include
| xsl:import
| xsl:import-schema
| xsl:key
| xsl:matching-substring
| xsl:message
| xsl:namespace
| xsl:namespace-alias
| xsl:next-match
| xsl:non-matching-substring
| xsl:number
| xsl:otherwise
| xsl:output
| xsl:output-character
| xsl:param
| xsl:perform-sort
| xsl:preserve-space
| xsl:processing-instruction
| xsl:result-document
| xsl:sequence
| xsl:sort
| xsl:strip-space
| xsl:stylesheet
| xsl:template
| xsl:text
| xsl:value-of
» | xsl:variable
| xsl:when
| xsl:with-param
| Literal Result Elements
XPath 2.0 Expression Syntax
Function Library
XSLT Patterns

# xsl:variable

The `xsl:variable` element is used to declare a variable and give it a value. If it appears at the top level (immediately within xsl:stylesheet) it declares a global variable, otherwise it declares a local variable that is visible only within the stylesheet element containing the xsl:variable declaration.

The mandatory `name` attribute defines the name of the variable.

The value of the variable may be defined either by an expression within the optional `select` attribute, or by the contents of the xsl:variable element. In the latter case the result is a temporary tree. A temporary tree can be used like a source document, for example it can be accessed using path expressions and processed using template rules.

There is an optional attribute, `as`, to define the type of the variable. The actual supplied value must be an instance of this type; it will not be converted. This has changed in Saxon 7.4: previous releases did a conversion.

In standard XSLT, variables once declared cannot be updated. Saxon however provides a saxon:assign extension element to circumvent this restriction.

The value of a variable can be referenced within an expression using the syntax `$name`.

Example:

    <xsl:variable name="title">A really exciting document"</xsl:variable>
    <xsl:variable name="backcolor" expr="'#FFFFCC'" />
    <xsl:template match="/*">
        <HTML><TITLE<xsl:value-of select="$title"/></TITLE>
        <BODY BGCOLOR='{$backcolor}'>
        ...

        </BODY></HTML>
    </xsl:template>

Next