Chapter 60: XSLT

1. What is <xsl:template> really? (the heart of XSLT)

The <xsl:template> element is the most important building block of every XSLT stylesheet.

It is the place where you define:

“When the processor sees this kind of node (or this pattern), then output this content (and maybe process children in a certain way).”

Think of <xsl:template> as a recipe card in a huge kitchen (the XML document).

Each recipe card says:

  • Trigger: when you see this ingredient / this dish being prepared…
  • Instructions: …then do these steps (output HTML, copy text, create new elements, call other recipes…)

The XSLT processor walks through the input XML and whenever it finds a node that matches one of your template triggers, it executes that template.

2. Two completely different ways to write a template

There are two main styles of <xsl:template> — beginners often mix them up.

Style Syntax example When the template runs Most common use case Typical in…
Match template <xsl:template match=”book”> … </xsl:template> When the processor is processing a <book> node The classic, most powerful style 95% of real stylesheets
Named template <xsl:template name=”format-price”> … </xsl:template> Only when you explicitly call it with <xsl:call-template name=”format-price”/> Reusable helper functions (like functions in other languages) Helper logic

Rule to remember forever:

Most of your templates should be match templates (match=”…”) Named templates (name=”…”) are like utility functions — use them when you want to reuse the same logic in many places

3. Anatomy of a <xsl:template match=”…”> (most important style)

XML

What happens when the processor sees a <product>?

  1. It finds this template (because match=”product”)
  2. It executes everything inside the template
  3. When it reaches <xsl:apply-templates select=”description”/> → it pauses, looks for a template that matches <description>, runs it, then continues

4. Real example 1 – Simple product → nice HTML card

Input XML

XML

XSLT

XML

Result HTML

HTML

Lesson 5 – The most important pattern: push style vs pull style

There are two philosophies in XSLT — beginners often mix them.

Push style (preferred – more XSLT-like)

XML

→ The processor pushes the nodes through the templates automatically

Pull style (more like traditional programming)

XML

→ You pull the data exactly where you want it

Rule of thumb (very useful in practice)

  • Use push style (apply-templates) when you want modular, reusable rules and natural hierarchy
  • Use pull style (for-each, value-of) when you want strict control over layout and order

Lesson 6 – Try yourself exercises (do these!)

  1. Create a simple HTML page that shows only product names in <h2> tags
  2. Show name + price with currency symbol
  3. If stock < 20, add a red “Low stock!” warning
  4. Make a table with columns: Name | Price | Stock | Status
  5. Add “Free shipping” message if price > 2000
  6. Show only products with stock > 0

Lesson 7 – Real-world context (where XSLT is still very much used in 2025–2026)

  • Mandatory e-Invoicing — GST, PEPPOL, Factur-X, ZUGFeRD → XML → nice HTML/PDF
  • Financial messages — ISO 20022 payment files → human-readable reports
  • Publishing — DocBook, DITA, JATS → HTML, PDF, EPUB
  • Legacy enterprise integration — SOAP responses → preview / logging
  • EDI / B2B — transform incoming XML → confirmation email / PDF
  • Technical documentation — XML config → styled documentation

Would you like to continue with one of these next?

  • Identity transform (copy almost everything + make small changes)
  • Grouping (group products by category – XSLT 2.0/3.0)
  • Variables and parameters
  • XSLT with namespaces (real e-invoice, SOAP, HL7, Android manifest…)
  • XSLT 1.0 vs 2.0 vs 3.0 – what changed and why it matters
  • Real-world example — GST e-invoice XML → beautiful HTML invoice

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 *