XSLT (Extensible Stylesheet Language Transformations) is a language originally designed fortransformingXML documents into other XML documents,[1] or other formats such asHTML forweb pages,plain text, orXSL Formatting Objects. These formats can be subsequently converted to formats such asPDF,PostScript, andPNG.[2] Support for JSON and plain-text transformation was added in later updates to the XSLT 1.0 specification.
As of August 2022[update], the most recent stable version of the language is XSLT 3.0, which achieved Recommendation status in June 2017.
XSLT 3.0 implementations support Java, .NET, C/C++, Python, PHP and NodeJS. An XSLT 3.0 JavaScript library can also be hosted within the web browser. Modern web browsers also include native support for XSLT 1.0.[3]
The XSLT document transformation specifies how to transform an XML document into new document (usually XML, but other formats, such as plain text are supported).[4] Typically, input documents are XML files, but anything from which the processor can build anXQuery and XPath Data Model can be used, such asrelational database tables orgeographical information systems.[1]
While XSLT was originally designed as a special-purpose language for XML transformation, the language isTuring-complete, making it theoretically capable of arbitrary computations.[5]
XSLT is influenced byfunctional languages,[6] and by text-based pattern matching languages likeSNOBOL andAWK. Its most direct predecessor isDSSSL, which did forSGML what XSLT does for XML.[7]
XSLT 1.0: XSLT was part of theWorld Wide Web Consortium (W3C)'seXtensible Stylesheet Language (XSL) development effort of 1998–1999, a project that also producedXSL-FO andXPath. Some members of the standards committee that developed XSLT, includingJames Clark, the editor, had previously worked on DSSSL. XSLT 1.0 was published as aW3C recommendation in November 1999.[8] Despite its age, XSLT 1.0[9] is still widely used (as of 2018[update]), since later versions are not supported natively inweb browsers or for environments likeLAMP.
XSLT 2.0: after an abortive attempt to create a version 1.1 in 2001,[10] the XSL working group joined forces with theXQuery working group to createXPath 2.0,[11] with a richer data model and type system based onXML Schema. Building on this is XSLT 2.0,[12] developed under the editorship ofMichael Kay, which reached recommendation status in January 2007.[13] The most important innovations in XSLT 2.0 include:
Functions and operators for manipulating dates, times, and durations
Multiple output documents
Grouping (creating hierarchic structure from flat input sequences)
A richer type system and stronger type checking
XSLT 3.0: became a W3C Recommendation on 8 June 2017. The main new features are:[14]
Streaming transformations: in previous versions the entire input document had to be read into memory before it could be processed,[15] and output could not be written until processing had finished. XSLT 3.0 allowsXML streaming which is useful for processing documents too large to fit in memory or when transformations are chained inXML 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 (higher-order) functions.
Diagram of the basic elements and process flow of eXtensible Stylesheet Language Transformations.
The XSLT processor takes one or more XML source documents, plus one or more XSLT stylesheets, and processes them to produce one or multiple output documents.[16][17] In contrast to widely implementedimperative programming languages likeC, XSLT isdeclarative.[18] The basic processing paradigm is pattern matching.[19] Rather than listing an imperative sequence of actions to perform in a stateful environment, template rules only define how to handle a node matching a particular XPath-like pattern, if the processor should happen to encounter one, and the contents of the templates effectively comprisefunctionalexpressions that directly represent their evaluated form: the result tree, which is the basis of the processor's output.
A typical processor behaves as follows. First, assuming a stylesheet has already been read and prepared, the processor builds a sourcetree 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.
XSLT usesXPath to identify subsets of the source document tree and perform calculations. XPath also provides a range offunctions, 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.
XSLT functionalities overlap with those ofXQuery, 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 withinW3C, working together to ensure a common approach where appropriate. They share the same data model, type system, and function library, and both includeXPath 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, onthe web (as aweb template language), or on paper. XQuery was primarily conceived as adatabase query language in the tradition ofSQL.
Because the two languages originate in different communities, XSLT is stronger in its handlingof narrative documents with more flexible structure, while XQuery is stronger in its data handling, for example when performing relational joins.[20]
The<output> element can optionally take the attributemedia-type, which allows one to set themedia type (or MIME type) for the resulting output, for example:<xsl:output output="xml" media-type="application/xml"/>. The XSLT 1.0 recommendation recommends the more general attribute typestext/xml andapplication/xml since for a long time there was no registered media type for XSLT. During this timetext/xsl became the de facto standard. In XSLT 1.0 it was not specified how themedia-type values should be used.
With the release of the XSLT 2.0, the W3C recommended in 2007 the registration of the MIME media typeapplication/xslt+xml[21] and it was later registered with theInternet Assigned Numbers Authority.[22]
Pre-1.0 working drafts of XSLT usedtext/xsl in their embedding examples, and this type was implemented and continued to be promoted by Microsoft in Internet Explorer[23] and MSXML circa 2012. It is also widely recognized in thexml-stylesheet processing instruction by other browsers. In practice, therefore, users wanting to control transformation in the browser using this processing instruction were obliged to use this unregistered media type.[24]
This XHTML generates the output below when rendered in a web browser.
Rendered XHTML generated from an XML input file and an XSLT transformation.
In order for a web browser to be able 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:[25]
In this example,text/xsl is technically incorrect according to the W3C specifications[25] (which say the type should beapplication/xslt+xml), but it is the only media type that is widely supported across browsers as of 2009, and the situation is unchanged in 2021.
RaptorXML fromAltova 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 theDatapower brand.
libxslt is afreelibrary released under theMIT License that can be reused in commercial applications. It is based onlibxml and implemented inC for speed and portability. It supports XSLT 1.0 andEXSLT extensions.[26]
TheWebKit andBlink layout engines, used for example in theSafari andChrome web browsers respectively, uses the libxslt library to do XSL transformations.[30]
Microsoft provides two XSLT processors (both XSLT 1.0 only). The earlier processorMSXML provides COM interfaces; from MSXML 4.0 it also includes the command line utilitymsxsl.exe.[38] The .NET runtime includes a separate built-in XSLT processor in itsSystem.Xml.Xsl library.
Saxon is an XSLT 3.0 and XQuery 3.1 processor withopen-source andproprietary versions for stand-alone operation and forJava,JavaScript and .NET. A separate product Saxon-JS[39] offers XSLT 3.0 processing onNode.js and in the browser.
Xalan is an open source XSLT 1.0 processor from theApache 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 (only).[40] 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.[41]
Most early XSLT processors were interpreters. More recently, code generation is increasingly common, using portable intermediate languages (such asJava bytecode or .NETCommon Intermediate Language) as the target. However, even the interpretive products generally offer separate analysis and execution phases, allowing an optimized expression tree to be created in memory and reused to perform multiple transformations. This gives substantial performance benefits in online publishing applications, where the same transformation is applied many times per second to different source documents.[42] This separation is reflected in the design of XSLT processing APIs (such asJAXP).
Early XSLT processors had very few optimizations. Stylesheet documents were read intoDocument 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 (e.g., to move calculations out of loops), and lazy pipelined evaluation to reduce thememory footprint of intermediate results (and allow "early exit" when the processor can evaluate an expression such asfollowing-sibling::*[1] without a complete evaluation of all subexpressions). Many processors also use tree representations that are significantly more efficient (in both space and time)[43] than general-purpose DOM implementations.
In June 2014, Debbie Lockett andMichael Kay introduced an open-source benchmarking framework for XSLT processors called XT-Speedo.[44]
XSL formatting objects orXSL-FO – An XML-based language for documents, usually generated by transforming source documents with XSLT, consisting of objects used to create formatted output
Identity transform – a starting point for filter chains that add or remove data elements from XML trees in a transformation pipeline
Apache Cocoon – aJava-based framework for processing data with XSLT and other transformers.
EXSLT is a widespread community initiative to provide extensions to XSLT.
FXSL is a library implementing support forHigher-order functions in XSLT. FXSL is written in XSLT itself.
The XSLT Standard Library xsltsl, provides the XSLT developer with a set of XSLT templates for commonly used functions. These are implemented purely in XSLT, that is they do not use any extensions. xsltsl is a SourceForge project.
Kernow A GUI for Saxon that provides a point and click interface for running transforms.