Causes iteration over the nodes selected by a node-set expression.

Category: instruction
Content: ( xsl:sort* , sequence-constructor )
Permitted parent elements: any XSLT element whose content model is sequence-constructor; any literal result element




Defines the nodes over which the statement will iterate. The XSLT statements subordinate to the xsl:for-each element are applied to each source node selected by the node-set expression in turn. The full syntax of expressions is outlined in XPath Expression Syntax.

Notes on the Saxon implementation

Saxon-EE offers an extension to the xsl:for-each instruction; the saxon:threads attribute allows the items in the input sequence to be processed in parallel.

For xsl:for-each to be streamable, the W3C rules require that the select expression must be "striding", which essentially means that it may use the child axis but not the descendant axis (to ensure that selected nodes do not overlap each other). Prior to Saxon 9.5, Saxon attempted to be more liberal than this, and allow limited streaming also when the descendant axis was used. Since Saxon 9.6, the implementation has been brought into line with the W3C specification. In many cases the restriction can be circumvented by using the outermost function, for example the expression outermost(//title) is striding even though it uses the descendant axis.


The xsl:for-each element can be used as an alternative to xsl:apply-templates where the child nodes of the current node are known in advance.

It may have one or more xsl:sort child elements to define the order of sorting. The sort keys are specified in major-to-minor order. The expression used for sorting can be any string expression. The following are particularly useful:


Example 1

<xsl:template match="BOOKLIST"> <TABLE> <xsl:for-each select="BOOK"> <TR> <TD><xsl:value-of select="TITLE"/></TD> <TD><xsl:value-of select="AUTHOR"/></TD> <TD><xsl:value-of select="ISBN"/></TD> </TR> </xsl:for-each> </TABLE> </xsl:template>

Example 2

Sorting with xsl:for-each. This example shows a template for a <BOOKLIST> element which processes all the child <BOOK> elements in order of their child <AUTHOR> elements.

<xsl:template match="BOOKLIST"> <h2> <xsl:for-each select="BOOK"> <xsl:sort select="AUTHOR"/> <p>AUTHOR: <xsl:value-of select="AUTHOR"/></p> <p>TITLE: <xsl:value-of select="TITLE"/></p> <hr/> </xsl:for-each> </h2> </xsl:template>

Links to W3C specifications

XSLT 2.0 Specification

XSLT 3.0 Specification

See also