Skip to content

xsl:iterate

Loops over a sequence with accumulating parameters that can be updated for each iteration.

<xsl:iterate select="item">
  <xsl:param name="sum" select="0"/>
  ...
  <xsl:next-iteration>
    <xsl:with-param name="sum" select="$sum + ."/>
  </xsl:next-iteration>
</xsl:iterate>

Unlike xsl:for-each, where each iteration is isolated, xsl:iterate carries state forward via parameters. The loop can also terminate early with xsl:break and run a final block once the sequence is exhausted with xsl:on-completion. This makes xsl:iterate the XSLT equivalent of a fold or accumulator loop.

xsl:param children declare the iteration parameters and their initial values; they must come before any other body content. xsl:next-iteration updates these parameters for the next round — if a body executes without reaching xsl:next-iteration, the parameters keep their current values.

Attributes

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

Children

Element Description
xsl:param Declares an iteration parameter with an initial value. Must come first.
xsl:on-completion Runs after the last iteration if the loop was not broken. Accepts a select attribute or a sequence constructor body.
xsl:next-iteration Updates parameters for the next iteration via xsl:with-param children.
xsl:break Terminates the loop immediately. May contain a sequence constructor that produces final output.

Examples

Summing values with xsl:on-completion:

<xsl:iterate select="item">
  <xsl:param name="sum" select="0"/>
  <xsl:on-completion>
    <total><xsl:value-of select="$sum"/></total>
  </xsl:on-completion>
  <xsl:next-iteration>
    <xsl:with-param name="sum" select="$sum + number(.)"/>
  </xsl:next-iteration>
</xsl:iterate>

Breaking out of the loop early:

<xsl:iterate select="line">
  <xsl:if test=". = 'STOP'">
    <xsl:break/>
  </xsl:if>
  <xsl:value-of select="."/>
</xsl:iterate>

xsl:break with content — produce a final element when the break is hit:

<xsl:iterate select="page">
  <xsl:param name="count" select="0"/>
  <xsl:if test="$count = 100">
    <xsl:break>
      <stopped at="{$count}"/>
    </xsl:break>
  </xsl:if>
  <xsl:next-iteration>
    <xsl:with-param name="count" select="$count + 1"/>
  </xsl:next-iteration>
</xsl:iterate>

xsl:on-completion with a select attribute (shorthand for a single-expression body):

<xsl:on-completion select="$accumulator"/>