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>
&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;

  &lt;xsl:template match="/"&gt;
    &lt;html&gt;
      &lt;body&gt;
        &lt;h2&gt;Product Catalog&lt;/h2&gt;
        &lt;xsl:apply-templates select="catalog/product"/&gt;
      &lt;/body&gt;
    &lt;/html&gt;
  &lt;/xsl:template&gt;

  &lt;xsl:template match="product"&gt;
    &lt;p&gt;
      &lt;b&gt;&lt;xsl:value-of select="name"/&gt;&lt;/b&gt; —
      $&lt;xsl:value-of select="price"/&gt;
    &lt;/p&gt;
  &lt;/xsl:template&gt;

&lt;/xsl:stylesheet&gt;
</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>
&lt;xsl:template match="/catalog"&gt;
  &lt;table border="1"&gt;
    &lt;tr&gt;&lt;th&gt;Name&lt;/th&gt;&lt;th&gt;Category&lt;/th&gt;&lt;th&gt;Price&lt;/th&gt;&lt;/tr&gt;
    &lt;xsl:for-each select="product"&gt;
      &lt;tr&gt;
        &lt;td&gt;&lt;xsl:value-of select="name"/&gt;&lt;/td&gt;
        &lt;td&gt;&lt;xsl:value-of select="@category"/&gt;&lt;/td&gt;
        &lt;td&gt;&lt;xsl:value-of select="price"/&gt;&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/xsl:for-each&gt;
  &lt;/table&gt;
&lt;/xsl:template&gt;
</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>
&lt;xsl:template match="/catalog"&gt;
  &lt;xsl:apply-templates select="product"&gt;
    &lt;xsl:sort select="price" data-type="number" order="ascending"/&gt;
  &lt;/xsl:apply-templates&gt;
&lt;/xsl:template&gt;

&lt;xsl:template match="product"&gt;
  &lt;p&gt;
    &lt;xsl:choose&gt;
      &lt;xsl:when test="price &amp;gt; 100"&gt;
        &lt;span class="premium"&gt;&lt;xsl:value-of select="name"/&gt;&lt;/span&gt;
      &lt;/xsl:when&gt;
      &lt;xsl:otherwise&gt;
        &lt;xsl:value-of select="name"/&gt;
      &lt;/xsl:otherwise&gt;
    &lt;/xsl:choose&gt;
  &lt;/p&gt;
&lt;/xsl:template&gt;
</pre>

</body>
</html>
Aspectxsl:for-eachxsl:apply-templates
StyleProcedural — loop written where you need itDeclarative — dispatches to matching templates
ReusabilityLogic is embedded in one place onlySame template reused wherever matching nodes appear
Best forQuick one-off tables or listsRecursive or deeply nested document structures
Sortingxsl:sort nested directly inside the loopxsl:sort nested inside the apply-templates call
Note: Tip: Keep templates narrow and let apply-templates do the dispatching. A stylesheet built from many small, specific templates (match="product", match="product/price", …) scales far better as your schema grows than one giant template stuffed with for-each loops and choose blocks.
Note: Not every browser transforms XML client-side the same way, and many production pipelines skip the browser entirely — running the transformation ahead of time with a processor like Saxon, Xalan, or libxslt. If you're linking a stylesheet directly from XML with <?xml-stylesheet type="text/xsl" href="style.xsl"?>, test in more than one browser before relying on it for anything user-facing.

Exercise: XML XSLT

What does XSLT fundamentally do to an XML document?