Name
<xsl:for-each> — Acts as XSLT’s iteration operator. This element has a select
attribute that selects some nodes from the current context.
Category
Instruction
Required Attributes
- select
Contains an XPath expression that selects nodes from the current context.
Optional Attributes
None.
Content
<xsl:for-each>
contains a template that is evaluated against each of the selected nodes. The <xsl:for-each>
element can contain one or more <xsl:sort>
elements to order the selected nodes before they are processed. All <xsl:sort>
elements must appear first, before the template begins.
Appears in
<xsl:for-each>
appears inside a template.
Defined in
XSLT section 8, Repetition.
Example
We’ll demonstrate the <xsl:for-each>
element with the following stylesheet:
<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/> <xsl:variable name="newline"> <xsl:text> </xsl:text> </xsl:variable> <xsl:variable name="complicatedVariable"> <xsl:choose> <xsl:when test="count(//listitem) > 10"> <xsl:text>really long list</xsl:text> </xsl:when> <xsl:when test="count(//listitem) > 5"> <xsl:text>moderately long list</xsl:text> </xsl:when> <xsl:otherwise> <xsl:text>fairly short list</xsl:text> </xsl:otherwise> </xsl:choose> </xsl:variable> <xsl:template match="/"> <xsl:value-of select="$newline"/> <xsl:text>Here is a </xsl:text> <xsl:value-of select="$complicatedVariable"/> <xsl:text>:</xsl:text> <xsl:value-of select="$newline"/> <xsl:variable name="listitems" ...
Get XSLT now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.