XML technologies / XSLT / Reference: XSLT 1.0 and 2.0 elements / for-each
(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 6.)
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z
Instruction xsl:for-each forms a loop over every item of a sequence selected by its select attribute, applying its instruction block to each item.
Classification | Instruction |
Function group | Looping over an input sequence |
Introduction | XSLT 1.0 |
Position in the stylesheet and permitted contents:
Element xsl:for-each can occur anywhere in template rules, inside xsl:template elements. Multiple xsl:for-each instructions can be nested.
An xsl:for-each element may contain any number of xsl:sort instructions, including none, but only at the beginning of its template block. Afterwards, any number or order of XSLT instructions, Literal Result Elements and text is allowed.
Attributes:
Standard attributes apply. Instruction xsl:for-each also has one required attribute, select.
select
Value | xpath-expression |
Usage | Required |
Introduction | XSLT 1.0 |
The select attribute contains an XPath expression yielding a sequence of arbitrary items called the input sequence. It need not contain only nodes. During processing, it is the current sequence (current sequence), and the item being processed is the current item.
Note: In XSLT 1.0, the instruction processes a node set successively. It becomes the Current Node List; each processed node becomes the Current Node.
Purpose:
xsl:for-each is used for pull processing: it actively selects its own nodes through an XPath expression in attribute select.
The sequence is processed in input order unless sorting instructions xsl:sort are present. Then it is sorted first and processed in that order. For each item, the template block in xsl:for-each is instantiated. Each item receives focus while being processed, becoming the Current Item, or Current Node in XSLT 1.0.
Function fn:position() gives the current item’s position; function fn:last() gives the sequence length.
Note – No activation of further template rules inside xsl:for-each:
Since xsl:for-each builds its own sequence and sets the current template rule to null, rules cannot be activated inside it using xsl:apply-imports or xsl:next-match.
Examples:
Example 1 – Loop over elements:
<xsl:for-each select="buchhandel/buch">
<p><xsl:value-of select="."/></p>
</xsl:for-each>
A loop selects all elements <buch> that are children of element <buchhandel>, outputting them in document order.
Example 2 – Loop over sorted elements:
<xsl:for-each select="buchhandel/buch">
<xsl:sort select="@ersch-jahr" data-type="number"/>
<p><xsl:value-of select="."/></p>
</xsl:for-each>
The node sequence selected by attribute select of xsl:for-each is first sorted using xsl:sort by the numeric value of attribute ersch-jahr, holding each book’s publication year.
Example 3 – Two nested loops:
<xsl:template match="lied">
<h3><xsl:value-of select="titel"/></h3>
<!-- Künstler ausgeben, die den Song spielen -->
<p><b>Künstler: </b><br/>
<!-- äußere Schleife -->
<xsl:for-each select="kuenstler">
<xsl:value-of select="."/>
<!-- Liste der Songs desselben Künstlers -->
(auch:
<!-- innere Schleife -->
<xsl:for-each select="//lied[kuenstler/text()=current()/text()][. != current()/parent::lied]">
<xsl:value-of select="titel"/>
<xsl:if test="fn:position() != last()">
<xsl:text>, </xsl:text>
</xsl:if>
</xsl:for-each>)<br/>
</xsl:for-each>
</p>
</xsl:template>
This more complex example lists songs <lied> and their performers <kuenstler>. Each performer also gets a list of their other songs.
The outer xsl:for-each loop lists every performer of the template’s current song:
<xsl:for-each select="kuenstler">
An inner xsl:for-each loop lists their other songs:
<xsl:for-each select="//lied[kuenstler/text() = current()/text()][. != current()/parent::lied]">
Function current() excludes the first-listed performer from the contextual list. The changing current node is important here.
For the template, attribute match makes a <lied> node the current item. From it, the outer xsl:for-each loop selects all <kuenstler> elements naming the performers. Inside this loop, each output performer is current.
The inner xsl:for-each loop selects all songs by the output performer. While evaluating this inner loop’s select attribute, the outer loop’s current node still applies, so current() can be used. The current node changes to a <lied> node only inside the loop body. See function current() for further explanation.
Element definition:
XSLT 1.0:
<!-- Category: instruction -->
<xsl:for-each
select = node-set-expression>
<!-- Content: (xsl:sort*, template) -->
</xsl:for-each>
XSLT 2.0:
<!-- Category: instruction -->
<xsl:for-each
select = sequence-expression>
<!-- Content: (xsl:sort*, sequence-constructor) -->
</xsl:for-each>
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