SAXONICA

Saxon Documentation
Full Contents
About Saxon
Changes in this Release
Conditions of Use
Using XSLT 2.0
Using XQuery
Handling Source Documents
XML Schema Processing
The XPath API
Saxon on .NET
Extensibility
Saxon Extensions
Sample Saxon Applications
The Saxon SQL Extension
XSLT Elements
| Introduction
| xsl:analyze-string
| xsl:apply-imports
| xsl:apply-templates
| xsl:attribute
| xsl:attribute-set
| xsl:call-template
| xsl:character-map
| xsl:choose
| xsl:comment
| xsl:copy
| xsl:copy-of
| xsl:decimal-format
| xsl:document
| xsl:element
| xsl:fallback
» | xsl:for-each
| xsl:for-each-group
| xsl:function
| xsl:if
| xsl:include
| xsl:import
| xsl:import-schema
| xsl:key
| xsl:matching-substring
| xsl:message
| xsl:namespace
| xsl:namespace-alias
| xsl:next-match
| xsl:non-matching-substring
| xsl:number
| xsl:otherwise
| xsl:output
| xsl:output-character
| xsl:param
| xsl:perform-sort
| xsl:preserve-space
| xsl:processing-instruction
| xsl:result-document
| xsl:sequence
| xsl:sort
| xsl:strip-space
| xsl:stylesheet
| xsl:template
| xsl:text
| xsl:value-of
| xsl:variable
| xsl:when
| xsl:with-param
| Literal Result Elements
| XSLT Patterns
XPath 2.0 Expression Syntax
Function Library
Standards Conformance

# xsl:for-each

The `xsl:for-each` element causes iteration over the nodes selected by a node-set expression. It can be used as an alternative to `xsl:apply-templates` where the child nodes of the current node are known in advance. There is a mandatory attribute, `select`, which defines the nodes over which the statement will iterate. The XSLT statements subordinate to the `xsl:for-each` element are applied to each source node selected by the node-set expression in turn.

The full syntax of expressions is outlined in XPath Expression Syntax.

The `xsl:for-each` element may have one or more `xsl:sort` child elements to define the order of sorting. The sort keys are specified in major-to-minor order.

The expression used for sorting can be any string expressions. The following are particularly useful:

- element-name, e.g. TITLE: sorts on the value of a child element

- attribute-name, e.g. @CODE: sorts on the value of an attribute

- ".": sorts on the character content of the element

- "qname(.)": sorts on the name of the element

Example 1:

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

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.

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

Next