Skip to content
Buy me a coffee
Converters

XSL Transformer — XSLT

Transform an XML document using an XSL stylesheet (XSLT 1.0). Fully supports XML namespaces and lets you choose the indentation of the result.

0 characters · 0 lines
Option 2: Or upload your XML file
0 characters · 0 lines
Option 2: Or upload your XSL file
Ctrl + Enter

XSLT explained

What is XSLT?

XSLT (Extensible Stylesheet Language Transformations) is a declarative language for transforming XML documents into other documents: a different XML vocabulary, HTML pages, plain text, CSV… A stylesheet is a set of templates; each template matches nodes of the source document with an XPath pattern and describes the output to produce for them. The processor walks the source tree, applies the matching templates and builds the result tree.

This tool runs the transformation entirely in your browser with its built-in XSLT engine (XSLTProcessor). Browsers implement XSLT 1.0 and XPath 1.0 only: stylesheets written for XSLT 2.0 or 3.0 (e.g. using xsl:for-each-group, regular expressions, xs:date functions or xsl:result-document) will fail or produce an empty result. The method attribute of xsl:output (xml, html or text) is honored when serializing the result.

Example

The XML file (a CD catalog, with two namespaces):

<?xml version="1.0" encoding="UTF-8"?>
<catalog xmlns:foo="http://www.foo.org/" xmlns:bar="http://www.bar.org">
   <foo:cd>
      <title>Empire Burlesque</title>
      <artist>Bob Dylan</artist>
      <price>10.90</price>
   </foo:cd>
   <bar:cd>
      <title>Hide your heart</title>
      <artist>Bonnie Tyler</artist>
      <price>9.90</price>
   </bar:cd>
</catalog>

The XSL file. Note the namespace declarations on the xsl:stylesheet element, which allow the XPath expressions to select foo:cd and bar:cd:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:foo="http://www.foo.org/" xmlns:bar="http://www.bar.org">
   <xsl:output method="html" indent="yes"/>
   <xsl:template match="/">
      <html>
         <body>
            <h2>My CD Collection</h2>
            <table border="1">
               <tr><th>Title</th><th>Artist</th><th>Price</th></tr>
               <xsl:for-each select="catalog/foo:cd | catalog/bar:cd">
                  <tr>
                     <td><xsl:value-of select="title"/></td>
                     <td><xsl:value-of select="artist"/></td>
                     <td><xsl:value-of select="format-number(price, '0.00')"/></td>
                  </tr>
               </xsl:for-each>
            </table>
         </body>
      </html>
   </xsl:template>
</xsl:stylesheet>

The transformed document:

<html>
   <body>
      <h2>My CD Collection</h2>
      <table border="1">
         <tr><th>Title</th><th>Artist</th><th>Price</th></tr>
         <tr><td>Empire Burlesque</td><td>Bob Dylan</td><td>10.90</td></tr>
         <tr><td>Hide your heart</td><td>Bonnie Tyler</td><td>9.90</td></tr>
      </table>
   </body>
</html>

The most useful XSLT 1.0 instructions

InstructionPurpose
<xsl:template match="pattern">Defines the output for the nodes matching the pattern. match="/" is the document root.
<xsl:apply-templates select="…"/>Processes the selected nodes with their matching templates (recursive, "push" style).
<xsl:for-each select="…">Loops over the selected nodes ("pull" style).
<xsl:value-of select="…"/>Outputs the string value of an expression.
<xsl:if test="…">, <xsl:choose> / <xsl:when> / <xsl:otherwise>Conditional output.
<xsl:sort select="…" order="descending"/>Sorts the nodes of a for-each or apply-templates.
<xsl:element>, <xsl:attribute>, <xsl:text>Creates elements, attributes and literal text in the result.
<xsl:copy-of select="…"/>, <xsl:copy>Copies nodes from the source (deep / shallow). The identity transform is built on xsl:copy.
<xsl:variable>, <xsl:param>, <xsl:call-template>Variables, parameters and named templates.
<xsl:output method="xml|html|text" indent="yes"/>Controls how the result is serialized.

Tips and troubleshooting

  • Namespaces: every prefix used in an XPath expression must be declared on the root element of the stylesheet with the same URI as in the source document (the prefix itself can differ). Elements in a default namespace (xmlns="…") cannot be selected without a prefix in XPath 1.0: declare a prefix for that URI in the stylesheet and use it in your expressions.
  • Unwanted xmlns:… attributes in the result? Namespace declarations of the stylesheet are copied onto the literal result elements. Add exclude-result-prefixes="foo bar" to the xsl:stylesheet element to drop them.
  • Empty result? Check the match patterns and select expressions: XPath is case-sensitive and namespace-aware. A template that matches nothing produces nothing, silently.
  • Text output: use <xsl:output method="text"/> and <xsl:text> to control whitespace precisely (for CSV or code generation).
  • Document functions: document() and external entities are blocked by the browser for security reasons; inline all the data in the XML source.
  • Formatting: the result is re-indented with the chosen indentation when it is well-formed XML. HTML results that are not well-formed (unclosed <br>, <p>…) are formatted with an HTML beautifier instead.