Skip to content

xsl:for-each

Iterates over a sequence, executing its body once per item with that item as the context.

<xsl:for-each select="item">
  ...
</xsl:for-each>

Each iteration is independent — there is no way to carry state from one iteration to the next. For accumulating loops use xsl:iterate; for grouping use xsl:for-each-group. Optional xsl:sort children control the order in which items are visited.

Attributes

Attribute Description
select XPath expression producing the sequence to iterate over. Required.

Examples

Iterating with sorting:

<xsl:for-each select="book">
  <xsl:sort select="@year" data-type="number" order="descending"/>
  <li><xsl:value-of select="title"/> (<xsl:value-of select="@year"/>)</li>
</xsl:for-each>

Iterating over an integer range:

<xsl:for-each select="1 to 10">
  <step n="{.}"/>
</xsl:for-each>