XML technologies / XSLT / XSLT Cookbook / XPath / Eliminating recursion with for expressions

(Excerpt from "XSLT Kochbuch" by Sal Mangano)

Problem

You want to derive an output sequence from an input sequence, with each output item an arbitrarily complex function of the input and the two sequence lengths not necessarily equal.

Solution

XPath 1.0

Not possible in 1.0. Use a recursive XSLT template.

XPath 2.0

Use the for expression in XPath 2.0. The following cases show how a for expression can map sequences of different input and output lengths.

Aggregation

(: Summe der Quadrate. :)

sum(for $x in $numbers return $x * $x)

(: Durchschnittswert der Quadrate. :)

avg(for $x in $numbers return $x * $x)

Mapping

(: Bildet eine Sequenz von Wörtern in allen Absätzen auf eine Sequenz von Wortlängen ab. :)

for $x in //para/tokenize(., ' ') return string-length($x)

(: Bildet eine Sequenz von Wörtern in einem Absatz auf eine Sequenz von Wortlängen ab, und zwar für Wörter, die mehr als drei Buchstaben umfassen. :)

for $x in //para/tokenize(., ' ') return if (string-length($x) gt 3) then string-length($x) else ( )

(: Identisch mit dem oben Genannten, allerdings mit einer Bedingung in der Eingangssequenz. :)

for $x in //para/tokenize(., ' ')[string-length(.) gt 3] return string-length($x)

Generation

(: Generiert eine Sequenz von Quadraten der ersten 100 ganzen Zahlen. :)

for $i in 1 to 100 return $i * $i

(: Generiert eine Sequenz von Quadraten in umgekehrter Reihenfolge. :)

for $i in 0 to 10 return (10 - $i) * (10 - $i)

Expansion

(: Bildet eine Sequenz von Absätzen auf eine duplizierte Sequenz von Absätzen ab. :)

for $x in //para return ($x, $x)

(: Dupliziert Wörter. :)

for $x in //para/tokenize(., ' ') return ($x, $x)

(: Bildet Wörter auf Wort, gefolgt von Wortlänge, ab. :)

for $x in //para/tokenize(., ' ') return ($x, string-length($x))

Merging

(: Für jeden Kunden wird eine ID ausgegeben sowie die Gesamtheit der Bestellungen des Kunden. :)

for $cust in doc('customer.xml')/*/customer return($cust/id/text(), sum(for $ord in doc('orders.xml')/*/order[custID eq $cust/id] return ($ord/total)))

Discussion

As mentioned in the recipe Simplifying conditional code with if expressions, adding control-flow constructs to an expression language such as XPath may initially seem strange or misleading. Exploring their liberating power soon dispels these doubts, especially for the XPath 2.0 expression for.

The power of the for expression becomes clear when it reduces complex recursive XSLT 1.0 solutions to a single XPath 2.0 expression. A simple sum in XSLT 1.0 is easy with its built-in sum function. A sum of squares, however, requires a longer, harder and less transparent recursive template. Much of this book’s first edition provided ready-made solutions to such recursive contortions. In XPath 2.0, a sum of squares is simply sum(for $x in $numbers return $x * $x), where $numbers contains the sequence of numbers to sum. Think of the trees I could have saved had XPath 1.0 already offered this solution!

The for expression offers more: multiple iteration variables can be combined into nested loops that produce sequences of related nodes in a complex document.

(:Rückgabe einer Sequenz, die aus para-IDs und den IDs besteht, die diese para-Elemente referenzieren. :)

for $s in /*/section, $p in $s/para, $r in $p/ref return ($p/@id, $r)

The preceding expression is semantically equivalent to the following one, merely more compact:

for $s in /*/section return for $p in $s/para return for $r in $p/ref return ($p/@id, $r)

A nested for is unnecessary when a traditional path expression represents the desired sequence more elegantly.

(: Diese Anwendung von for ist einfach ein umständlicherer Ausdruck für /*/section/para/ref. :)

for $s in /*/section, $p in $s/para, $r in $p/ref return $r

Sometimes an item’s position in a sequence is needed during processing. You cannot use position() as in an xsl:for-each because an XPath for expression does not change context position. The following expression achieves the desired effect:

for $pos in 1 to count($sequenz), $objekt in $sequenz[$pos] return $objekt, $pos

  

 

 

 

back to top