XML technologies / XSLT / Introduction to XSLT 2.0 / XSLT and XPath – round three / Variables and parameters / Global variables and parameters
(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 3.)
Both xsl:variable and xsl:param may occur at the top level. Put their declarations near the start so they apply to subsequent elements and their children. Since all xsl:template elements are following-siblings of the declaration, the variable applies in every subsequent template. Such variables are global because they can be read throughout the stylesheet.
Top-level parameters differ slightly
Global parameters and variables have the same scope. However, a nondefault global parameter value cannot come from xsl:with-param; it must come from outside the stylesheet.
XSLT does not specify the value-passing mechanism. The XSLT 2.0 processors AltovaXML and Saxon also accept command-line parameters.
Saxon 9.0:
java net.sf.saxon.Transform -s:quelldokument -xsl:stylesheetname -o:zieldokument paramname=paramwertAltovaXML:
AltovaXML -xslt2 stylesheetname -in quelldokument -out zieldokument -param paramname=paramwertIn Altova, quote parameter strings containing spaces, whether in filenames or expressions as here.
Cocoon obtains parameter values from the URL query string, for example:
nehme_parameter.xml?parametername=wert
Although the request refers to an XML document, the parameter goes to the processing XSLT stylesheet, not that document:
<!-- Deklarationsebene -->
<xsl:param name="ausrichtung">left</xsl:param>
<!-- Eines von vielen Templates -->
<xsl:template match="para">
<p align="{$ausrichtung}"><xsl:apply-templates/></p>
</xsl:template>
A global parameter $ausrichtung is declared and used in an attribute value template on a literal result element in a template rule. Its default is "left".
The parameter also applies in every other template. An externally supplied value could therefore change alignment for all template rules referencing it.
Local variables and parameters in templates
Elements xsl:variable and xsl:param are also allowed in template blocks, but with much narrower scope than globals. Their scope is limited to their template block and its siblings following the declaration.
Declare them at the start of the block where possible. For local xsl:param elements, this is required.
Reading a local variable outside its scope is an error. A variable declared for one template is unavailable in the next:
<xsl:template name="das_eine_template">
<xsl:variable name="die_variable" select="der_wert"/>
<!-- bezieht sich auf lokale Variable: -->
Lokal: <xsl:value-of select="$die_variable"/>
</xsl:template>
<xsl:template name="anderes_template">
<!-- Folgendes kennt man hier nicht mehr: -->
Oops: <xsl:value-of select="$die_variable"/>
</xsl:template>
The figure shows global and local scope. Global variable $var_global and parameter $para_global are available in every rule, shown in light grey. Local values such as $var_a and $para_b apply only in their current rule, after declaration, shown in dark grey. They are unavailable in adjacent rules, but, as for $var_a, can be passed as parameters when another rule is called.
Figure: Scope of parameters and variables.
Local shadowing of variables
A variable is a value bound to a name. In ordinary programming, binding a new value to an existing name is reassignment. XSLT fundamentally forbids this and has no assignment operator.
Apparent reassignment is possible when an existing variable or parameter is global and a new same-named one is local.
The local variable then hides the global within its block, known as binding shadow. This is temporary: after the template finishes, the global value applies again.
Other attempts to give two variables or parameters the same name cause errors; the two kinds are not distinguished because their references are identical. Neither globally nor locally may two variables share a name. This example therefore fails:
<xsl:template name="so-gehts-nicht">
<xsl:param name="dublette" select="der_wert"/>
...
<!-- und jetzt gibt’s einen Fehler: -->
<xsl:variable name="dublette" select="anderer_wert"/>
...
<!-- worauf bezieht sich dies? -->
Lokal: <xsl:value-of select="$dublette"/>
...
</xsl:template>
Shadowing globals is allowed:
<!-- globale Variable: -->
<xsl:param name="dublette" select="der_wert"/>
<xsl:template name="so-gehts">
<!-- verdeckt die globale Variable: -->
<xsl:variable name="dublette" select="anderer_wert"/>
...
<!-- bezieht sich auf lokale Variable: -->
Lokal: <xsl:value-of select="$dublette"/>
</xsl:template>
<xsl:template name="anderes_template">
<!-- referenziert die globale Variable: -->
Global: <xsl:value-of select="$dublette"/>
</xsl:template>
Where can variables be used?
After declaration, variables and parameters can be referenced in two places:
In an XSLT element’s attribute
Attributes accepting XPath expressions also evaluate variables and parameters. Write the reference directly into the attribute:
<xsl:for-each select="$variable">
...
</xsl:for-each>
In an attribute value template
Elsewhere, put references inside the braces of an attribute value template (Note: the list of XSLT attributes accepting AVTs is in the XSLT 2.0 reference):
<xsl:element name="{$variable}">
...
</xsl:element>
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