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.
<?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¶
- Your first transformation — the smallest complete stylesheet.
- Templates — splitting rules per element with
apply-templates. - Named templates and parameters — reusable routines you call by name.
- Variables — binding values and node-sets with
xsl:variable. - Loops and output —
for-eachandvalue-of. - Conditionals —
if, andchoose/when/otherwise. - XPath predicates — filtering node selections with
[…]. - String functions —
concat,substring,translate,normalize-space, … - Producing XML output —
xsl:output, namespaces,xsl:copy, the identity transform. - Sorting — ordering output with
xsl:sort. - Number formatting —
format-numberandxsl:decimal-format. - Whitespace and xsl:text — controlling the spaces and newlines in your output.
- Template modes — processing the same nodes several ways.
- Reusing stylesheets —
xsl:includeandxsl:import. - External documents — lookups with
document(). - Keys and indexed lookup —
xsl:keyandkey(), 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.