XML technologies / XPath / XPath 2.0: language reference / XPath expressions: operators and keywords / Loop expressions with for
(Excerpt from "XSLT 2.0 & XPath 2.0" by Frank Bongers, chapter 4.)
Keywords: for, in, return
One of the most useful new constructs in XPath 2.0 is the for (loop), which processes a sequence item by item, binding each successive item to a temporary variable:
for $x in $eingabesequenz
return $ergebnisausdruck
A temporary variable $x is also called a range variable.
For every item $x in the supplied sequence $eingabesequenz, the return expression $ergebnisausdruck is evaluated, usually using the current value of $x. All generated values are concatenated in sequence order and returned together.
The XSLT instruction xsl:for-each can do something similar, but an XPath for loop returns a sequence directly, usable in further processing or other XPath expressions. An xsl:for-each instruction can at most write its result to a variable or directly to a result document.
Looping over a node sequence
For example, a calculation can process every member of a node sequence, obtaining values from child elements of each item:
for $x in /bestellung/ware
return
$x/preis * $x/menge
This example multiplies each ordered product’s price and quantity from an XML order, returning a sequence of atomic values.
The order total is easily obtained by passing that expression to fn:sum():
fn:sum(for $x in /bestellung/ware
return
$x/preis * $x/menge)
XPath 1.0 and XSLT 1.0 can express a similar solution, but much less conveniently. The result of an xsl:for-each instruction can be stored in a variable, but as a result tree fragment rather than a node set accessible to XPath 1.0. Further processing requires an extension that converts it to a node set, such as EXSLT’s exsl:node-set().
For loop with multiple variable bindings
A for expression may bind two or more variables, separated by commas:
for $x in $sequenz_1,
$y in $sequenz_2
return $x + $y
This only appears to be a loop with multiple variables. Internally, n bound variables become n nested for loops. The simple example therefore becomes:
for $x in $sequenz_1
return for $y in $sequenz_2
return $x + $y
The following expression computes a number sequence from two input sequences:
for $i in (10, 20, 30), $j in (1, 2, 3)
return ($i + $j)
The result sequence is: 11,12, 13, 21, 22, 23, 31, 32, 33.
A bound variable can be evaluated inside a nested loop:
for $a in distinct-values(//autor)
return ($a,
for $b in //buch[autor = $a]
return $b/titel)
This expression takes distinct author elements from an XML book list in document order. The sequence becomes a predicate for the inner loop’s input, which finds each author’s book titles in document order.
A look at XQuery’s more powerful for expression
The for expression in XPath is only a subset of the XQuery for expression. In XQuery, a for loop can sort (order by ), declare variables (let) and impose additional conditions (where). These features are unavailable in the XPath subset usable within XSLT.
Looking back at XPath 1.0
XPath 1.0 expressions have no concept equivalent to the for loop. Using xsl:for-each and variables is possible, but less convenient and flexible.
Copyright © Galileo Press, Bonn 2008
You may print the online version for private use.
Otherwise, this chapter from "XSLT 2.0 & XPath 2.0 ― Das umfassende Handbuch" is subject to the same terms as the bound edition: the work and all its parts are protected by copyright. All rights reserved, including reproduction, translation, microfilming, storage and processing in electronic systems.
Galileo Press, Rheinwerkallee 4, 53227 Bonn