is the best known XML transformation language. The XSLT 1.0 W3C recommendation was published in 1999 together with XPath 1.0, and it has been widely implemented since then. XSLT 2.0 has become a W3C recommendation since January 2007 and implementations of the specification like Saxon 8 are already available.
is a full functional language, despite having "query" in the name. It is a de facto standard used by Microsoft, Oracle, DB2, Mark Logic, etc., is the foundation for the XRX web programming model, and has a W3C recommendation for versions 1.0. XQuery is not written in XML itself like XSLT is, so its syntax is much lighter. The language is based on XPath 2.0. XQuery programs cannot have side-effects, just like XSLT and provides almost the same capabilities (for instance: declaring variables and functions, iterating over sequences, using W3C schema types), even though the program syntax are quite different. XQuery is logic driven, using FOR, WHERE and function composition (e.g. fn:concat("<html>", generate-body(), "</html>")). In contrast, XSLT is data-driven (push processing model) where certain conditions of the input document trigger the execution of templates rather than the code executing in the order in which it is written.
is an XML Pipeline language. The XProc 1.0 W3C Recommendation was published in May 2010.
is a Microsoft standard for performing simple transforms on XML documents. Primarily for creating IIS Web.config files (Config Transforms), other implementations allow it to be used for generic config files as build time (Slow Cheetah) or from the command line (CTT).
is inspired by XSLT but has been designed to allow a one-pass transformation process that never prevents streaming. Implementations are available in Java (Joost) and Perl (XML::STX).
is an imperative scripting language inspired by Perl that uses the XML syntax. XML Script supports XPath and its proprietary DSLPath for selecting nodes from the input tree.
is a functional XML transformation tool, implemented in Standard ML.
is a typed language with a lightweight syntax, compared to XSLT. It is written in ML.
extends XDuce to a general-purpose functional programming language, see CDuce homepage.
is a Java-based system for programming XML transformations. Notable features include XML templates as immutable values and a static analysis to ensure type safety using XML Schema types (XACT home page).
is a functional language X-Fun for defining transformations between XML data trees, while providing shredding instructions. X-Fun can be understood as an extension of Frisch’s XStream language with output shredding, while pattern matching is replaced by tree navigation with XPath expressions. ([1])
is a simple functional transformation language for XML documents based on CAML. XML transformations written in XStream are evaluated in streaming: when possible, parts of the output are computed and produced while the input document is still being parsed. Some transformations can thus be applied to huge XML documents which would not even fit in memory. The XStream compiler is distributed under the terms of the CeCILL free software license.
applies methods from XDuce to C#, see Xtatic homepage. HaXml::is a library and collection of tools to write XML transformations in Haskell. Its approach is very consistent and powerful. Also see this paper about HaXml published in 1999 and this IBM developerWorks article. See also the more recent HXML and Haskell XML Toolbox (HXT), which is based on the ideas of HaXml and HXML but takes a more general approach to XML processing.
is described in a 1999 paper by Erik Meijer and Mark Shields. No implementation is available. See XMLambda home page.
is an XML processing language first implemented by Kristofer Rose. Its approach is to add actions to an XML DTD specifying processing instructions for any subset of the DTD’s rules.
is a general-purpose functional and object-oriented language with specific support for XML transformation in the form of XML pattern matching, literals, and expressions, along with standard XML libraries.[1]
is a .NET 3.5 syntax and programming API available in C#, VB and some other .NET languages. LINQ is primarily designed as a query language, but it also supports XML transforms.
XML to XSL-Formatting Object transformation
xsl:stylesheet
or
xsl:transformation
(which are synonyms) where xsl:
is a prefix for the XSL namespace.
xsl:template
elements.
(Wikipedia, XSLT)
<?xml version="1.0" encoding="UTF-8"?> <persons> <person username="JS1"> <name>John</name> <family-name>Smith</family-name> </person> <person username="MI1"> <name>Morka</name> <family-name>Ismincius</family-name> </person> </persons>
(Wikipedia, XSLT)
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="xml" indent="yes"/> <xsl:template match="/persons"> <root> <xsl:apply-templates select="person"/> </root> </xsl:template> <xsl:template match="person"> <name username="{@username}"> <xsl:value-of select="name" /> </name> </xsl:template> </xsl:stylesheet>
(Wikipedia, XSLT)
<?xml version="1.0" encoding="UTF-8"?> <root> <name username="JS1">John</name> <name username="MI1">Morka</name> </root>
is a free library released under the MIT License that can be reused in commercial applications. It is based on libxml and implemented in C. It can be used at the command line via xsltproc
which is included in OS X and many Linux distributions.
used for example in the Safari and Chrome web browsers respectively, uses the libxslt library to do XSL transformations.
an XSLT (2.0 and partial 3.0) and XQuery 3.0 processor with open-source and proprietary commercial versions for stand-alone operation and for Java, JavaScript and .NET. The open-source version does not support XSLT 3.0.
an open source XSLT 1.0 processor from the Apache Software Foundation available stand-alone and for Java and C++. Integrated into Java SE.
Safari, Chrome, Firefox, Opera and Internet Explorer all support XSLT 1.0. None supports XSLT 2.0 natively, although the third party products like Saxon-CE (Saxon-Client Edition) and Frameless can provide this functionality. Browsers can perform on-the-fly transformations of XML files and display the transformation output in the browser window. This is done either by embedding the XSL in the XML document or by referencing a file containing XSL instructions from the XML document. The latter may not work with Chrome because of its security model.
XMLStarlet is a command line XML toolkit which can be used to transform,
query, validate, and edit XML documents and files using simple set of shell
commands in similar way it is done for plain text files using grep
/sed
/awk
/tr
/diff
/patch
. It does not require Java. Available for Windows and Linux. Supports XSLT 1.0.
includes an XSLT 1.0 processor. From MSXML 4.0 it includes the command line utility msxsl.exe
.
an XSLT 3.0 processor doing streaming implemented in Java by Innovimax and INRIA.
commercial versions support the newest standards such as XSLT 3.0.
XSLT 1.0 is still the most used version.
xsl:stylesheet
(or xsl:transform
) is the top-level element. Occurs only once in a stylesheet document.
The attribute version
specifies which XSLT version is being used. The NS declaration xmlns:xsl
specifies the URL, which is always http://www.w3.org/1999/XSL/Transform
regardless of the XSLT version.
xsl:output
Child element of stylesheet
. It describes how data will be returned. The attribute method
designates what kind of data is returned (such as xml
, text
, html
). The attribute omit-xml-declaration
indicates if the initial <?xml
heading should be included. The attribute encoding
designates the encoding used for output.
xsl:template
Specifies processing templates “match” is when the template should be used. “name” gives the template a name which xsl:call-template can use to call this template.
xsl:stylesheet
xsl:param
parameter declarations (and their implicit value). Such parameters can then be set when calling XSLT processing, e.g. java net.sf.saxon.Transform
-o outfile.xml infile.xml style.xsl -Dparam=paramvalue
xsl:variable
similarly to parameters, it declares and initializes variables. They however cannot be set from outside. It should also be noted that XSLT (without processor-specific extension) is a pure functional language, i.e. applications of templates do not have side effects → variables (or parameters) can be assigned just once, then just read!
Template (xsl:template
) is a specification
which node should be rewriten and how (into what):
match
.
xsl:apply-templates select="<xpath expression>"
.
The template can also be explicitly named
(named template) using the name
attribute, in which case it can be called
directly / explicitly using xsl:call-template
.
xsl:import
Retrieves another XSLT file addressed by the href
(URI of the file). The templates in the linking (originating) stylesheet have priority over the imported ones.
xsl:include
Similarly, but works as a textual (verbatim) include, so no prioritization of the linking stylesheet is done.
mode
attribute at the xsl:template
element.
mode
in the xsl:apply-templates
or xsl:call-templates
.
Modes allow a parallel set of templates with the same patterns match, but used for different purposes, for example:
/
priority
attribute. The priority can be an integer, greater number means higher priority.
xsl:call-template
(possibly using xsl:with-params
)
by applying a matching template (again may be with parameters) using xsl:apply-templates
without explicit selection of nodes for further processing. Then, all child elements of the current (context) node will be selected and processed. Equivalent to xsl:apply-templates select="*"
.
when using xsl:apply-templates
with explicit selection of nodes for further processing by using the select
attribute.
Either:
xsl:text
. Inside of it, whitespaces are always maintained!
<xsl:template match="*|/"> <xsl:apply-templates/> <xsl:template>
<xsl:template match="*|/" mode="..."> <xsl:apply-templates mode="..."/> <xsl:template>
<xsl:template match="text()|@*"> <xsl:value-of select="."/> <xsl:template>
<xsl:template match="processing-instruction()|comment()" />
Generate the output of a predetermined element (with pre-known name), but with attributes with values with calculated during transformation.
Use the normal procedure - literal result element - attributes and values specified as the attribute value templates (AVT)
<link ref="a_link_href"> ... </link>
<xsl:template match="link"> <a href="#{@ref}"> ... </a> </xsl: template>
link
to a (possibly HTML) a
element, the href
attribute value is composed of #
and the value of the original ref
attribute.
<a href="#a_link_href"> ... </a>
Generate the output element whose name, attributes and content is NOT known in advance when writing the style. So it must be determined (calculated) in runtime (when transforming).
Use a template to component xsl:element
<generate element-name="elt_name"> ... </generate>
<xsl:template match="generate"> <xsl:element name="{@element-name}"> <xsl:attribute name="id">ID1</xsl:attribute> </xsl:element> </xsl:template>
Creates an element with the name elt_name
, equipping it
with the attribute id="ID1"
. Also the attribute name could be generated
if we wished so.
To influence the output based on a condition.
Use branching in the template - either
xsl:if
for single then/else branches or
xsl:choose
/ xsl:when
/ xsl:otherwise
xsl:if
<bread price="50"> ... </bread>
<xsl:template match="bread"> <p> <xsl:if test="@price > 30"> <span class="expensive">Expensive </span> </xsl:if>bread - price <xsl:value-of select="@price"/> CZK</p> </xsl:template>
Creates an element p
with a record about the bread. If the bread
was expensive, also the "Expensive" indication is produced.
xsl:choose
<bread price="12"> ... </bread> <bread price="19"> ... </bread> <bread price="30"> ... </bread>
<xsl:template match="bread"> <xsl:choose> <xsl:when test="@price > 30"> <span class="expensive">Expensive</span> </xsl:when> <xsl:when test="@price < 10"> <span class="strangely-cheap">Suspiciously cheap</span> </xsl:when> <xsl:otherwise> <span class="normal-price">Normal</span> </xsl:otherwise> </xsl:choose> bread - price <xsl:value-of select="@price"/> CZK </xsl:template>
Filters out the two extreme price level — normal prices remain for xsl:otherwise
.
<grocery> <bread price="12"> ... </bread> <bread price="19"> ... </bread> <bread price="30"> ... </bread> </grocery>
<xsl:template match="grocery"> <xsl:for-each select="bread"> <p> bread - price <xsl:value-of select="@price"/> CZK </p> </xsl:for-each> </xsl:template>
Creates series of elements p
with bread prices.
Construction xsl:for-each
typically has procedural nature,
which is generally not recommended for XSLT as it namely gives minimum
flexibility to iterate through the contents of a set of nodes — we must know its exact structures beforehand. The style is also more difficult to modify if the structure changes (eg. new or altered element names).
<xsl:template name="thistemplatename">
.
The template may contain declarations of parameters: <xsl:param name="parametername"/>
(parameter type is not specified — i.e. dynamic typing)
using <xsl:call-template name="atemplatename"/>
The call can also specify the parameters (if they were declared at the template definition): <xsl:with-param name="parametername" select="parametervalueexpression"/>
or <xsl:with-param name="parametername">_parametervalue_</xsl:with-param>
can also be specified using <xsl:param name="parametername" select="defaultvalueexpression"/>
xsl:number
element
java.text
.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/> <xsl:strip-space elements="*"/> <xsl:template match="group"> <xsl:text>Group </xsl:text> <xsl:number count="group" level="multiple"/> <xsl:text>
</xsl:text> <xsl:apply-templates/> </xsl:template> <xsl:template match="person"> <xsl:number count="group" level="multiple" format="1.1.1."/> <xsl:number count="person" level="single" format="1 "/> <xsl:value-of select="@name"/> <xsl:text>
</xsl:text> </xsl:template> </xsl:stylesheet>
<people> <group> <person name="Al Zehtooney" age="33" sex="m" smoker="no"/> <person name="Brad York" age="38" sex="m" smoker="yes"/> </group> <group> <person name="Greg Sutter" age="40" sex="m" smoker="no"/> <person name="Harry Rogers" age="37" sex="m" smoker="no"/> <group> <person name="John Quincy" age="43" sex="m" smoker="yes"/> <person name="Kent Peterson" age="31" sex="m" smoker="no"/> </group> <person name="John Frank" age="24" sex="m" smoker="no"/> </group> </people>
Group 1 1.1 Al Zehtooney 1.2 Brad York Group 2 2.1 Greg Sutter 2.2 Harry Rogers Group 2.1 2.1.1 John Quincy 2.1.2 Kent Peterson 2.3 John Frank
xsltproc
or xmlstarlet
javax.xml.transform
package)
Good for simple try-and-see:
With XSLT 2.0 support:
All recent NetBeans version allow to launch XSLT just by: