XSLT is a language for transforming XML documents into other XML documents, or other formats such as HTML for web pages, plain text or XSL Formatting Objects, which may subsequently be converted to other formats, such as PDF, PostScript and PNG. XSLT 1.0 is widely supported in modern web browsers.
The original document is not changed; rather, a new document is created based on the content of an existing one. Typically, input documents are XML files, but anything from which the processor can build an XQuery and XPath Data Model can be used, such as relational database tables or geographical information systems.
Although XSLT is designed as a special-purpose language for XML transformation, the language is Turing-complete, making it theoretically capable of arbitrary computations.
HistoryXSLT is influenced by functional languages, and by text-based pattern matching languages like SNOBOL and AWK. Its most direct predecessor is DSSSL, which did for SGML what XSLT does for XML.
- XSLT 1.0: XSLT was part of the World Wide Web Consortium 's eXtensible Stylesheet Language development effort of 1998–1999, a project that also produced XSL-FO and XPath. Some members of the standards committee that developed XSLT, including James Clark, the editor, had previously worked on DSSSL. XSLT 1.0 was published as a W3C recommendation in November 1999. Despite its age, XSLT 1.0 is still widely used, since later versions are not supported natively in web browsers or for environments like LAMP.
- XSLT 2.0: after an abortive attempt to create a version 1.1 in 2001, the XSL working group joined forces with the XQuery working group to create XPath 2.0, with a richer data model and type system based on XML Schema. Building on this is XSLT 2.0, developed under the editorship of Michael Kay, which reached recommendation status in January 2007. The most important innovations in XSLT 2.0 include:
- * String manipulation using regular expressions
- * Functions and operators for manipulating dates, times, and durations
- * Multiple output documents
- * Grouping
- * A richer type system and stronger type checking
- XSLT 3.0: became a W3C Recommendation on 8 June 2017. The main new features are:
- * Streaming transformations: in previous versions the entire input document had to be read into memory before it could be processed, and output could not be written until processing had finished. XSLT 3.0 allows XML streaming which is useful for processing documents too large to fit in memory or when transformations are chained in XML Pipelines.
- * Packages, to improve the modularity of large stylesheets.
- * Improved handling of dynamic errors with, for example, an xsl:try instruction.
- * Support for maps and arrays, enabling XSLT to handle JSON as well as XML.
- * Functions can now be arguments to other functions.
Design and processing model
A typical processor behaves as follows. First, assuming a stylesheet has already been read and prepared, the processor builds a source tree from the input XML document. It then processes the source tree's root node, finds the best-matching template for that node in the stylesheet, and evaluates the template's contents. Instructions in each template generally direct the processor to either create nodes in the result tree, or to process more nodes in the source tree in the same way as the root node. Finally the result tree is serialized as XML or HTML text.
XPathXSLT uses XPath to identify subsets of the source document tree and perform calculations. XPath also provides a range of functions, which XSLT itself further augments.
XSLT 1.0 uses XPath 1.0, while XSLT 2.0 uses XPath 2.0. XSLT 3.0 will work with either XPath 3.0 or 3.1. In the case of 1.0 and 2.0, the XSLT and XPath specifications were published on the same date. With 3.0, however, they were no longer synchronized; XPath 3.0 became a Recommendation in April 2014, followed by XPath 3.1 in February 2017; XSLT 3.0 followed in June 2017.
XQuery comparedXSLT functionalities overlap with those of XQuery, which was initially conceived as a query language for large collections of XML documents.
The XSLT 2.0 and XQuery 1.0 standards were developed by separate working groups within W3C, working together to ensure a common approach where appropriate. They share the same data model, type system, and function library, and both include XPath 2.0 as a sublanguage.
The two languages, however, are rooted in different traditions and serve the needs of different communities. XSLT was primarily conceived as a stylesheet language whose primary goal was to render XML for the human reader on screen, on the web, or on paper. XQuery was primarily conceived as a database query language in the tradition of SQL.
Because the two languages originate in different communities, XSLT is stronger in its handling
of narrative documents with more flexible structure, while XQuery is stronger in its data handling, for example when performing relational joins.
<output>element can optionally take the attribute
media-type, which allows one to set the media type for the resulting output, for example:
. The XSLT 1.0 recommendation recommends the more general attribute types
application/xmlsince for a long time there was no registered media type for XSLT. During this time
text/xslbecame the de facto standard. In XSLT 1.0 it was not specified how the
media-typevalues should be used.
With the release of the XSLT 2.0, the W3C recommended the registration of the MIME media type
application/xslt+xmland it was later registered with the Internet Assigned Numbers Authority.
Pre-1.0 working drafts of XSLT used
text/xslin their embedding examples, and this type was implemented and continues to be promoted by Microsoft in Internet Explorer and MSXML. It is also widely recognized in the
xml-stylesheetprocessing instruction by other browsers. In practice, therefore, users wanting to control transformation in the browser using this processing instruction are obliged to use this unregistered media type.
ExamplesThese examples use the following incoming XML document
Example 1 (transforming XML to XML)This XSLT stylesheet provides templates to transform the XML document:
Its evaluation results in a new XML document, having another structure:
Example 2 (transforming XML to XHTML)Processing the following example XSLT file
with the XML input file shown above results in the following XHTML :
- Ismincius, Morka
- Smith, John
This XHTML generates the output below when rendered in a web browser.
In order for a web browser to be able automatically to apply an XSL transformation to an XML document on display, an XML stylesheet processing instruction can be inserted into XML. So, for example, if the stylesheet in Example 2 above were available as "example2.xsl", the following instruction could be added to the original incoming XML:
In this example,
text/xslis technically incorrect according to the W3C specifications, but it is the only media type that is widely supported across browsers as of 2009.
- RaptorXML from Altova is an XSLT 3.0 processor available in the XMLSpy development toolkit and as a free-standing server implementation, invoked using a REST interface.
- IBM offers XSLT processing embedded in a special-purpose hardware appliance under the Datapower brand.
- libxslt 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 for speed and portability. It supports XSLT 1.0 and EXSLT extensions.
- *It can be used at the command line via xsltproc which is included in macOS and many Linux distributions, and can be used on Windows via Cygwin.
- *The WebKit and Blink layout engines, used for example in the Safari and Chrome web browsers respectively, uses the libxslt library to do XSL transformations.
- *Bindings exist for Python, Perl, Ruby, PHP, Common Lisp, Tcl, and C++.
- Microsoft provides two XSLT processors. The earlier processor MSXML provides COM interfaces; from MSXML 4.0 it also includes the command line utility
msxsl.exe. The.NET runtime includes a separate built-in XSLT processor in its
- Xalan is an open source XSLT 1.0 processor from the Apache Software Foundation available for Java and C++. A variant of the Xalan processor is included as the default XSLT processor in the standard Java distribution from Oracle.
- Web browsers: Safari, Chrome, Firefox, Opera and Internet Explorer all support XSLT 1.0. 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 on files from local filesystem because of its security model.
Early XSLT processors had very few optimizations. Stylesheet documents were read into Document Object Models and the processor would act on them directly. XPath engines were also not optimized. Increasingly, however, XSLT processors use optimization techniques found in functional programming languages and database query languages, such as static rewriting of an expression tree, and lazy pipelined evaluation to reduce the memory footprint of intermediate results. Many processors also use tree representations that are significantly more efficient than general-purpose DOM implementations.
In June 2014, Debbie Lockett and Michael Kay introduced an open-source benchmarking framework for XSLT processors called XT-Speedo.