<?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><xsl:template match="BOOKLIST"> <TABLE> <xsl:for-each select="BOOK"> <TR> <TD><xsl:value-of select="TITLE"/></TD> <TD><xsl:value-of select="AUTHOR"/></TD> <TD><xsl:value-of select="ISBN"/></TD> </TR> </xsl:for-each> </TABLE> </xsl:template></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><xsl:template match="BOOKLIST"> <h2> <xsl:for-each select="BOOK"> <xsl:sort select="AUTHOR"/> <p>AUTHOR: <xsl:value-of select="AUTHOR"/></p> <p>TITLE: <xsl:value-of select="TITLE"/></p> <hr/> </xsl:for-each> </h2> </xsl:template></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>