Chapter 70: XSLT Examples

XSLT Examples tutorial, written as if I’m your personal teacher sitting next to you with a whiteboard, explaining everything slowly and patiently.

We will go step-by-step through progressively more realistic and useful examples — from the absolute simplest “hello world” transformation to real-world patterns that companies actually use in production in 2025–2026.

Every example includes:

  • the input XML
  • the XSLT code (with line-by-line comments)
  • the expected output
  • explanation of what happens inside the processor
  • common beginner mistakes & how to avoid them
  • why this pattern is useful in real projects

Preparation – Tools you can use right now to test

  1. Online XSLT tester (quick & no setup)
    • https://www.freeformatter.com/xslt-transformer.html
    • https://www.utilities-online.info/xslt/
    • https://xsltfiddle.liberty-development.net/
  2. Local setup (recommended for serious learning)
    • Oxygen XML Editor (free trial / academic license)
    • VS Code + “XSLT/XPath” extension + Saxon-HE jar
    • Command line: xsltproc (Linux/Mac) or Saxon (java -jar saxon-he-12.5.jar -s:input.xml -xsl:style.xsl -o:output.html)

Let’s start.

Example 1 – The absolute minimal XSLT (Hello World style)

Goal: No matter what the input XML is, always output the same HTML page.

Input XML (can be anything – even empty)

XML

XSLT code (minimal.xsl)

XML

What happens inside the processor?

  1. It starts reading any input XML
  2. It immediately looks for a template that matches the root (/)
  3. Finds this one → runs it
  4. Outputs the HTML — no data from input is used

Real-world use case for this pattern

  • “Landing page” / error page when XML is invalid
  • Fallback HTML when transformation fails
  • Simple “XML received” confirmation page

Example 2 – Identity Transform (copy XML almost unchanged)

Goal: Copy the entire input XML → add one small modification

Input XML

XML

XSLT – copy everything + add a <processed> timestamp

XML

Output XML

XML

Why this pattern is used in real life (2025–2026)

  • Add audit timestamp / processed-by / version stamp
  • Add digital signature placeholder
  • Add missing required fields (compliance)
  • Remove sensitive fields (data masking)
  • Rename tags / namespaces for different partner

Example 3 – Real-world: Enrich GST invoice with extra info

Input (simplified GST invoice XML)

XML

Goal: Add:

  • Current processing timestamp
  • Tax category message
  • QR code placeholder

XSLT

XML

Result (new fields added after IssueDate)

XML

Lesson 4 – Common “edit XML” tasks & how XSLT does them

Task XSLT pattern / technique Key code snippet
Add element after specific tag Match preceding tag → copy it + add new element <xsl:template match=”IssueDate”> <xsl:copy-of select=”.”/> <newTag>…</newTag> </xsl:template>
Add attribute to existing element Match element → copy + add <xsl:attribute> <xsl:template match=”Total”> <xsl:copy> <xsl:attribute name=”currency”>INR</xsl:attribute> <xsl:apply-templates/> </xsl:copy> </xsl:template>
Change existing element text Match element → output new value <xsl:template match=”Note”> <Note>Updated note</Note> </xsl:template>
Remove element conditionally Match element + condition → output nothing <xsl:template match=”TaxTotal[TaxAmount=0]”/>
Rename element Match old name → output new tag + copy content `<xsl:template match=”PayableAmount”> <GrandTotal> <xsl:copy-of select=”@*
Move element to different position Copy it → remove original → paste copy Two templates: copy + skip original
Add namespace / schema location Add to root using <xsl:copy> + <xsl:attribute> Very common for compliance

Lesson 5 – Try yourself exercises (do these!)

  1. Add a <ProcessedBy>System XSLT v3.0</ProcessedBy> element after <IssueDate>
  2. Change all <price> currency to “USD”
  3. Remove <stock> element if value = 0
  4. Rename <price> to <unitPrice>
  5. Add <Discount>0.00</Discount> before <price>
  6. Move <author> to be the first child of <book>

Lesson 6 – Real-world context (2025–2026)

Most “edit XML” XSLT today happens on servers in:

  • GST / e-Invoice portals — add IRN, QR code, signed properties
  • PEPPOL / Factur-X / ZUGFeRD — enrich with PDF attachment references
  • ISO 20022 migration — add missing fields, convert currency
  • HL7 CDA — add confidentiality codes, normalize dates
  • ERP → partner integration — rename tags, add partner-specific fields
  • Data masking — remove personal information before sharing

Would you like to continue with one of these next?

  • Identity transform + conditional removal (remove sensitive data)
  • Adding digital signature placeholder / QR code
  • Enriching with calculated fields (tax breakdown, totals)
  • Namespace / schemaLocation changes
  • Multi-step transformation (XSLT → another XSLT)
  • Debugging when changes don’t appear or XML becomes invalid

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 *