XML technologies / XSLT / Introduction to XSLT 2.0 / XSLT and XPath – round three / Variables and parameters / xsl:variable and xsl:param
(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 3.)
What flexibility remains if variables cannot even be reassigned? Things are not that dire: similarities to variables in other languages remain.
In XSLT too, a variable is a value associated with a freely chosen name, such as der_name, used to retrieve and include it. Its value comes from an XPath expression or a template (block ) inside the declaration:
<xsl:variable name="der_name">
<!-- hier den Wert ermitteln -->
</xsl:variable>
Prefix a variable name with a dollar sign to reference it: $der_name.
Variables and parameters – fraternal twins
Two XSLT elements have slightly different variable-like behavior: xsl:variable and xsl:param.
Both represent a named value. They differ in how that value is obtained. For a variable xsl:variable, it comes from inside, determined at declaration time through an XPath expression in attribute select or an internal sequence constructor. Once fixed, it cannot change for the variable’s lifetime.
A parameter declaration can likewise assign an immediate value using select or a sequence constructor. Unlike a variable, this is only a default for xsl:param and can be overridden externally, but only at declaration time, never later.
Local parameter values are supplied using instruction xsl:with-param. A supplied value wins; otherwise the default applies. Explicit empty strings and sequences also count as supplied values when addressed to the parameter.
Two forms – global and local
Unlike other XSLT elements, xsl:variable and xsl:param can appear as instructions inside template blocks or at top level on the declaration level. Top-level values are available throughout the stylesheet, giving global scope. These are global parameters and variables, as opposed to local variables and parameters declared inside template rules or named templates and valid only there.
XSLT variables have a strictly limited scope (scope). They are readable inside it and invisible outside.
A rule of thumb: a variable is valid from its declaration for all subsequent sibling elements (following siblings) and their descendants (descendants). Typical declarations look like this:
<!-- eine Variable -->
<xsl:variable name="zahl" select="17"/>
<!-- ein Parameter -->
<xsl:param name="zahl" select="4"/>
The variable is then referenced using $zahl. Unfortunately, the same reference $zahl would also address the same-named parameter. Therefore:
Same-named variables and parameters are indistinguishable when referenced and should be avoided!
Copyright © Galileo Press, Bonn 2008
You may print the online version for your private use.
Otherwise, this chapter from “XSLT 2.0 & XPath 2.0 ― Das umfassende Handbuch” is subject to the same conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
Galileo Press, Rheinwerkallee 4, 53227 Bonn