Chapter 66: XSLT Apply

1. What does <xsl:apply-templates> really do?

<xsl:apply-templates> is the most important instruction in XSLT after <xsl:template> itself.

It tells the processor:

“Now take these nodes (selected by the select attribute) and process each one of them using the best matching template you can find.”

In other words:

  • It is the “continue processing my children” command
  • It is the mechanism that makes XSLT recursive and template-driven
  • It is what creates the push-style (also called template-driven or event-driven) programming model of XSLT

Everyday analogy

Imagine you are cooking a big meal and you have several assistants (the templates).

You are currently preparing the main dish.

At some point you say:

“Someone please handle all the vegetables now.”

→ <xsl:apply-templates select=”vegetables”/>

Then each vegetable (carrot, potato, onion…) looks for the assistant (template) that knows how to handle that kind of vegetable.

  • Carrot → “I know how to peel and chop carrots” → runs that template
  • Potato → “I know how to boil potatoes” → runs another template

<xsl:apply-templates> is you delegating the work to the right specialists.

2. Basic syntax – the two most common patterns

Pattern A – No select attribute (process all children)

XML

→ the processor will automatically process every child node of <book> (title, author, price, etc.)

Pattern B – With select attribute (process specific children)

XML

Very important difference

  • <xsl:apply-templates/> → process all children in document order
  • <xsl:apply-templates select=”…”/> → process only the nodes that match the expression (in document order)

3. Visual example – how the processor walks the tree

Input XML

XML

XSLT rules

XML

Step-by-step what happens

  1. Processor starts at root → finds match=”/catalog” template
  2. Outputs <ul>
  3. Sees <xsl:apply-templates select=”product”/> → finds all <product> children → processes first product → finds match=”product” template → outputs <li><strong>Wireless Mouse</strong> → sees <xsl:apply-templates select=”price”/> → finds the <price> child → processes it → outputs – ₹1499.00 → finishes the <li> → processes second product → same steps
  4. Finishes the <ul>

Result

HTML

4. Real example: Product catalog with conditional stock message

XML

Result (for stock=12)

HTML

Lesson 5 – Common beginner mistakes & how to avoid them

Mistake 1 Forgetting <xsl:apply-templates> → children are ignored

XML

Fix Add <xsl:apply-templates/> or <xsl:apply-templates select=”…”/>

Mistake 2 Using <xsl:for-each> when <xsl:apply-templates> is better

XML

→ hard to reuse or override later

Better (more modular)

XML

Mistake 3 Not understanding the context change

Inside <xsl:apply-templates select=”price”>, the current node becomes the <price> element.

XML

Lesson 6 – Try yourself exercises (do these!)

  1. Make a simple bullet list of product names using <xsl:apply-templates>
  2. Show name + price with currency symbol
  3. Add a red “Low stock!” message if stock < 20 (in a separate template)
  4. Create a table using <xsl:apply-templates> for rows
  5. Show “Free shipping” only if price > 2000 (in its own template)
  6. Make a nested structure: catalog → products → details

Summary – <xsl:apply-templates> Quick Reference

Question / Goal Recommended pattern Important note / trap
Process all children <xsl:apply-templates/> Processes everything — including text nodes
Process only specific children <xsl:apply-templates select=”product”/> Very common & powerful
Process children in different order Use multiple <xsl:apply-templates> with different select Order is controlled by you
Reuse same element in different contexts Separate template with match=”product” Templates are global — can be called from anywhere
Override behavior in subtree More specific match wins (longer path = higher priority) Very powerful feature
Skip processing children Don’t write <xsl:apply-templates/> Children are ignored unless processed elsewhere

Would you like to continue with one of these next?

  • <xsl:apply-templates> vs <xsl:for-each> – detailed comparison + when to choose which
  • Priority & conflict resolution (which template wins?)
  • Modes – <xsl:apply-templates mode=”pdf”/> (very powerful)
  • Pushing vs pulling – push style (apply-templates) vs pull style (for-each/value-of)
  • Real-world patterns — invoice lines, order items, report sections, hierarchical navigation
  • Debugging when apply-templates “does nothing” or processes wrong nodes

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 *