Skip to content

XSLT Tutorial

XSLT (Extensible Stylesheet Language Transformations) is a language for transforming XML documents into other formats — most commonly HTML for display in a browser, but also plain text or a different shape of XML.

You write a stylesheet: a set of rules that an XSLT processor applies to a source document to produce a result document.

flowchart LR
  X[Source XML] --> P[XSLT processor]
  S[Stylesheet .xsl] --> P
  P --> R[Result: HTML / text / XML]

XSLT versions

  • 1.0 is universally supported — every browser and processor handles it.
  • 2.0 / 3.0 add grouping, regular expressions, and a much richer function library, but need a processor like Saxon.

This tutorial is 1.0 unless noted, so the examples run anywhere.

The running example

Every page in this tutorial transforms the same little CD catalog, so you can focus on the XSLT rather than re-learning the data each time.

catalog.xml
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
  <cd>
    <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
    <price>10.90</price>
  </cd>
  <cd>
    <title>Hide your heart</title>
    <artist>Bonnie Tyler</artist>
    <price>9.90</price>
  </cd>
  <cd>
    <title>Greatest Hits</title>
    <artist>Dolly Parton</artist>
    <price>9.90</price>
  </cd>
</catalog>

Where to go next

  1. Your first transformation — the smallest complete stylesheet.
  2. Templates — splitting rules per element with apply-templates.
  3. Named templates and parameters — reusable routines you call by name.
  4. Variables — binding values and node-sets with xsl:variable.
  5. Loops and outputfor-each and value-of.
  6. Conditionalsif, and choose/when/otherwise.
  7. XPath predicates — filtering node selections with […].
  8. String functionsconcat, substring, translate, normalize-space, …
  9. Producing XML outputxsl:output, namespaces, xsl:copy, the identity transform.
  10. Sorting — ordering output with xsl:sort.
  11. Number formattingformat-number and xsl:decimal-format.
  12. Whitespace and xsl:text — controlling the spaces and newlines in your output.
  13. Template modes — processing the same nodes several ways.
  14. Reusing stylesheetsxsl:include and xsl:import.
  15. External documents — lookups with document().
  16. Keys and indexed lookupxsl:key and key(), the scalable join.

Those sixteen pages are XSLT 1.0 — they run in any processor and any browser. When you are comfortable with them, the Modern XSLT (2.0 & 3.0) section picks up where they leave off: sequences and a real type system, xsl:function, grouping, regular expressions, JSON, streaming, packages, and the rest of what a current processor like Saxon adds. Start at Moving to XSLT 2.0 and 3.0.