"for-each" - Looping through a Node Set

This section describes the 'for-each' element, which is used in the content of a 'template' element. The 'for-each' element is a loop statement that be used to repeat a block of output content over each node in a node set.

Most XSLT examples we have seen so far are based on a single XML node, a single element or a single attribute. Now, let's learn how to work with a group of elements or attributes using the "for-each" XSLT element.

for-each: A child element used as part of the content of a "template" element. A "for-each" element serves as a loop statement to repeat a block of output content over each node in a node set. The syntax of the "for-each" element is:

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

where the "expression" is an XPath expression to return a set of nodes in the source XML document, and "content" is a mixture literal data and XSLT elements to be repeated for each node in the node set.

Here are some examples of "for-each" elements:

<!-- loop on all child elements of the specified name -->
<xsl:for-each select="child_element_name">
 content
</xsl:for-each>

<!-- loop on all child elements of any names -->
<xsl:for-each select="*">
 content
</xsl:for-each>

<!-- loop on all attributes of any names -->
<xsl:for-each select="@*">
 content
</xsl:for-each>

Some XSLT functions can also be used with the "select" attribute:

<xsl:for-each select="node()">
 XSL statements
</xsl:for-each>
<xsl:for-each select="text()">
 XSL statements
</xsl:for-each>
<xsl:for-each select="comment()">
 XSL statements
</xsl:for-each>

where "node()" matches any child nodes, including element nodes, text nodes, and comment nodes, "text()" matches any child text nodes, and "comment()" matches any child comment nodes.

Once we have the node set defined for the loop, we need to know how to access certain information about this loop, like the total number of iterations. We also need to know who is the current node at the current iteration. Here are some XSLT expressions and functions that are very useful within a loop:

Last update: 2016.

Table of Contents

 About This Book

 Introduction of XML (eXtensible Markup Language)

 XML File Syntax

 XML File Browsers

 DOM (Document Object Model) Programming Interface

 SAX (Simple API for XML) Programming Interface

 DTD (Document Type Definition) Introduction

 Syntaxes of DTD Statements

 Validating an XML Document against the Specified DTD Document Type

 XSD (XML Schema Definition) Introduction

 Syntaxes of XSD Statements

 Validating XML Documents Against Specified XML Schemas

 XSL (Extensible Stylesheet Language) Introduction

 XSLT (XSL Transformations) Introduction

 Java Implementation of XSLT

 XPath (XML Path) Language

XSLT Elements as Programming Statements

 "value-of" - Evaluating XPath String Expressions

 Examples of Using "value-of" Elements

 "{expression}" - Shorthand of "value-of" Elements

 "variable" - Declaring Variables

"for-each" - Looping through a Node Set

 Examples of Using "for-each" Elements

 "if" - The Conditional Element

 "choose" - The If...Else Element

 Control and Generate XML Element in the Result

 XML Notepad - XML Editor

 XML Tools Plugin for Notepad++

 XML 1.1 Changes and Parsing Examples

 Outdated Tutorials

 References

 PDF Printing Version