5️⃣ 🎨 XSLT Tutorial
Estimated reading: 4 minutes 32 views

🔁 XSLT <xsl:for-each> – Loop Through XML Elements Dynamically

🧲 Introduction – Why Learn <xsl:for-each>?

In real-world XML documents, you often have repeated elements like product lists, blog posts, or books in a catalog. The <xsl:for-each> element lets you iterate over node sets and apply formatting or transformations to each item. It’s a powerful tool for dynamically generating output from repeating XML structures.

🎯 In this guide, you’ll learn:

  • How <xsl:for-each> works and when to use it
  • How to loop over node sets with XPath
  • Examples of generating HTML lists and tables from XML
  • Best practices and comparison to <xsl:apply-templates>

🧾 Syntax of <xsl:for-each>

<xsl:for-each select="XPathExpression">
  <!-- Output for each node -->
</xsl:for-each>
  • select: XPath that returns a node set (e.g., list of <book> elements)
  • The inner content is rendered once for each node in the set

📄 Sample XML

<catalog>
  <book id="101">
    <title>Learn XSLT</title>
    <author>Jane Doe</author>
  </book>
  <book id="102">
    <title>Advanced XML</title>
    <author>John Smith</author>
  </book>
</catalog>

📘 Basic Example – List Titles

<xsl:for-each select="catalog/book">
  <p><xsl:value-of select="title"/></p>
</xsl:for-each>

✅ Output:

<p>Learn XSLT</p>
<p>Advanced XML</p>

🧠 Example – Generate HTML Table

<table border="1">
  <tr>
    <th>Title</th>
    <th>Author</th>
  </tr>
  <xsl:for-each select="catalog/book">
    <tr>
      <td><xsl:value-of select="title"/></td>
      <td><xsl:value-of select="author"/></td>
    </tr>
  </xsl:for-each>
</table>

✅ Converts all books into table rows.


🔢 Add Index with position()

<xsl:for-each select="catalog/book">
  <p>
    <xsl:value-of select="position()"/>. 
    <xsl:value-of select="title"/>
  </p>
</xsl:for-each>

✅ Outputs a numbered list of titles.


🔀 Difference: <xsl:for-each> vs <xsl:apply-templates>

Feature<xsl:for-each><xsl:apply-templates>
Use caseManual loopingTemplate-based recursion
Output controlFull control inside the loopDelegated to templates
Position trackingEasy with position()Complex with templates
Nesting flexibilityMediumHigh

✅ Best Practices for <xsl:for-each>

  • ✔️ Use when you need precise control over loop structure/output
  • ✔️ Use position() for counters or odd/even logic
  • ✔️ Combine with <xsl:sort> for ordered output
  • ❌ Don’t overuse it for complex recursion—prefer templates for deep nesting

📌 Summary – Recap & Next Steps

<xsl:for-each> is your go-to tool for rendering repeated XML elements. It helps turn node sets into lists, tables, paragraphs, or any structure you need. It’s straightforward, powerful, and ideal for XML that follows consistent patterns.

🔍 Key Takeaways:

  • Use select="XPath" to iterate over a node set
  • Output is repeated once per node
  • Use position() for counters or conditional formatting

⚙️ Real-world relevance: Used in catalog listings, report generators, XML-to-HTML converters, dashboards, and RSS feed renderers.


❓ FAQs – XSLT <for-each>

❓ What is <xsl:for-each> used for?
✅ To loop over repeating XML elements and apply transformations/output to each.

❓ How do I number each iteration?
✅ Use position() inside the loop.

❓ Can I use <xsl:sort> inside <xsl:for-each>?
✅ Yes. Place <xsl:sort> as the first child of <xsl:for-each>.

❓ Is <xsl:for-each> better than <xsl:apply-templates>?
✅ Not better—just different. Use <xsl:for-each> for inline logic and output control.

❓ Can I nest <for-each> loops?
✅ Yes. You can loop over child nodes within each iteration.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

XSLT <for-each>

Or Copy Link

CONTENTS
Scroll to Top