Chapter 62: XSLT

1. What does <xsl:for-each> really do?

<xsl:for-each> is the classic loop in XSLT 1.0.

It lets you:

Take a set of nodes (selected by an XPath expression) and repeat the same block of output code once for each node in that set.

It is the XSLT equivalent of:

JavaScript

or in Python:

Python

Very important mindset shift

In normal programming languages, you usually write:

text

In XSLT push style (the more elegant way), you prefer templates + apply-templates.

But sometimes you need exact control over the order or very simple flat repetition — and that’s when <xsl:for-each> is useful.

2. Basic syntax – the pattern you will see 95% of the time

XML
  • select=”…” → XPath expression that returns a node-set (list of nodes)
  • For each node in that set:
    • the current node becomes the context node inside the loop
    • the block is executed once

3. Very first realistic example – simple list

Input XML

XML

XSLT

XML

Result HTML

HTML

Inside the loop:

  • First time: current node = first <product>
  • Second time: current node = second <product>

That’s why name and price refer to the current product each time.

4. Example 2 – Adding position number + conditional formatting

XML

Result table rows

HTML

5. Example 3 – Using <xsl:for-each> with sorting

XML

→ products will be listed in alphabetical order

Note: <xsl:sort> must be first child inside <xsl:for-each>

6. Example 4 – <xsl:for-each> vs <xsl:apply-templates> (very important comparison)

Pull style – for-each

XML

Push style – apply-templates (usually preferred)

XML

When to prefer <xsl:apply-templates> over <xsl:for-each>

  • You want reusable rules for the same element type in different places
  • You have nested structures (book → chapter → paragraph)
  • You want to override behavior in different parts of the document
  • You want the processor to automatically handle hierarchy

When to prefer <xsl:for-each>

  • You need very strict control over order and layout
  • You are building a flat list or table row
  • You want to sort or group explicitly
  • You are doing simple repetition without deep nesting

7. Common beginner mistakes & how to avoid them

Mistake 1 Forgetting that context changes inside the loop

XML

Fix Inside the loop, the current product is the context:

XML

Mistake 2 Trying to modify variables inside <xsl:for-each> (XSLT 1.0 doesn’t allow it)

XSLT 1.0 variables are immutable.

Fix (use XSLT 2.0/3.0 or rethink the logic)

Mistake 3 Expecting <xsl:for-each> to create a new scope like a function

No — the context node changes, but variables from outside are still visible.

8. Try yourself exercises (do these!)

  1. Create a numbered list of all product names using <xsl:for-each>
  2. Make a table row for each product with columns: Position | Name | Price
  3. Show “Low stock!” in red if stock < 20
  4. Sort products alphabetically by name
  5. Show only products where price > 1500
  6. Create a comma-separated list of all names

Summary – <xsl:for-each> Quick Reference

Feature / Question Answer / Pattern
Basic syntax <xsl:for-each select=”products”> … </xsl:for-each>
Access current item name, price, @currency (relative to current node)
Get position (1-based) position()
Get total count last()
Sort the loop <xsl:sort select=”name”/> as first child
Prefer over for-each <xsl:apply-templates select=”product”/> + separate template
When to use for-each Flat lists, tables, strict order, sorting

Would you like to continue with one of these next?

  • <xsl:for-each> vs <xsl:apply-templates> – detailed comparison with examples
  • Sorting inside <xsl:for-each> (multiple sort keys)
  • Grouping with <xsl:for-each-group> (XSLT 2.0/3.0)
  • Nested <xsl:for-each> – products → variants → options
  • Real-world patterns — invoice lines, order items, report rows
  • Debugging when for-each outputs nothing or wrong order

Just tell me which direction feels most useful or interesting for you right now! 😊

You may also like...

Leave a Reply

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