XML XSLT
XSLT (Extensible Stylesheet Language Transformations) is a rule-based language that transforms XML documents into HTML, plain text, or other XML vocabularies by matching templates against the source tree.
What Is XSLT?
XSLT works by pairing an XML source document with a stylesheet full of template rules. Each rule says, in effect, 'when you find a node that looks like this, output this instead.' An XSLT processor walks the source tree, matches nodes against your templates, and writes the result to a new document — most commonly HTML for display in a browser, but it could just as easily be plain text, CSV, or a different XML vocabulary entirely. Because the transformation logic lives outside the data, the same XML file can be rendered a dozen different ways just by swapping stylesheets.
Anatomy of a Stylesheet
A Minimal XSLT Stylesheet
<!DOCTYPE html>
<html>
<body>
<pre>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Product Catalog</h2>
<xsl:apply-templates select="catalog/product"/>
</body>
</html>
</xsl:template>
<xsl:template match="product">
<p>
<b><xsl:value-of select="name"/></b> —
$<xsl:value-of select="price"/>
</p>
</xsl:template>
</xsl:stylesheet>
</pre>
</body>
</html>Every stylesheet starts with the xsl:stylesheet (or xsl:transform — the two are interchangeable) root element, declared against the XSLT namespace http://www.w3.org/1999/XSL/Transform and tagged with a version, almost always 1.0 or 2.0. Inside it, xsl:template elements each carry a match attribute holding an XPath pattern; the template matching '/' fires first, since it matches the document root, and typically builds the outer HTML skeleton before handing off to more specific templates with xsl:apply-templates.
Looping and Selecting Values
<!DOCTYPE html>
<html>
<body>
<pre>
<xsl:template match="/catalog">
<table border="1">
<tr><th>Name</th><th>Category</th><th>Price</th></tr>
<xsl:for-each select="product">
<tr>
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="@category"/></td>
<td><xsl:value-of select="price"/></td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</pre>
</body>
</html>- xsl:template — defines a rule for transforming nodes that match a given XPath pattern
- xsl:value-of — outputs the string value of an XPath expression
- xsl:for-each — iterates over a node-set without needing a separate template
- xsl:apply-templates — hands control to whichever template best matches the selected nodes
- xsl:if — a single-branch conditional with a test attribute
- xsl:choose / xsl:when / xsl:otherwise — multi-branch conditional logic
- xsl:sort — reorders nodes inside a for-each or apply-templates
- xsl:attribute — writes a computed attribute onto an output element
Conditionals and Sorting
Sorting and Branching with xsl:choose
<!DOCTYPE html>
<html>
<body>
<pre>
<xsl:template match="/catalog">
<xsl:apply-templates select="product">
<xsl:sort select="price" data-type="number" order="ascending"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="product">
<p>
<xsl:choose>
<xsl:when test="price &gt; 100">
<span class="premium"><xsl:value-of select="name"/></span>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="name"/>
</xsl:otherwise>
</xsl:choose>
</p>
</xsl:template>
</pre>
</body>
</html>Exercise: XML XSLT
What does XSLT fundamentally do to an XML document?