Applies a template repeatedly, applying it in turn to each node in a set.
<xsl:for-each select = expression> </xsl:for-each>
Number of occurrences | Unlimited |
Parent elements | xsl:attribute, xsl:comment, xsl:copy, xsl:element, xsl:for-each, xsl:if, xsl:otherwise, xsl:param, xsl:processing-instruction, xsl:template, xsl:variable, xsl:when, xsl:with-param, output elements |
Child elements | xsl:apply-templates, xsl:attribute, xsl:choose, xsl:comment, xsl:copy, xsl:copy-of, xsl:element, xsl:for-each, xsl:if, xsl:processing-instruction, xsl:sort, xsl:text, xsl:value-of, xsl:variable, output elements |
The <xsl:for-each> element establishes the context for iteration; the XSLT 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.
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/1999/XSL/Transform" version="1.0"> <xsl:template match="/"> <HTML> <BODY> <TABLE> <xsl:for-each select="customers/customer"> <xsl:sort select="state" order="descending"/> <xsl:sort select="name"/> <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>
Creating and Populating an HTML Template