Chapter 57: XSLT Introduction
1. What is XSLT? (The clearest honest explanation)
XSLT = XSL Transformations (also sometimes just called XSL)
XSLT is a special-purpose language that was designed to do one main job:
Take an XML document as input Transform / rearrange / filter / calculate / rename / decorate it Produce a new document as output
The output is most commonly:
- Another XML document
- HTML (very very common – to display data in a browser)
- Plain text
- JSON (in modern XSLT 3.0)
- PDF (indirectly via XSL-FO)
Real-life analogy everyone understands
Imagine you have a raw shopping list written in XML:
|
0 1 2 3 4 5 6 7 8 9 10 |
<shopping-list> <item quantity="2">milk</item> <item quantity="1">bread</item> <item quantity="500">flour</item> </shopping-list> |
You want to turn this raw list into a beautiful shopping note that your family can read on their phone:
- Heading “Shopping for tomorrow”
- Numbered list
- Convert grams to kg
- Add a friendly message
XSLT is the smart assistant who reads your raw list and follows your instructions to produce the nice note.
You don’t write loops or variables like in Python or JavaScript — you write rules:
“When you see <shopping-list>, output
Shopping for tomorrow
” “When you see <item>, output
- + the quantity + the name +
”
This is the declarative style of XSLT — you describe what the result should look like, not how to build it step by step.
2. Why do people still use XSLT in 2025–2026? (honest answer)
Even though JSON is dominant for new APIs, XSLT is still very alive in certain worlds because:
| Reason | Typical situations / industries | Still very active today? |
|---|---|---|
| Mandatory standards require XML + transformation | e-Invoicing (UBL, GST, PEPPOL, Factur-X), ISO 20022, HL7 CDA | Extremely high |
| Need very strict validation before/after transformation | Financial messages, legal/government documents, healthcare | Very high |
| Large documents + streaming transformation | Batch invoices (thousands), customs declarations, catalog feeds | Yes |
| Mixed content & document-oriented data | Publishing (DocBook, DITA), technical manuals, legal XML | Yes |
| Legacy enterprise systems | Many banks, insurance, ERP, government portals | Very common |
| Separation of data & presentation | Designers can work on XSLT without touching backend code | Still used |
Bottom line (2025–2026 reality):
- New web/mobile apps → almost always JSON + JavaScript/React
- Enterprise / finance / government / healthcare / publishing / B2B → XSLT is still very common (and sometimes mandatory)
3. The absolute minimal XSLT stylesheet (copy-paste & understand)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <!-- Tell the processor: output HTML --> <xsl:output method="html" indent="yes" encoding="UTF-8"/> <!-- Main template - matches the root of the input XML --> <xsl:template match="/"> <html> <head> <title>Hello from XSLT</title> </head> <body> <h1>Welcome!</h1> <p>This page was generated by XSLT.</p> <p>Even if your input XML was empty, I still produce this.</p> </body> </html> </xsl:template> </xsl:stylesheet> |
What this does:
- When the XSLT processor starts reading any XML document, it finds the template that matches the root (/)
- It runs the code inside that template
- It outputs a complete HTML page — even if the input was <nothing/>
4. Real example 1 – Transform simple product list into HTML table
Input XML (products.xml)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?xml version="1.0" encoding="UTF-8"?> <catalog> <product> <name>Wireless Mouse</name> <price currency="INR">1499.00</price> <stock>45</stock> </product> <product> <name>USB-C Hub</name> <price currency="INR">2499.00</price> <stock>12</stock> </product> </catalog> |
XSLT stylesheet (products-to-html.xsl)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" indent="yes" encoding="UTF-8"/> <!-- Match the root --> <xsl:template match="/"> <html> <head> <title>Product Catalog</title> <style> table { border-collapse: collapse; width: 80%; margin: 20px auto; } th, td { border: 1px solid #ccc; padding: 10px; text-align: left; } th { background-color: #f0f0f0; } .low-stock { color: red; font-weight: bold; } </style> </head> <body> <h1>Our Products</h1> <table> <tr> <th>Name</th> <th>Price</th> <th>Stock</th> </tr> <!-- Process every <product> --> <xsl:apply-templates select="catalog/product"/> </table> </body> </html> </xsl:template> <!-- Template for each <product> --> <xsl:template match="product"> <tr> <td><xsl:value-of select="name"/></td> <td> <xsl:value-of select="price"/> <xsl:text> </xsl:text> <xsl:value-of select="price/@currency"/> </td> <td> <xsl:choose> <xsl:when test="stock &lt; 20"> <span class="low-stock"><xsl:value-of select="stock"/> (Low!)</span> </xsl:when> <xsl:otherwise> <xsl:value-of select="stock"/> </xsl:otherwise> </xsl:choose> </td> </tr> </xsl:template> </xsl:stylesheet> |
Result HTML (what the browser sees)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<html> <head>...</head> <body> <h1>Our Products</h1> <table> <tr><th>Name</th><th>Price</th><th>Stock</th></tr> <tr> <td>Wireless Mouse</td> <td>1499.00 INR</td> <td>45</td> </tr> <tr> <td>USB-C Hub</td> <td>2499.00 INR</td> <td class="low-stock">12 (Low!)</td> </tr> </table> </body> </html> |
Lesson 5 – Core XSLT building blocks (with examples)
5.1 <xsl:template match=”pattern”> – the heart of XSLT
|
0 1 2 3 4 5 6 7 8 9 10 11 |
<xsl:template match="product"> <div class="card"> <h3><xsl:value-of select="name"/></h3> <p>₹<xsl:value-of select="price"/></p> </div> </xsl:template> |
- match=”product” → this rule runs when the processor sees a <product>
- <xsl:value-of select=”price”/> → copy the text content of the <price> child
5.2 <xsl:apply-templates> – “now process my children”
|
0 1 2 3 4 5 6 7 8 9 10 11 |
<xsl:template match="catalog"> <div class="catalog"> <h1>Products</h1> <xsl:apply-templates select="product"/> </div> </xsl:template> |
Without apply-templates, the <product> elements would be ignored.
5.3 <xsl:value-of select=”expression”/> – output text
|
0 1 2 3 4 5 6 7 8 |
<xsl:value-of select="price"/> <xsl:value-of select="price/@currency"/> <xsl:value-of select="concat('Total: ₹', price * 1.18)"/> |
5.4 <xsl:if> – simple condition
|
0 1 2 3 4 5 6 7 8 |
<xsl:if test="stock &lt; 10"> <span style="color:red">Only <xsl:value-of select="stock"/> left!</span> </xsl:if> |
5.5 <xsl:choose> – if / else-if / else
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<xsl:choose> <xsl:when test="stock = 0"> <span style="color:red">Out of stock</span> </xsl:when> <xsl:when test="stock &lt; 10"> <span style="color:orange">Low stock</span> </xsl:when> <xsl:otherwise> <span style="color:green">Available</span> </xsl:otherwise> </xsl:choose> |
5.6 <xsl:for-each> – classic loop
|
0 1 2 3 4 5 6 7 8 9 10 |
<ul> <xsl:for-each select="product"> <li><xsl:value-of select="name"/> – ₹<xsl:value-of select="price"/></li> </xsl:for-each> </ul> |
Note: Prefer <xsl:apply-templates> over <xsl:for-each> when possible — it’s more modular.
Lesson 6 – Try yourself exercises (do these!)
- Output only the product names in a bullet list
- Show name + price with currency symbol
- If stock < 20, show “Low stock!” in red
- Create a table with columns: Name, Price, Stock
- Add a message “Free shipping” if price > 2000
- Show only products from category “electronics”
Lesson 7 – 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
- Real-world example — GST invoice → nice HTML
Just tell me which direction feels most useful or interesting for you right now! 😊
