XML technologies / XSLT / XSLT 3.0 / XSLT streaming / Elements / xsl:for-each

Using xsl:for-each items can be processed independently of other items in the sequence. This means they can be processed in any order or in parallel. They are nevertheless output in input order. Unlike xsl:iterate, this construct also allows the input to be sorted.

The basic structure of xsl:for-each is shown in the following code example.

Code example: introductory example of xsl:for-each.

<xsl:for-each select="Ausdruck">
  <xsl:sort select="Name/Familienname"/>
</xsl:for-each>

Basic structure of xsl:for-each.

Line 1: In the xsl:for-each instruction, each item of an input sequence is processed once by evaluating the instruction’s sequence constructor once for each item. The select attribute is needed to construct the input sequence: an XPath expression is evaluated (an exception here is the extension of maps).

Line 2: The optional xsl:sort element allows the input sequence to be sorted. If it is omitted, the sorted output sequence is the same as the input sequence. In this example, the input sequence is specified to be sorted by surname and then output.

Exercise on xsl:for-each

   

back to top