Partager via


xsl:for-each Element (Compact 2013)

3/26/2014

Applies a template repeatedly, applying it in turn to each node in a set.

Syntax

<xsl:for-each
  select = expression>
</xsl:for-each>

Attributes

  • select
    [required] Expression evaluated on the current context to determine the set of nodes to iterate over.

Element Information

Number of occurrences

Unlimited

Parent elements

xsl:attribute, xsl:comment, xsl:copy, xsl:element, 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:call-template, xsl:choose, xsl:comment, xsl:copy, xsl:copy-of, xsl:element, xsl:if, xsl:processing-instruction, xsl:sort, xsl:text, xsl:value-of, xsl:variable, output elements

Remarks

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.

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/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>

See Also

Reference

XSLT Elements