XML technologies / XSLT / XSLT 3.0 / XSLT streaming / Elements / xsl:iterate

Construct xsl:iterate applies the same instructions to many same-type elements. For example, read every employee’s salary to calculate the average. The root contains any number of elements person to iterate over while reading attribute gehalt.

First, the structure of construct xsl:iterate and its components are explained, followed by an introductory example. The reference is the XSLT 3.0 standard.

Construct xsl:iterate is structured as follows:

Code example: xsl:iterate skeleton.

<xsl:iterate select = Ausdruck > 
  <xsl:on-completion select = Ausdruck > </xsl:on-completion> 
  <xsl:break select = Ausdruck > </xsl:break> 
  <xsl:next-iteration> </xsl:next-iteration> 
</xsl:iterate>

Line 1: Attribute select of instruction xsl:iterate is required and contains an XPath expression creating an input sequence. The content of the xsl:iterate is evaluated once per input item, until the loop processes all items or encounters an element xsl:break, whichever happens first.

Line 2: Optional element xsl:on-completion must appear at the start of element xsl:iterate. Element xsl:on-completion can specify attribute select or child elements, never both!

Line 3: Optional early termination. Attribute select and element contents are optional too. Either specify select or let element xsl:break have children, never both!

Line 4: Construct xsl:next-iteration can set parameter values for the next iteration.

Caution!
Since xsl:iterate is used with streaming, function last is forbidden. Streaming processes one element at a time without accessing the next through next, so it cannot know when the last item is processed.

The example XML represents bank transactions, each with date and amount. A transformation using xsl:iterate produces XML with the balance after each transaction. See Introductory xsl:iterate example – output account XML.

Code example: Introductory xsl:iterate – input transaction XML.

<transactions>
  ...  
  <transaction date="2008-09-01" value="12.00"/> 
  <transaction date="2008-09-01" value="8.00"/> 
  <transaction date="2008-09-02" value="-2.00"/> 
  <transaction date="2008-09-02" value="5.00"/> 
</transactions>

Transaction XML structure.

Line 1: All transactions for a bank account.

Line 2: Any number of elements transaction is permitted.

Line 3: Transaction date (date) and amount (value) attributes.

 

Code example: Introductory xsl:iterate – output account XML.

<account>
  <balance date="2008-09-01" value="12.00"/>
  <balance date="2008-09-01" value="20.00"/> 
  <balance date="2008-09-02" value="18.00"/> 
  <balance date="2008-09-02" value="23.00"/> 
</account>

Account XML structure.

Line 1: Generated root element account.

Line 2: For each element transaction, xsl:iterate creates an element balance.

To transform transaction XML to account XML, write this XSLT transformation:

Code example: Introductory xsl:iterate – XSLT.

<account> 
  <xsl:stream href="transactions.xml"> 
    <xsl:iterate select="transactions/transaction"> 
      <xsl:param name="balance" select="0.00" as="xs:decimal"/> 
      <xsl:variable name="newBalance" select="$balance + xs:decimal(@value)"/> 
      <balance date="{@date}" value="{$newBalance}"/> 
      <xsl:next-iteration> 
        <xsl:with-param name="balance" select="$newBalance"/> 
      </xsl:next-iteration> 
    </xsl:iterate> 
  </xsl:stream> 
</account>

XSLT transforming transaction XML into account XML.

Line 1: Create the root for child elements generated in xsl:iterate: balance.

Line 3: Start iterating over the parameter in select="transactions/transaction". Iterate over all elements transactions.

Line 4: Create parameter balance of type xs:decimal.

Line 6: Transaction date (date) and amount (value) attributes.

Exercise on xsl:iterate

   

back to top