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:

  1. Take an XML document as input
  2. Transform / rearrange / filter / calculate / rename / decorate it
  3. 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)

XML

XSLT stylesheet (books-to-html.xsl)

XML

Result HTML (what browser sees)

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)

XML

XSLT with some logic

XML

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! 😊

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *