<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet href="../make-menu.xsl" type="text/xsl"?><html>
   <head>
      <this-is section="xsl-elements" page="iterate" subpage=""/>
      <!--
           Generated at 2011-12-09T20:47:22.916Z--><title>Saxonica: XSLT and XQuery Processing: xsl:iterate</title>
      <meta name="coverage" content="Worldwide"/>
      <meta name="copyright" content="Copyright Saxonica Ltd"/>
      <meta name="title" content="Saxonica: XSLT and XQuery Processing: xsl:iterate"/>
      <meta name="robots" content="noindex,nofollow"/>
      <link rel="stylesheet" href="../saxondocs.css" type="text/css"/>
   </head>
   <body class="main">
      <h1>xsl:iterate</h1>
      <p>The <code>xsl:iterate</code> instruction is new in XSLT 3.0. It is similar to <code>xsl:for-each</code>,
         except that the items in the input sequence are processed sequentially, and after processing each item
         in the input sequence it is possible to set parameters for use in the next iteration. It can therefore
         be used to solve problems than in XSLT 2.0 require recursive functions or templates.</p>
      <p>Here is an example that computes the running balance of a sequence of financial transactions:</p>
      <div class="codeblock"
           style="border: solid thin; background-color: #B1CCC7; padding: 2px">
         <pre>
            <code>
    &lt;xsl:iterate select="transactions/transaction"&gt;
      &lt;xsl:param name="balance" select="0.00" as="xs:decimal"/&gt;
      &lt;xsl:variable name="newBalance" 
                    select="$balance + xs:decimal(@value)"/&gt;
      &lt;balance date="{@date}" value="{$newBalance}"/&gt;
      &lt;xsl:next-iteration&gt;
        &lt;xsl:with-param name="balance" select="$newBalance"/&gt;
      &lt;/xsl:next-iteration&gt;
      &lt;/xsl:iterate&gt;</code>
         </pre>
      </div>
      <p>As well as <code>xsl:next-iteration</code>, the instruction allows a child element <code>xsl:break</code>
         which causes premature completion before the entire input sequence has been processed, and a child element
         <code>xsl:on-completion</code> which defines processing to be carried out when the input sequence is exhausted. 
               The instructions within <code>xsl:on-completion</code>
         have access to the final values of the parameters declared in the <code>xsl:next-iteration</code> instruction
         set while processing the last item in the sequence.</p>
      <p>Here is an example that copies the input sequence up to the first <code>br</code> element:</p>
      <div class="codeblock"
           style="border: solid thin; background-color: #B1CCC7; padding: 2px">
         <pre>
            <code>
    &lt;xsl:iterate select="*"&gt;
      &lt;xsl:choose&gt;
        &lt;xsl:when test="self::br"&gt;
          &lt;xsl:break/&gt;
        &lt;/xsl:when&gt;
        &lt;xsl:otherwise&gt;
          &lt;xsl:copy-of select="."/&gt;
        &lt;/xsl:otherwise&gt;
      &lt;/xsl:choose&gt;  
    &lt;/xsl:iterate&gt;</code>
         </pre>
      </div>
      <table width="100%">
         <tr>
            <td>
               <p align="right"><a class="nav" href="key.xml">Next</a></p>
            </td>
         </tr>
      </table>
   </body>
</html>