XSLT <for-each>

Looping through XML nodes and elements

🔄 What is <xsl:for-each>?

The <xsl:for-each> element loops through each node in a selected node-set. It allows you to process multiple XML elements repeatedly, displaying or transforming each one.


<!-- Loop through all items -->
<xsl:for-each select="item">
    <xsl:value-of select="name"/>
</xsl:for-each>
                                    

Understanding <for-each>

The <xsl:for-each> element iterates over a collection of nodes, executing its content for each node found. It's perfect for processing lists, tables, and repeating data structures in your XML documents.

🔁

Iteration

Process multiple nodes

<xsl:for-each select="book">
📋

Node Selection

Choose which nodes to loop

select="catalog/book"
🎯

Context Node

Current node in loop

<xsl:value-of select="."/>
📊

List Generation

Create HTML lists/tables

<li><xsl:value-of/></li>

🔹 Basic Example

Loop through multiple books and display their information:

XML Data:

<?xml version="1.0"?>
<library>
    <book>
        <title>XML Guide</title>
        <price>25.00</price>
    </book>
    <book>
        <title>XSLT Basics</title>
        <price>30.00</price>
    </book>
    <book>
        <title>Web Development</title>
        <price>35.00</price>
    </book>
</library>

XSLT Template:

<xsl:template match="library">
    <ul>
        <xsl:for-each select="book">
            <li>
                <xsl:value-of select="title"/> - 
                $<xsl:value-of select="price"/>
            </li>
        </xsl:for-each>
    </ul>
</xsl:template>

Output:

  • XML Guide - $25.00
  • XSLT Basics - $30.00
  • Web Development - $35.00

🔹 Creating HTML Tables

Use for-each to generate table rows from XML data:

XML Data:

<employees>
    <employee>
        <name>John Doe</name>
        <position>Developer</position>
    </employee>
    <employee>
        <name>Jane Smith</name>
        <position>Designer</position>
    </employee>
</employees>

XSLT Template:

<xsl:template match="employees">
    <table border="1">
        <tr>
            <th>Name</th>
            <th>Position</th>
        </tr>
        <xsl:for-each select="employee">
            <tr>
                <td><xsl:value-of select="name"/></td>
                <td><xsl:value-of select="position"/></td>
            </tr>
        </xsl:for-each>
    </table>
</xsl:template>

Output:

Name Position
John Doe Developer
Jane Smith Designer

🔹 Filtering with For-Each

Add conditions to select specific nodes during iteration:

XML Data:

<products>
    <product category="Electronics">Laptop</product>
    <product category="Books">XML Guide</product>
    <product category="Electronics">Phone</product>
</products>

XSLT Template:

<xsl:template match="products">
    <h3>Electronics Only:</h3>
    <ul>
        <xsl:for-each select="product[@category='Electronics']">
            <li><xsl:value-of select="."/></li>
        </xsl:for-each>
    </ul>
</xsl:template>

Output:

Electronics Only:

  • Laptop
  • Phone

🔹 Nested For-Each Loops

Loop through nested XML structures with multiple for-each elements:

XML Data:

<catalog>
    <category name="Fiction">
        <book>Novel A</book>
        <book>Novel B</book>
    </category>
    <category name="Science">
        <book>Physics 101</book>
    </category>
</catalog>

XSLT Template:

<xsl:template match="catalog">
    <xsl:for-each select="category">
        <h3><xsl:value-of select="@name"/></h3>
        <ul>
            <xsl:for-each select="book">
                <li><xsl:value-of select="."/></li>
            </xsl:for-each>
        </ul>
    </xsl:for-each>
</xsl:template>

Output:

Fiction

  • Novel A
  • Novel B

Science

  • Physics 101

💡 Key Points:

  • Use select attribute to specify which nodes to loop through
  • Current node context changes inside the loop
  • Can be nested for complex XML structures
  • Combine with filters using XPath predicates
  • Perfect for generating lists, tables, and repeated HTML structures

🧠 Test Your Knowledge

What does <xsl:for-each> do?