Applies a template repeatedly to a set of nodes.
Syntax
<xsl:for-each
order-by="sort-criteria-list"
select="pattern" >
</xsl:for-each>
Attributes
order-by
Sort criteria in a semicolon-separated list. When the first sort results in two equal items, the second sort criterion is checked, and so on. The first non-white-space character in each sort criterion indicates whether the sort is ascending (optional +) or descending (-). The sort criterion is expressed as an XSL pattern, relative to the pattern described in the select attribute.
select
XSL pattern query evaluated the current context to determine the set of nodes to iterate over. The default value "node()" indicates selection of all children of the current node.
Element Information
Number of occurrences | Unlimited |
Parent elements | xsl:attribute, xsl:comment, xsl:copy, xsl:element, xsl:for-each, xsl:if, xsl:otherwise, xsl:pi, xsl:template, xsl:when, output elements |
Child elements | xsl:apply-templates, xsl:attribute, xsl:choose, xsl:comment, xsl:copy, xsl:element, xsl:eval, xsl:for-each, xsl:if, xsl:pi, xsl:value-of, output elements |
Requires closing tag | Yes. XSL is an XML grammar and, like all XML grammars, all tags must have closing tags to satisfy the definition of well-formed. |
Remarks
The xsl:for-each element establishes the context for iteration; the XSL transformation instructions within this loop are to be applied to the selected nodes. Each source element selected by xsl:for-each becomes a new context against which any pattern matching within the xsl:for-each occurs.
Note that the order-by attribute syntax is obsolete. The December XSL Working Draft defines the xsl:sort element instead. Future releases will support the official syntax.
Example
This example specifies a template that defines what the structure of the overall output document should be (a top-level HTML element containing BODY and TABLE elements with repeated rows for each customer) and uses templates to create TD elements for the name, address, and phone source elements.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">
<HTML>
<BODY>
<TABLE>
<xsl:for-each select="customers/customer"
order-by="name; -address/state">
<TR>
<TD><xsl:value-of select="name" /></TD>
<TD><xsl:value-of select="address" /></TD>
<TD><xsl:value-of select="phone" /></TD>
</TR>
</xsl:for-each>
</TABLE>
</BODY>
</HTML>
</xsl:template>
</xsl:stylesheet>
See Also