XML XSLT
XSLT (eXtensible Stylesheet Language Transformations) is a language for transforming one XML document into another document — often HTML, but also different XML, plain text or CSV. An XSLT stylesheet is itself written in XML. It uses XPath to find nodes and templates to say what to output for them.
Note: Browsers are deprecating their built-in XSLT support. Chrome has announced its removal and other browsers plan to follow. XSLT itself is alive and well on servers and in publishing tools (Saxon, libxslt, .NET, Java), so the concepts below remain useful — but for new browser code, transform XML with JavaScript instead. The demo on this page checks whether your browser still supports
XSLTProcessor.
The idea
XML data ──┐
├──► XSLT processor ──► result (HTML, XML, text…)
XSLT sheet ─┘The data stays clean, and different stylesheets can turn the same data into a web page, a printable report or a feed for another system.
A first stylesheet
Source data:
<films>
<film year="1984"><title>Nausicaä</title><director>Hayao Miyazaki</director></film>
<film year="1988"><title>Akira</title><director>Katsuhiro Otomo</director></film>
<film year="1995"><title>Ghost in the Shell</title><director>Mamoru Oshii</director></film>
</films>A stylesheet that turns it into an HTML table:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<table border="1">
<tr><th>Title</th><th>Year</th><th>Director</th></tr>
<xsl:for-each select="films/film">
<xsl:sort select="@year" data-type="number" order="descending"/>
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="@year"/></td>
<td><xsl:value-of select="director"/></td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>Running the second block confirms that a stylesheet is ordinary, well-formed XML. Elements in the xsl: namespace are instructions; everything else (<table>, <tr> …) is copied to the output.
Core instructions
| Instruction | What it does |
|---|---|
<xsl:template match="pattern"> |
A rule: output this when a node matches the pattern |
<xsl:apply-templates select="…"/> |
Process the selected nodes with matching templates |
<xsl:value-of select="…"/> |
Output the text value of an XPath expression |
<xsl:for-each select="…"> |
Loop over a node set |
<xsl:sort select="…"/> |
Sort inside for-each or apply-templates |
<xsl:if test="…"> |
Output only if the condition is true |
<xsl:choose> / <xsl:when> / <xsl:otherwise> |
Multi-way branch |
<xsl:attribute name="…"> |
Add a computed attribute to the output element |
{expression} inside an attribute |
Attribute value template, e.g. <a href="{@url}"> |
Running XSLT in the browser
XSLTProcessor imports a stylesheet document and transforms a source document. The demo falls back gracefully if your browser no longer has it:
<!DOCTYPE html>
<html>
<body>
<div id="out"></div>
<script>
const data = `<films>
<film year="1984"><title>Nausicaä</title><director>Hayao Miyazaki</director></film>
<film year="1988"><title>Akira</title><director>Katsuhiro Otomo</director></film>
<film year="1995"><title>Ghost in the Shell</title><director>Mamoru Oshii</director></film>
</films>`;
const sheet = `<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<ul>
<xsl:for-each select="films/film">
<xsl:sort select="@year" data-type="number" order="descending"/>
<li>
<b><xsl:value-of select="title"/></b> (<xsl:value-of select="@year"/>)
<xsl:if test="@year < 1990"> — a 1980s classic</xsl:if>
</li>
</xsl:for-each>
</ul>
</xsl:template>
</xsl:stylesheet>`;
const parse = (s) => new DOMParser().parseFromString(s, 'application/xml');
if (typeof XSLTProcessor === 'undefined') {
console.log('This browser has removed XSLTProcessor — use a server-side processor or JavaScript instead.');
} else {
const proc = new XSLTProcessor();
proc.importStylesheet(parse(sheet));
const fragment = proc.transformToFragment(parse(data), document);
document.getElementById('out').append(fragment);
console.log('Transformed', document.querySelectorAll('#out li').length, 'films with XSLT');
}
</script>
</body>
</html>Note < inside test="…": the stylesheet is XML, so < must be escaped even in an XPath expression.
Template rules and apply-templates
for-each is easy to read, but idiomatic XSLT uses template rules: you write one template per kind of node and let the processor walk the tree.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/films">
<section><xsl:apply-templates select="film"/></section>
</xsl:template>
<xsl:template match="film">
<article>
<h2><xsl:value-of select="title"/></h2>
<p>Directed by <xsl:value-of select="director"/></p>
</article>
</xsl:template>
</xsl:stylesheet>This style shines with document formats where elements can appear in many places — each rule handles its element wherever it shows up.
Linking a stylesheet to an XML file
Historically a processing instruction at the top of the XML file asked the browser to transform it on opening:
<?xml-stylesheet type="text/xsl" href="films.xsl"?>Because browser XSLT is going away, do the transformation on the server or at build time instead, and serve the resulting HTML.
XSLT versions
| Version | Year | Highlights |
|---|---|---|
| 1.0 | 1999 | What browsers implemented; XPath 1.0 |
| 2.0 | 2007 | XPath 2.0, grouping, regular expressions, multiple outputs |
| 3.0 | 2017 | Streaming, maps and arrays, JSON input/output |
Test yourself
What is an XSLT stylesheet written in?
- CSS
- JavaScript
- XML
- JSON
Which instruction outputs the text of the title child?
<xsl:text select="title"/><xsl:value-of select="title"/><xsl:print title/><xsl:copy select="title"/>
What is the recommended way to transform XML for a web page today?
- Rely on the browser's built-in XSLT
- Transform on the server/at build time, or use JavaScript in the page
- Use CSS
transform - Rename the file to .html