Chapter 14: XML XSLT
1. What is XSLT? (The clearest possible explanation)
XSLT = XSL Transformations (also sometimes called XSL or XSLT 1.0 / 2.0 / 3.0)
XSLT is a special-purpose transformation language designed to:
- 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 browser)
- Plain text
- JSON (in XSLT 3.0)
- PDF (indirectly via XSL-FO)
Analogy everyone understands:
Imagine you have a raw data sheet written in XML:
- product codes, prices, descriptions, categories…
You want to turn this raw data into a beautiful invoice, web page, email, or report.
XSLT is the chef that takes the raw ingredients (XML) and cooks them into the final dish (HTML, another XML, text…).
2. Why do people use XSLT instead of just writing code in Python/Java/JavaScript?
| Reason | Typical situation |
|---|---|
| The input is XML and output must be structured | Invoices, EDI, government reports, publishing, SOAP responses |
| Need separation between data and presentation | Designers can work on XSLT without touching Java/Python code |
| Very large documents → streaming transformation | XSLT processors can handle GB-sized files without loading everything into memory |
| Industry standards require it | e-Invoice (India GST), HL7 CDA, UBL, ebXML, many banking/finance formats |
| Need declarative transformation | “Describe what the result should look like” instead of “write step-by-step code” |
3. Very First Simple Example – XML → HTML
Input XML (books.xml)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?xml version="1.0" encoding="UTF-8"?> <library> <book> <title>Atomic Habits</title> <author>James Clear</author> <year>2018</year> <price>499.00</price> </book> <book> <title>Rich Dad Poor Dad</title> <author>Robert Kiyosaki</author> <year>1997</year> <price>350.00</price> </book> </library> |
XSLT stylesheet (books-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 |
<?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 document --> <xsl:template match="/"> <html> <head> <title>My Book Collection</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; } </style> </head> <body> <h1>My Book Collection</h1> <table> <tr> <th>Title</th> <th>Author</th> <th>Year</th> <th>Price</th> </tr> <!-- Loop over all books --> <xsl:for-each select="library/book"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="author"/></td> <td><xsl:value-of select="year"/></td> <td>₹<xsl:value-of select="price"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet> |
Result HTML (what browser sees)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<html> <head><title>My Book Collection</title>...</head> <body> <h1>My Book Collection</h1> <table> <tr><th>Title</th><th>Author</th><th>Year</th><th>Price</th></tr> <tr><td>Atomic Habits</td><td>James Clear</td><td>2018</td><td>₹499.00</td></tr> <tr><td>Rich Dad Poor Dad</td><td>Robert Kiyosaki</td><td>1997</td><td>₹350.00</td></tr> </table> </body> </html> |
This is the classic XSLT use case: XML data → nice HTML page
4. Most Important XSLT Building Blocks
| Construct | Purpose | Most common usage example |
|---|---|---|
| <xsl:stylesheet> | Root element of stylesheet | Always present |
| <xsl:template match=”…”> | Defines a rule: when this pattern matches → do this | match=”/”, match=”book”, match=”@*│node()” (identity) |
| <xsl:apply-templates> | “Call the best matching template for children” | <xsl:apply-templates select=”book”/> |
| <xsl:for-each> | Classic loop | Loop over repeating elements |
| <xsl:value-of select=”…”/> | Output the value of an XPath expression | Print title, price, etc. |
| <xsl:if> | Conditional | Show discount if price > 1000 |
| <xsl:choose> | If-elseif-else | Different colors for different categories |
| <xsl:sort> | Sort elements | Sort books by year or title |
| <xsl:copy> | Copy current node | Very useful in identity transform |
5. More Realistic Example – Invoice with conditions & formatting
Input XML (very simplified invoice)
|
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 |
<invoice number="INV/2025/0789" date="2025-07-20"> <seller> <name>TechTrend Innovations</name> <gstin>36AAECT4567P1Z2</gstin> </seller> <items> <item> <description>Annual LMS License</description> <quantity>1</quantity> <rate>185000.00</rate> </item> <item> <description>Training Session</description> <quantity>2</quantity> <rate>45000.00</rate> </item> </items> <total currency="INR">275000.00</total> </invoice> |
XSLT with some logic
|
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 |
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" indent="yes"/> <xsl:template match="/"> <html> <head><title>Tax Invoice</title></head> <body style="font-family:Arial;"> <h1>Tax Invoice</h1> <p><strong>Invoice No:</strong> <xsl:value-of select="/invoice/@number"/></p> <p><strong>Date:</strong> <xsl:value-of select="/invoice/@date"/></p> <h3>Seller</h3> <p><xsl:value-of select="invoice/seller/name"/></p> <p>GSTIN: <xsl:value-of select="invoice/seller/gstin"/></p> <h3>Items</h3> <table border="1" cellspacing="0" cellpadding="8"> <tr style="background:#eee;"> <th>Description</th> <th>Qty</th> <th>Rate</th> <th>Amount</th> </tr> <xsl:for-each select="invoice/items/item"> <tr> <td><xsl:value-of select="description"/></td> <td><xsl:value-of select="quantity"/></td> <td><xsl:value-of select="rate"/></td> <td> <xsl:value-of select="quantity * rate"/> </td> </tr> </xsl:for-each> </table> <h3>Total: <xsl:value-of select="invoice/total/@currency"/> <xsl:value-of select="invoice/total"/></h3> </body> </html> </xsl:template> </xsl:stylesheet> |
Notice:
- XPath inside <xsl:value-of select=”…”/>
- Simple calculation: quantity * rate
- Formatting with HTML + inline CSS
6. Quick Summary – XSLT Cheat Sheet
| You want to do… | Typical XSLT pattern |
|---|---|
| Create HTML page from XML | <xsl:template match=”/”> <html> … </html> |
| Copy everything + make small changes | Identity transform + specific templates |
| Loop over repeating items | <xsl:for-each select=”items/item”> |
| Conditional content | <xsl:if test=”price > 1000″> or <xsl:choose> |
| Sort items | <xsl:for-each select=”book”> <xsl:sort select=”title”/> |
| Output plain text | <xsl:output method=”text”/> |
| Use variables | <xsl:variable name=”total” select=”sum(//price)”/> |
Would you like to go deeper into one of these areas?
- Identity transform (copy XML + make small changes – very powerful pattern)
- XSLT 2.0 / 3.0 features (grouping, higher-order functions, JSON support…)
- How to run XSLT (online tools, command line, Java, Python, browser)
- Real-world example: GST e-invoice XML → human-readable PDF/HTML
- XSLT with namespaces (very common in real enterprise XML)
- Debugging XSLT – common mistakes & how to fix them
Just tell me which direction feels most interesting or useful for you right now! 😊
