XML technologies / XSLT / Introduction to XSLT 2.0 / XSLT and XPath – round three / Variables and parameters / Variable values are temporary trees
(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 3.)
XSLT 1.0 has the concept of a temporary result tree fragment, better known as Result Tree Fragment (RTF). An RTF arises when a temporary tree is created and stored in an XSLT variable xsl:variable.
However, XPath 1.0 cannot address the structure inside an RTF after creation. An RTF is not a proper node set. XSLT 1.0 has no built-in conversion to a “real” node set, so extension functions such as saxon:node-set(), exsl:node-set and others added this capability.
Rather than building such a function into XSLT 2.0, the RTF concept was abolished. Every temporary tree created for a variable is now a true node sequence. As a full tree, it has its own root node and can be navigated with XPath like an independent document.
A variable or parameter can acquire a value in two ways.
Through the select attribute
If a variable or parameter declaration has attribute select, its value must be an XPath expression. Its evaluation result becomes the variable value: a string, number, Boolean or sequence. With attribute select present, the element must be empty.
For example, this variable contains the number 2:
<xsl:variable name="n" select="2"/>
Through element content
If a variable or parameter declaration has no attribute select, its content defines the value. This is always a sequence resulting from the element’s sequence constructor.
In this case the variable or parameter never contains a simple atomic value such as a number or string, though the difference often causes no problem. Instead, it contains a document node, accessible by the variable name, whose children result from the declaration’s sequence constructor. The value is effectively an independent document tree that can be searched and evaluated with XPath expressions.
For example, this variable contains a sequence with a text node whose string value is “2”, which may differ from the above:
<xsl:variable name="n">2</xsl:variable>
Alternative value assignments
The missing assignment operator was discussed with xsl:variable. For example, in JavaScript, assigning a declared variable a value based on a condition is simply:
var die_variable;
if(bedingung)
die_variable = der_wert;
else
die_variable = anderer_wert;
Here the condition is evaluated before assignment. XSLT requires the reverse: declare first, then use the condition to determine the value:
<xsl:variable name="die_variable">
<xsl:choose>
<xsl:when test="$bedingung">der_wert</xsl:when>
<xsl:otherwise>anderer_wert</xsl:otherwise>
</xsl:choose>
</xsl:variable>
XPath 2.0 makes this simpler:
<!-- Lösung mit XPath 2.0 -->
<xsl:variable name="die_variable" select="if($bedingung) then 'der_wert' else 'anderer_wert'"/>
Note:
For an introduction to XPath conditions, see Alternative in XPath: if - then - else.
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