XSLT explained

XSLT
File Ext:.xslt
Paradigm:Declarative
Year:1998
Developer:World Wide Web Consortium (W3C)
Latest Release Version:3.0
Implementations:libxslt, Saxon, Xalan
Influenced By:DSSSL
XSLT
Extension:.xslt
Mime:application/xslt+xml
Uniform Type:org.w3.xsl

XSLT (Extensible Stylesheet Language Transformations) is a language originally designed for transforming XML documents into other XML documents,[1] 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.[2] Support for JSON and plain-text transformation was added in later updates to the XSLT 1.0 specification.

, 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]

For an XSLT document transformation, the original document is not changed; rather, a new document is created based on the content of an existing one.[4] 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.[1]

While XSLT was originally designed as a special-purpose language for XML transformation, the language is Turing-complete, making it theoretically capable of arbitrary computations.[5]

History

XSLT is influenced by functional languages,[6] 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.[7]

Design and processing model

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] In contrast to widely implemented imperative programming languages like C, XSLT is declarative.[17] The basic processing paradigm is pattern matching.[18] 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 comprise functional expressions 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 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.

XPath

XSLT 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 compared

XSLT 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 (as a web template language), 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 handlingof narrative documents with more flexible structure, while XQuery is stronger in its data handling, for example when performing relational joins.[19]

Media types

The &lt;output&gt; element can optionally take the attribute media-type, which allows one to set the media 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 types text/xml and application/xml since for a long time there was no registered media type for XSLT. During this time text/xsl became the de facto standard. In XSLT 1.0 it was not specified how the media-type values should be used.

With the release of the XSLT 2.0, the W3C recommended in 2007 the registration of the MIME media type application/xslt+xml[20] and it was later registered with the Internet Assigned Numbers Authority.[21]

Pre-1.0 working drafts of XSLT used text/xsl in their embedding examples, and this type was implemented and continued to be promoted by Microsoft in Internet Explorer[22] and MSXML circa 2012. It is also widely recognized in the xml-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.[23]

Examples

These examples use the following incoming XML document John Smith Morka Ismincius

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:

John Morka

Example 2 (transforming XML to XHTML)

Processing the following example XSLT file

Testing XML Example

