Chapter 59: XSLT – Transformation

1. What does “transformation” really mean in XSLT?

Transformation = taking one document (almost always XML) and producing a completely new document from it.

The new document can be:

  • another XML document (different structure, different tags, filtered content…)
  • HTML (most common real use case)
  • plain text (CSV, email body, log file…)
  • JSON (XSLT 3.0+)
  • PDF (indirectly via XSL-FO)

Most important mindset to adopt right now:

XSLT is not a general-purpose programming language like Python or JavaScript. It is a declarative, rule-based, pattern-matching language.

You don’t write:

text

Instead you write:

text

The XSLT processor reads your input XML, finds matching patterns, and applies your rules — in the order it finds them, following a very specific processing model.

2. The absolute minimal transformation (copy-paste & understand)

XML

What happens inside the processor?

  1. It starts reading any input XML document
  2. It looks for a template that matches the root of the document (/)
  3. It finds this template → runs the code inside
  4. It outputs the HTML — even if the input was <nothing/>

This is the starting point of every XSLT transformation.

3. Real example 1 – Transform product list into nice HTML table

Input XML (products.xml)

XML

XSLT stylesheet (products-to-html.xsl)

XML

Result HTML (what you see in browser)

HTML

Lesson 4 – Core concepts you must understand deeply

4.1 The processing model – how XSLT really works

  1. Processor starts at the root of the input XML (/)
  2. Looks for a template that matches the root (match=”/”)
  3. Executes the code inside that template
  4. When it sees <xsl:apply-templates select=”something”/> → it pauses, finds all matching nodes, and for each one:
    • looks for the best matching template
    • runs it
    • continues
  5. When no more templates apply → finishes

This is why XSLT feels “automatic” — you define rules, and the processor walks the tree for you.

4.2 <xsl:value-of select=”…”/> – the most used instruction

XML

4.3 <xsl:apply-templates> vs <xsl:for-each>

XML

Rule of thumb: Use apply-templates when you want reusability and hierarchy Use for-each when you want strict control over order or simple flat lists

Lesson 5 – Try yourself exercises (do these!)

  1. Output only the product names in a bullet list
  2. Show name + price with currency symbol
  3. If stock < 20, show “Low stock!” in red
  4. Create a table with columns: Name, Price, Stock, Status
  5. Add a message “Free shipping” if price > 2000
  6. Show only products from category “electronics”

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

  • e-Invoicing — GST XML → human-readable HTML/PDF
  • Financial messages — ISO 20022 → reports
  • Publishing — DocBook/DITA → HTML/PDF/EPUB
  • Legacy SOAP services — transform XML → preview
  • EDI / B2B — EDIFACT → XML → confirmation page
  • Configuration — XML config → documentation

Would you like to continue with one of these next?

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

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 *