What is an XSLT tester?
XSLT is a language for turning one XML document into another document. You write a stylesheet, which is itself an XML file made of templates, and a processor applies it to your data. The output can be HTML for a page, new XML for another system, or plain text such as CSV. It is still used in publishing pipelines, in feeds, in invoice and SOAP integrations and in older enterprise systems that nobody dares to replace.
This tester runs an XSLT stylesheet on an XML document and shows the result. You paste the XML in one field and the stylesheet in another. The page applies the stylesheet while you type, so you can fix a template and see the effect right away. The output can be HTML, XML or text, depending on the xsl:output element in your stylesheet.
The language is XSLT 1.0, together with the XPath 1.0 expressions it uses for select and test. XSLT 2.0 and 3.0 features, such as xsl:for-each-group, xsl:function, sequences and regular-expression functions, are not available. The transformation is done by xslt-processor, a JavaScript library, not by libxslt or Saxon, so a stylesheet that relies on an extension function of another processor can behave differently. The examples below were compared with the output of xsltproc. Both documents stay in your browser and are not uploaded.
How to test a stylesheet
- Paste the XML into the XML field. The "Example" button loads a three-book catalog and a stylesheet that turns it into a table.
- Paste the stylesheet into the XSLT stylesheet field. It has to start with
<xsl:stylesheet>or<xsl:transform>. - Read the transformed output on the right. Copy it or download it as a file.
- Change a template, a
selector a sort, and the output updates. - If something is wrong, read the message. It says whether the XML or the stylesheet is the problem, with the line and column when the parser knows them.
Examples
The stylesheet from the Example button
The XML is a catalog of three books. The stylesheet matches /catalog, loops over the book elements, sorts them by price as numbers and writes one table row each. Whitespace aside, the output is:
<table>
<tr><td>Sayings of the Century</td><td>Nigel Rees</td><td>8.95</td></tr>
<tr><td>Moby Dick</td><td>Herman Melville</td><td>8.99</td></tr>
<tr><td>Sword of Honour</td><td>Evelyn Waugh</td><td>12.99</td></tr>
</table>The books are ordered by price, not by their place in the file. Remove the xsl:sort line and they come back in document order.
Text output with a condition
With the same catalog, this stylesheet prints one word and one price per book:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output method="text"/><xsl:template match="/"><xsl:for-each select="//price"><xsl:choose><xsl:when test=". < 10">cheap</xsl:when><xsl:otherwise>dear</xsl:otherwise></xsl:choose>:<xsl:value-of select="."/>;</xsl:for-each></xsl:template></xsl:stylesheet>The stylesheet is written on one line, so the output has no stray spaces or line breaks:
cheap:8.95;dear:12.99;cheap:8.99;Note the < in the test: inside XML, the < sign has to be escaped.
A stylesheet that is refused
A file whose root is not a stylesheet, such as <a/>, gets this message: the stylesheet must start with <xsl:stylesheet> or <xsl:transform>. A stylesheet cut off after <xsl:stylesheet> is reported as not well-formed XML, with the stylesheet named as the culprit.
Common mistakes
- No output at all. The stylesheet ran but no template matched the root. Add
<xsl:template match="/">, or a template for the root element, such asmatch="/catalog". - Wrong namespace on
xsl. The prefix must be bound tohttp://www.w3.org/1999/XSL/Transform. With another address the elements are just literal output. - Unescaped
<or&. In an attribute or a text, write<and&. An unescaped character makes the stylesheet not well-formed. - Sorting numbers as text. Without
data-type="number", the value12.99sorts before8.95because it is compared as text. - Blaming the wrong file. The error says whether the XML or the stylesheet is broken. XML formatter helps find a missing closing tag.
- Using XSLT 2.0. A
version="2.0"stylesheet withxsl:functionorxsl:for-each-groupis not supported by an XSLT 1.0 processor. - Expecting the HTML to be rendered. The page shows the output as text. To see the HTML on a page, use another viewer.
Frequently asked questions
Which XSLT version does it support?
XSLT 1.0 with XPath 1.0 expressions. Version 2.0 and 3.0 features are not available.
Why is my output empty?
Usually no template matches the root of your document. The page reminds you of this. Check the match pattern of your first template.
Can I test only the XPath part first?
Yes. Try the select expressions in the XPath tester, then paste them into the stylesheet. To turn the result into JSON afterwards, see XML to JSON.
Is my XML uploaded for processing?
No. The transformation runs in your browser with a JavaScript library.