<?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="for-each" subpage=""/>
      <!--
           Generated at 2011-12-09T20:47:22.916Z--><title>Saxonica: XSLT and XQuery Processing: xsl:for-each</title>
      <meta name="coverage" content="Worldwide"/>
      <meta name="copyright" content="Copyright Saxonica Ltd"/>
      <meta name="title" content="Saxonica: XSLT and XQuery Processing: xsl:for-each"/>
      <meta name="robots" content="noindex,nofollow"/>
      <link rel="stylesheet" href="../saxondocs.css" type="text/css"/>
   </head>
   <body class="main">
      <h1>xsl:for-each</h1>
      <p>The <code>xsl:for-each</code> element causes iteration over the nodes selected by a node-set expression.
It can be used as an alternative to <code>xsl:apply-templates</code> where the child nodes of the
current node are known in advance. There is a mandatory attribute, <code>select</code>, which defines
the nodes over which the statement will iterate. The XSLT statements subordinate to the <code>xsl:for-each</code> element
are applied to each source node selected by the node-set expression in turn.</p>
      <p>The full syntax of expressions is outlined in <a class="bodylink" href="../expressions/intro.xml">XPath Expression Syntax</a>.</p>
      <p>The <code>xsl:for-each</code> element may have one or more <code>xsl:sort</code> child elements to define the order of
 sorting. The sort keys are specified in major-to-minor order.</p>
      <p>The expression used for sorting can be any string expressions. The following are
particularly useful:</p>
      <ul>
         <li content="para">
            <p>element-name, e.g. TITLE: sorts on the value of a child element</p>
         </li>
         <li content="para">
            <p>attribute-name, e.g. @CODE: sorts on the value of an attribute</p>
         </li>
         <li content="para">
            <p>".": sorts  on the character content of the element</p>
         </li>
         <li content="para">
            <p>"qname(.)": sorts on the name of the element</p>
         </li>
      </ul>
      <p>Example 1:</p>
      <div class="codeblock"
           style="border: solid thin; background-color: #B1CCC7; padding: 2px">
         <pre>
            <code>&lt;xsl:template match="BOOKLIST"&gt;
    &lt;TABLE&gt;
    &lt;xsl:for-each select="BOOK"&gt;
        &lt;TR&gt;
        &lt;TD&gt;&lt;xsl:value-of select="TITLE"/&gt;&lt;/TD&gt;
        &lt;TD&gt;&lt;xsl:value-of select="AUTHOR"/&gt;&lt;/TD&gt;
        &lt;TD&gt;&lt;xsl:value-of select="ISBN"/&gt;&lt;/TD&gt;
        &lt;/TR&gt;        
    &lt;/xsl:for-each&gt;
    &lt;/TABLE&gt;
&lt;/xsl:template&gt;</code>
         </pre>
      </div>
      <p>Example 2: sorting with xsl:for-each. This example also shows a template for a BOOKLIST
element which processes all the child BOOK elements in order of their child AUTHOR elements.</p>
      <div class="codeblock"
           style="border: solid thin; background-color: #B1CCC7; padding: 2px">
         <pre>
            <code>&lt;xsl:template match="BOOKLIST"&gt;
    &lt;h2&gt;
        &lt;xsl:for-each select="BOOK"&gt;
            &lt;xsl:sort select="AUTHOR"/&gt;
            &lt;p&gt;AUTHOR: &lt;xsl:value-of select="AUTHOR"/&gt;&lt;/p&gt;
            &lt;p&gt;TITLE: &lt;xsl:value-of select="TITLE"/&gt;&lt;/p&gt;
            &lt;hr/&gt;
        &lt;/xsl:for-each&gt;            
    &lt;/h2&gt;
    &lt;/xsl:template&gt;</code>
         </pre>
      </div>
      <p>Saxon-EE offers an extension to the <code>xsl:for-each</code> instruction: the <code>saxon:threads</code>
         attribute allows the items in the input sequence to be processed in parallel. For details see
         <a class="bodylink" href="../extensions/attributes/threads.xml">saxon:threads</a>.</p>
      <table width="100%">
         <tr>
            <td>
               <p align="right"><a class="nav" href="for-each-group.xml">Next</a></p>
            </td>
         </tr>
      </table>
   </body>
</html>