XSLT(Extensible Stylesheet Language Transformations) is a language originally designed fortransformingXMLdocuments into other XML documents,[1]or other formats such asHTMLforweb pages,plain textorXSL Formatting Objects,which may subsequently be converted to other formats, such asPDF,PostScriptandPNG.[2]Support for JSON and plain-text transformation was added in later updates to the XSLT 1.0 specification.
Paradigm | Declarative |
---|---|
Developer | World Wide Web Consortium(W3C) |
First appeared | 1998 |
Stable release | 3.0
/ June 8, 2017 |
Filename extensions | .xslt |
Website | www |
Majorimplementations | |
libxslt,Saxon,Xalan | |
Influenced by | |
DSSSL |
Filename extension | .xslt |
---|---|
Internet media type |
application/xslt+xml |
Uniform Type Identifier (UTI) | org.w3.xsl |
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]
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 anXQuery and XPath Data Modelcan be used, such asrelational databasetables 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]
History
editXSLT is influenced byfunctional languages,[6]and by text-based pattern matching languages likeSNOBOLandAWK.Its most direct predecessor isDSSSL,which did forSGMLwhat 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-FOandXPath.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 recommendationin 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 browsersor 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 theXQueryworking 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:
- String manipulation usingregular expressions
- 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 streamingwhich 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.
Design and processing model
editThe 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 programminglanguages 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 comprisefunctionalexpressionsthat 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 sourcetreefrom 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
editXSLT usesXPathto 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.
XQuery compared
editXSLT 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 includeXPath2.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 languagein the tradition ofSQL.
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.[20]
Media types
editThe<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]
Examples
editThese examples use the following incoming XML document:
<?xml version= "1.0"?>
<persons>
<personusername="JS1">
<name>John</name>
<family-name>Smith</family-name>
</person>
<personusername="MI1">
<name>Morka</name>
<family-name>Ismincius</family-name>
</person>
</persons>
Example 1 (transforming XML to XML)
editThis XSLT stylesheet provides templates to transform the XML document:
<?xml version= "1.0" encoding= "UTF-8"?>
<xsl:stylesheetxmlns:xsl="http://www.w3.org/1999/XSL/Transform"version="1.0">
<xsl:outputmethod="xml"indent="yes"/>
<xsl:templatematch="/persons">
<root>
<xsl:apply-templatesselect="person"/>
</root>
</xsl:template>
<xsl:templatematch="person">
<nameusername="{@username}">
<xsl:value-ofselect="name"/>
</name>
</xsl:template>
</xsl:stylesheet>
Its evaluation results in a new XML document, having another structure:
<?xml version= "1.0" encoding= "UTF-8"?>
<root>
<nameusername="JS1">John</name>
<nameusername="MI1">Morka</name>
</root>
Example 2 (transforming XML to XHTML)
editProcessing the following example XSLT file
<?xml version= "1.0" encoding= "UTF-8"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/xhtml">
<xsl:outputmethod="xml"indent="yes"encoding="UTF-8"/>
<xsl:templatematch="/persons">
<html>
<head><title>TestingXMLExample</title></head>
<body>
<h1>Persons</h1>
<ul>
<xsl:apply-templatesselect="person">
<xsl:sortselect="family-name"/>
</xsl:apply-templates>
</ul>
</body>
</html>
</xsl:template>
<xsl:templatematch="person">
<li>
<xsl:value-ofselect="family-name"/><xsl:text>,</xsl:text><xsl:value-ofselect="name"/>
</li>
</xsl:template>
</xsl:stylesheet>
with the XML input file shown above results in the followingXHTML(whitespacehas been adjusted here for clarity):
<?xml version= "1.0" encoding= "UTF-8"?>
<htmlxmlns="http://www.w3.org/1999/xhtml">
<head><title>TestingXMLExample</title></head>
<body>
<h1>Persons</h1>
<ul>
<li>Ismincius,Morka</li>
<li>Smith,John</li>
</ul>
</body>
</html>
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:[25]
<?xml-stylesheet href= "example2.xsl" type= "text/xsl"?>
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.
Processor implementations
edit- RaptorXML fromAltovais an XSLT 3.0 processor available in the XMLSpy development toolkit and as a free-standing server implementation, invoked using a REST interface.
- IBMoffers XSLT processing embedded in a special-purpose hardware appliance under theDatapowerbrand.
- libxsltis afreelibraryreleased under theMIT Licensethat can be reused in commercial applications. It is based onlibxmland implemented inCfor speed and portability. It supports XSLT 1.0 andEXSLTextensions.[26]
- It can be used at the command line via xsltproc[27]which is included inmacOS[28]and manyLinux distributions,and can be used onWindowsviaCygwin.[29]
- TheWebKitandBlinklayout engines, used for example in theSafariandChromeweb browsers respectively, uses the libxslt library to do XSL transformations.[30]
- Bindingsexist forPython,[31]Perl,[32]Ruby,[33]PHP,[34]Common Lisp,[35]Tcl,[36]andC++.[37]
- Microsoftprovides two XSLT processors (both XSLT 1.0 only). The earlier processorMSXMLprovides COM interfaces; from MSXML 4.0 it also includes the command line utility
msxsl.exe
.[38]The.NET runtime includes a separate built-in XSLT processor in itsSystem.Xml.Xsl
library. - Saxonis an XSLT 3.0 and XQuery 3.1 processor withopen-sourceandproprietaryversions for stand-alone operation and forJava,JavaScriptand.NET. A separate product Saxon-JS[39]offers XSLT 3.0 processing onNode.jsand in the browser.
- xjsltis anopen-sourceXSLT 2.0 compiler forJavaScriptsupportingNode.jsand the browser.
- Xalanis an open source XSLT 1.0 processor from theApache Software Foundationavailable 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]
- Adobe AXSLE engine, a proprietary library
Performance
editMost early XSLT processors were interpreters. More recently, code generation is increasingly common, using portable intermediate languages (such asJava bytecodeor.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 Modelsand the processor would act on them directly.XPathengines 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 footprintof 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 Kayintroduced an open-source benchmarking framework for XSLT processors called XT-Speedo.[44]
See also
edit- XSLT elements– a list of some commonly used XSLT structures.
- Muenchian grouping– a dialect differential between XSLT1 and XSLT2+.
- eXtensible Stylesheet Language– a family of languages of which XSLT is a member
- XQuery and XSLT compared
- XSL formatting objectsorXSL-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.
References
edit- ^ab"Transformation".2012-09-19.
- ^"XML Output Method".2012-09-19.
- ^"What is XSLT Used For?".2018-02-07.
- ^"Introduction".XSL Transformations (XSLT) Version 1.0 W3C Recommendation.W3C. 16 November 1999.RetrievedNovember 7,2012.
- ^XSLT Version 2.0 Is Turing-Complete: A Purely Transformation Based Proof
- ^Michael Kay."What kind of language is XSLT?".IBM.RetrievedJuly 8,2016.
- ^"A Proposal for XSL".W3C.RetrievedNovember 7,2012.
- ^"XML and Semantic Web W3C Standards Timeline"(PDF).Archived fromthe original(PDF)on 2013-04-24.Retrieved2012-02-04.
- ^"XSL Transformations (XSLT)".W3.org. 1999-11-16.Retrieved2014-07-12.
- ^"XSL Transformations (XSLT) Version 1.1".W3.org. 2001-08-24.Retrieved2014-07-12.
- ^"XML Path Language (XPath) 2.0 (Second Edition)".W3.org. 2010-12-14.Retrieved2014-07-12.
- ^"XSL Transformations (XSLT) Version 2.0".W3.org. 2007-01-23.Retrieved2014-07-12.
- ^"XML and Semantic Web W3C Standards Timeline"(PDF).2012-02-04. Archived fromthe original(PDF)on 2013-04-24.Retrieved2012-02-04.
- ^"What's New in XSLT 3.0?".w3.Retrieved6 January2014.
- ^ Kay, Michael."A Streaming XSLT Processor".Balisage: The Markup Conference 2010 Proceedings.Retrieved15 February2012.
- ^Flatt, Amelie; Langner, Arne; Leps, Olof (2022),"Phase III: Generating Artifacts from the Model",Model-Driven Development of Akoma Ntoso Application Profiles,Cham: Springer International Publishing, pp. 31–37,doi:10.1007/978-3-031-14132-4_5,ISBN978-3-031-14131-7,retrieved2023-01-07
- ^"XSL Transformations (XSLT) Version 2.0 (Second Edition)".www.w3.org.Retrieved2023-02-07.
Example: Multiple Result Documents
- ^"Discover the Wonders of XSLT: XSLT Quirks".Archived fromthe originalon 2011-07-09.Retrieved2011-02-11.
XSLT is a very specialized language with a distinct declarative flavor.
- ^Kay, Michael."What kind of language is XSLT?".IBM.Retrieved13 November2013.
- ^"Saxonica: XSLT and XQuery".www.saxonica.com.Retrieved2022-06-29.
- ^"XSL Transformations (XSLT) Version 2.0".W3C.Retrieved19 October2012.
- ^"Application Media Types".IANA.Retrieved19 October2012.
- ^"XSLT Requirements for Viewing XML in a Browser".Microsoft.Retrieved19 October2012.
- ^Kay, Michael (2008).XSLT 2.0 and XPath 2.0 Programmer's Reference.Wiley. p.100.ISBN978-0-470-19274-0.
- ^ab"XSL Transformations (XSLT) Version 1.0: W3C Recommendation – Embedding Stylesheets".W3C. 16 November 1999.Retrieved20 September2016.
- ^"The XSLT C library for GNOME: libxslt".Retrieved23 November2012.
- ^"The XSLT C library for GNOME: The xsltproc tool".Retrieved23 November2012.
- ^"xsltproc man page".Retrieved23 November2012.
- ^"New package: libxslt".Retrieved23 November2012.
- ^"The WebKit Open Source Project - XSLT".Retrieved2009-10-25.
- ^"The XML C parser and toolkit of Gnome: Python and bindings".Retrieved23 November2012.
- ^"XML::LibXSLT - Interface to the GNOME libxslt library".CPAN.Retrieved23 November2012.
- ^"libxslt-ruby".Retrieved23 November2012.
- ^"libxml".Retrieved23 November2012.
- ^"cl-libxml2 High-level wrapper around libxml2 and libxslt libraries".
- ^"TclXML".Retrieved21 May2013.
- ^"libxml++".sourceforge.net.Retrieved23 November2012.
- ^"Command Line Transformation Utility (msxsl.exe)".Microsoft.Retrieved22 October2012.
- ^"Saxon-JS".Saxonica.Retrieved6 September2018.
- ^"Common XSLT Errors".MDN Web Docs.Mozilla. 10 July 2023.Retrieved16 September2024.
- ^"Issue 58151: Fails to load xml file on local file system using XMLHttpRequest".
- ^Saxon: Anatomy of an XSLT processor- Article describing implementation & optimization details of a popular XSLT processor.
- ^Lumley, John; Kay, Michael (June 2015)."Improving Pattern Matching Performance in XSLT".XML London 2015:9–25.doi:10.14337/XMLLondon15.Lumley01(inactive 1 November 2024).ISBN978-0-9926471-2-4.
{{cite journal}}
:CS1 maint: DOI inactive as of November 2024 (link) - ^Kay, Michael; Lockett, Debbie (June 2014)."Benchmarking XSLT Performance".XML London 2014:10–23.doi:10.14337/XMLLondon14.Kay01(inactive 1 November 2024).ISBN978-0-9926471-1-7.
{{cite journal}}
:CS1 maint: DOI inactive as of November 2024 (link)
Further reading
edit- XSLTby Doug Tidwell, published by O’Reilly (ISBN0-596-00053-7)
- XSLT Cookbookby Sal Mangano, published by O’Reilly (ISBN0-596-00974-7)
- XSLT 2.0 Programmer's ReferencebyMichael Kay(ISBN0-764-56909-0)
- XSLT 2.0 and XPath 2.0 Programmer's Referenceby Michael Kay (ISBN978-0-470-19274-0)
- XSLT 2.0 Web Developmentby Dmitry Kirsanov (ISBN0-13-140635-3)
- XSL Companion, 2nd Editionby Neil Bradley, published by Addison-Wesley (ISBN0-201-77083-0)
- XSLT and XPath on the Edge (Unlimited Edition)byJeni Tennison,published by Hungry Minds Inc, U.S. (ISBN0-7645-4776-3)
- XSLT & XPath, A Guide to XML Transformationsby John Robert Gardner and Zarella Rendon, published by Prentice-Hall (ISBN0-13-040446-2)
- XSL-FOby Dave Pawson, published by O'Reilly (ISBN978-0-596-00355-5)
External links
edit- Documentation
- XSLT 1.0 W3C Recommendation
- XSLT 2.0 W3C Recommendation
- XSLT 3.0 W3C Recommendation
- XSLT - MDC DocsArchived2011-12-26 at theWayback MachinebyMozilla Developer Network
- XSLT Reference (MSDN)
- XSLT Elements (Saxon)
- XSLT introduction and reference
- XSLT code libraries
- EXSLTis a widespread community initiative to provide extensions to XSLT.
- FXSLis a library implementing support forHigher-order functionsin XSLT. FXSL is written in XSLT itself.
- The XSLT Standard Libraryxsltsl, 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.
- KernowA GUI for Saxon that provides a point and click interface for running transforms.
- xslt.js – Transform XML with XSLTJavaScript library that transforms XML with XSLT in the browser.