Persons

  • ,
  • with the XML input file shown above results in the following XHTML (whitespace has been adjusted here for clarity): Testing XML Example

    Persons

    This XHTML generates the output below when rendered in a web browser.

    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:[24]

    In this example, text/xsl is technically incorrect according to the W3C specifications (which say the type should be application/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.

    Processor implementations

    Performance

    Most early XSLT processors were interpreters. More recently, code generation is increasingly common, using portable intermediate languages (such as Java bytecode or .NET Common 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.[40] This separation is reflected in the design of XSLT processing APIs (such as JAXP).

    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 (e.g., to move calculations out of loops), and lazy pipelined evaluation to reduce the memory footprint of intermediate results (and allow "early exit" when the processor can evaluate an expression such as following-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)[41] 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.[42]

    See also

    Further reading

    External links

    Documentation
    XSLT code libraries

    Notes and References

    1. Web site: Transformation. 2012-09-19.
    2. Web site: XML Output Method. 2012-09-19.
    3. Web site: What is XSLT Used For?. 2018-02-07.
    4. Web site: Introduction . W3C . XSL Transformations (XSLT) Version 1.0 W3C Recommendation . 16 November 1999 . November 7, 2012.
    5. https://www.researchgate.net/publication/221568016_XSLT_Version_20_Is_Turing-Complete_A_Purely_Transformation_Based_Proof XSLT Version 2.0 Is Turing-Complete: A Purely Transformation Based Proof
    6. Web site: What kind of language is XSLT? . Michael Kay . . July 8, 2016.
    7. Web site: A Proposal for XSL . W3C . November 7, 2012.
    8. Web site: XML and Semantic Web W3C Standards Timeline . 2012-02-04 . 2013-04-24 . https://web.archive.org/web/20130424125723/http://www.dblab.ntua.gr/~bikakis/XML%20and%20Semantic%20Web%20W3C%20Standards%20Timeline-History.pdf . dead .
    9. Web site: XSL Transformations (XSLT) . W3.org . 1999-11-16 . 2014-07-12.
    10. Web site: XSL Transformations (XSLT) Version 1.1 . W3.org . 2001-08-24 . 2014-07-12.
    11. Web site: XML Path Language (XPath) 2.0 (Second Edition) . W3.org . 2010-12-14 . 2014-07-12.
    12. Web site: XSL Transformations (XSLT) Version 2.0 . W3.org . 2007-01-23 . 2014-07-12.
    13. Web site: XML and Semantic Web W3C Standards Timeline. 2012-02-04. 2012-02-04. 2013-04-24. https://web.archive.org/web/20130424125723/http://www.dblab.ntua.gr/~bikakis/XML%20and%20Semantic%20Web%20W3C%20Standards%20Timeline-History.pdf. dead.
    14. Web site: What's New in XSLT 3.0?. w3. 6 January 2014.
    15. Web site: Kay. Michael. A Streaming XSLT Processor. Balisage: The Markup Conference 2010 Proceedings. 15 February 2012.
    16. Web site: XSL Transformations (XSLT) Version 2.0 (Second Edition) . 2023-02-07 . www.w3.org . Example: Multiple Result Documents.
    17. Web site: Discover the Wonders of XSLT: XSLT Quirks . XSLT is a very specialized language with a distinct declarative flavor. . 2011-02-11 . 2011-07-09 . https://web.archive.org/web/20110709015528/http://www.developer.com/xml/article.php/3357231#Coding%20styles . dead .
    18. Web site: Kay. Michael. What kind of language is XSLT?. IBM. 13 November 2013.
    19. Web site: Saxonica: XSLT and XQuery . 2022-06-29 . www.saxonica.com.
    20. Web site: XSL Transformations (XSLT) Version 2.0. W3C. 19 October 2012.
    21. Web site: Application Media Types. IANA. 19 October 2012.
    22. Web site: XSLT Requirements for Viewing XML in a Browser. Microsoft. 19 October 2012.
    23. Book: Kay, Michael . XSLT 2.0 and XPath 2.0 Programmer's Reference . limited . 2008 . Wiley . 978-0-470-19274-0 . 100.
    24. Web site: XSL Transformations (XSLT) Version 1.0: W3C Recommendation – Embedding Stylesheets . 16 November 1999 . W3C . 20 September 2016.
    25. Web site: The XSLT C library for GNOME: libxslt. 23 November 2012.
    26. Web site: The XSLT C library for GNOME: The xsltproc tool. 23 November 2012.
    27. Web site: xsltproc man page. 23 November 2012.
    28. Web site: New package: libxslt. 23 November 2012.
    29. Web site: The WebKit Open Source Project - XSLT . 2009-10-25.
    30. Web site: The XML C parser and toolkit of Gnome: Python and bindings. 23 November 2012.
    31. Web site: XML::LibXSLT - Interface to the GNOME libxslt library. CPAN. 23 November 2012.
    32. Web site: libxslt-ruby. 23 November 2012.
    33. Web site: libxml. 23 November 2012.
    34. Web site: cl-libxml2 High-level wrapper around libxml2 and libxslt libraries .
    35. Web site: TclXML. 21 May 2013.
    36. Web site: libxml++. sourceforge.net. 23 November 2012.
    37. Web site: Command Line Transformation Utility (msxsl.exe). Microsoft. 22 October 2012.
    38. Web site: Saxon-JS. Saxonica. 6 September 2018.
    39. Web site: Issue 58151: Fails to load xml file on local file system using XMLHttpRequest .
    40. http://www-128.ibm.com/developerworks/xml/library/x-xslt2/ Saxon: Anatomy of an XSLT processor
    41. Improving Pattern Matching Performance in XSLT. John. Lumley. Michael. Kay. June 2015. XML London 2015. 10.14337/XMLLondon15.Lumley01. 9–25. 2024-02-27 . 978-0-9926471-2-4. free.
    42. Benchmarking XSLT Performance. Michael. Kay. Debbie. Lockett. June 2014. XML London 2014. 10.14337/XMLLondon14.Kay01. 10–23. 2024-02-27 . 978-0-9926471-1-7. free.