Jump to content

XSLT: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Tags: Mobile edit Mobile web edit
Citation bot (talk | contribs)
Added doi-broken-date. | Use this bot. Report bugs. | Suggested by LeapTorchGear | #UCB_webform 262/339
(48 intermediate revisions by 39 users not shown)
Line 1: Line 1:
{{short description|Language for transforming XML documents}}
{{Infobox programming language
{{Infobox programming language
| name = XSLT
| name = XSLT
| logo =
| logo =
| logo caption =
| logo caption =
| screenshot =
| screenshot =
| screenshot caption =
| screenshot caption =
| file ext =
| file ext = .xslt
| paradigm = [[Declarative programming|Declarative]]
| paradigm = [[Declarative programming|Declarative]]
| scope =
| scope =
| year = 1998
| year = 1998
| designer =
| designer =
| developer = [[W3C]]
| developer = [[World Wide Web Consortium]] (W3C)
| latest release version = 3.0
| latest release version = 3.0
| latest release date = <!-- {{start date and age|2007|01|23}} -->
| latest release date = {{release date and age|2017|6|8}}
| latest preview date = <!-- {{start date and age|2014|10|2}} -->
| latest preview date = <!-- {{start date and age|2014|10|2}} -->
| typing =
| typing =
| implementations = [[libxslt]], [[Saxon XSLT|Saxon]], [[Xalan]]
| implementations = [[libxslt]], [[Saxon XSLT|Saxon]], [[Xalan]]
| influenced by = [[DSSSL]]
| versions = XSLT 1.0, XSLT 2.0, XSLT 3.0
| influenced by = [[DSSSL]]
| influenced =
| programming language =
| influenced =
| platform =
| programming language =
| operating system =
| platform =
| license =
| operating system =
| website = {{URL|https://www.w3.org/TR/xslt-30/}}
| license =
| wikibooks =
| website = [http://www.w3.org/standards/xml/transformation W3C Transformation]
}}
| wikibooks =
{{Infobox file format
}}'''XSLT''' ('''eXtensible Stylesheet Language Transformations''') is a language for [[XML transformation language|transforming]] [[XML]] documents into other XML documents,<ref name="Transformation">{{cite web|url=http://www.w3.org/standards/xml/transformation |title=Transformation|date=2012-09-19}}</ref> or other formats such as [[HTML]] for [[web page]]s, [[plain text]] or [[XSL Formatting Objects]], which may subsequently be converted to other formats, such as [[Portable Document Format|PDF]], [[PostScript]] and [[Portable Network Graphic|PNG]].<ref>{{cite web|url=http://www.w3.org/TR/xslt#output |title=XML Output Method|date=2012-09-19}}</ref> XSLT 1.0 is widely supported in modern web browsers.<ref name="Used for">{{cite web|url=https://www.w3.org/standards/xml/transformation#uses|title=What is XSLT Used For?|date=2018-02-07}}</ref>
| name = XSLT
| icon =
| extension = <code>.xslt</code>
| mime = application/xslt+xml
| uniform type = org.w3.xsl
| owner =
| genre =
| url =
| containerfor =
| containedby =
| extendedfrom =
| extendedto =
}}
'''XSLT''' ('''Extensible Stylesheet Language Transformations''') is a language originally designed for [[XML transformation language|transforming]] [[XML]] documents into other XML documents,<ref name="Transformation">{{cite web|url=http://www.w3.org/standards/xml/transformation |title=Transformation|date=2012-09-19}}</ref> or other formats such as [[HTML]] for [[web page]]s, [[plain text]] or [[XSL Formatting Objects]], which may subsequently be converted to other formats, such as [[Portable Document Format|PDF]], [[PostScript]] and [[Portable Network Graphics|PNG]].<ref>{{cite web|url=http://www.w3.org/TR/xslt#output |title=XML Output Method|date=2012-09-19}}</ref> Support for JSON and plain-text transformation was added in later updates to the XSLT 1.0 specification.


{{As of|August 2022}}, the most recent stable version of the language is XSLT 3.0, which achieved Recommendation status in June 2017.
The original document is not changed; rather, a new document is created based on the content of an existing one.<ref>{{cite web | url=http://www.w3.org/TR/xslt#section-Introduction | title=Introduction | publisher=W3C | work=XSL Transformations (XSLT) Version 1.0 W3C Recommendation | date=16 November 1999 | accessdate=November 7, 2012}}</ref> 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]].<ref name="Transformation"/>


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.<ref name="Used for">{{cite web|url=https://www.w3.org/standards/xml/transformation#uses|title=What is XSLT Used For?|date=2018-02-07}}</ref>
Although XSLT is designed as a special-purpose language for XML transformation, the language is [[Turing-complete]], making it theoretically capable of arbitrary computations.<ref>[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]</ref>

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.<ref>{{cite web | url=http://www.w3.org/TR/xslt#section-Introduction | title=Introduction | publisher=W3C | work=XSL Transformations (XSLT) Version 1.0 W3C Recommendation | date=16 November 1999 | access-date=November 7, 2012}}</ref> 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]].<ref name="Transformation"/>

While XSLT was originally designed as a special-purpose language for XML transformation, the language is [[Turing completeness|Turing-complete]], making it theoretically capable of arbitrary computations.<ref>[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]</ref>


==History==
==History==
XSLT is influenced by [[functional programming|functional languages]],<ref>{{cite web | url=http://www.ibm.com/developerworks/library/x-xslt/ | author=Michael Kay | title=What kind of language is XSLT? | accessdate=July 8, 2016}}</ref> and by text-based pattern matching languages like [[SNOBOL]] and [[AWK (programming language)|AWK]]. Its most direct predecessor is [[DSSSL]], which did for [[SGML]] what XSLT does for XML.<ref>{{cite web | url=http://www.w3.org/TR/NOTE-XSL.html | title=A Proposal for XSL | publisher=W3C | accessdate=November 7, 2012}}</ref>
XSLT is influenced by [[functional programming|functional languages]],<ref>{{cite web | url=http://www.ibm.com/developerworks/library/x-xslt/ | author=Michael Kay | title=What kind of language is XSLT? | website=[[IBM]] | access-date=July 8, 2016}}</ref> 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.<ref>{{cite web | url=http://www.w3.org/TR/NOTE-XSL.html | title=A Proposal for XSL | publisher=W3C | access-date=November 7, 2012}}</ref>
* XSLT 1.0: XSLT was part of the [[World Wide Web Consortium]] (W3C)'s [[Extensible Stylesheet Language|eXtensible Stylesheet Language]] (XSL) 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 (XML expert)|James Clark]], the editor, had previously worked on DSSSL. XSLT 1.0 was published as a [[W3C recommendation]] in November 1999.<ref name="timelinehistory">{{cite web|url=http://www.dblab.ntua.gr/~bikakis/XML%20and%20Semantic%20Web%20W3C%20Standards%20Timeline-History.pdf |title=XML and Semantic Web W3C Standards Timeline
* XSLT 1.0: XSLT was part of the [[World Wide Web Consortium]] (W3C)'s [[Extensible Stylesheet Language|eXtensible Stylesheet Language]] (XSL) 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 (XML expert)|James Clark]], the editor, had previously worked on DSSSL. XSLT 1.0 was published as a [[W3C recommendation]] in November 1999.<ref name="timelinehistory">{{cite web |url=http://www.dblab.ntua.gr/~bikakis/XML%20and%20Semantic%20Web%20W3C%20Standards%20Timeline-History.pdf |title=XML and Semantic Web W3C Standards Timeline |access-date=2012-02-04 |archive-date=2013-04-24 |archive-url=https://web.archive.org/web/20130424125723/http://www.dblab.ntua.gr/~bikakis/XML%20and%20Semantic%20Web%20W3C%20Standards%20Timeline-History.pdf |url-status=dead }}</ref> Despite its age, XSLT 1.0<ref>{{cite web|url=http://www.w3.org/TR/xslt |title=XSL Transformations (XSLT) |publisher=W3.org |date=1999-11-16 |access-date=2014-07-12}}</ref> is still widely used ({{as of|2018|lc=y}}), since later versions are not supported natively in [[web browser]]s or for environments like [[LAMP (software bundle)|LAMP]].
}}</ref> Despite its age, XSLT 1.0<ref>{{cite web|url=http://www.w3.org/TR/xslt |title=XSL Transformations (XSLT) |publisher=W3.org |date=1999-11-16 |accessdate=2014-07-12}}</ref> is still widely used (as of 2018), since later versions are not supported natively in [[web browser]]s or for environments like [[LAMP (software bundle)|LAMP]].


* XSLT 2.0: after an abortive attempt to create a version 1.1 in 2001,<ref>{{cite web|url=http://www.w3.org/TR/xslt11/ |title=XSL Transformations (XSLT) Version 1.1 |publisher=W3.org |date=2001-08-24 |accessdate=2014-07-12}}</ref> the XSL working group joined forces with the [[XQuery]] working group to create [[XPath 2.0]],<ref>{{cite web|url=http://www.w3.org/TR/xpath20/ |title=XML Path Language (XPath) 2.0 (Second Edition) |publisher=W3.org |date=2010-12-14 |accessdate=2014-07-12}}</ref> with a richer data model and type system based on [[XML Schema (W3C)|XML Schema]]. Building on this is XSLT 2.0,<ref>{{cite web|url=http://www.w3.org/TR/xslt20/ |title=XSL Transformations (XSLT) Version 2.0 |publisher=W3.org |date=2007-01-23 |accessdate=2014-07-12}}</ref> developed under the editorship of [[Michael Howard Kay|Michael Kay]], which reached recommendation status in January 2007.<ref>{{cite web|url=http://www.dblab.ntua.gr/~bikakis/XML%20and%20Semantic%20Web%20W3C%20Standards%20Timeline-History.pdf |title=XML and Semantic Web W3C Standards Timeline|date=2012-02-04}}</ref> The most important innovations in XSLT 2.0 include:
* XSLT 2.0: after an abortive attempt to create a version 1.1 in 2001,<ref>{{cite web|url=http://www.w3.org/TR/xslt11/ |title=XSL Transformations (XSLT) Version 1.1 |publisher=W3.org |date=2001-08-24 |access-date=2014-07-12}}</ref> the XSL working group joined forces with the [[XQuery]] working group to create [[XPath 2.0]],<ref>{{cite web|url=http://www.w3.org/TR/xpath20/ |title=XML Path Language (XPath) 2.0 (Second Edition) |publisher=W3.org |date=2010-12-14 |access-date=2014-07-12}}</ref> with a richer data model and type system based on [[XML Schema (W3C)|XML Schema]]. Building on this is XSLT 2.0,<ref>{{cite web|url=http://www.w3.org/TR/xslt20/ |title=XSL Transformations (XSLT) Version 2.0 |publisher=W3.org |date=2007-01-23 |access-date=2014-07-12}}</ref> developed under the editorship of [[Michael Howard Kay|Michael Kay]], which reached recommendation status in January 2007.<ref>{{cite web|url=http://www.dblab.ntua.gr/~bikakis/XML%20and%20Semantic%20Web%20W3C%20Standards%20Timeline-History.pdf|title=XML and Semantic Web W3C Standards Timeline|date=2012-02-04|access-date=2012-02-04|archive-date=2013-04-24|archive-url=https://web.archive.org/web/20130424125723/http://www.dblab.ntua.gr/~bikakis/XML%20and%20Semantic%20Web%20W3C%20Standards%20Timeline-History.pdf|url-status=dead}}</ref> The most important innovations in XSLT 2.0 include:
** String manipulation using [[regular expressions]]
** String manipulation using [[regular expressions]]
** Functions and operators for manipulating dates, times, and durations
** Functions and operators for manipulating dates, times, and durations
Line 44: Line 62:


* XSLT 3.0: became a W3C Recommendation on 8 June 2017. The main new features are:<ref name=new-in-3>{{cite web|title=What's New in XSLT 3.0?
* XSLT 3.0: became a W3C Recommendation on 8 June 2017. The main new features are:<ref name=new-in-3>{{cite web|title=What's New in XSLT 3.0?
|url=http://www.w3.org/TR/xslt-30/#whats-new-in-xslt3|publisher=w3|access-date=6 January 2014}}</ref>

|url=http://www.w3.org/TR/xslt-30/#whats-new-in-xslt3|publisher=w3|accessdate=6 January 2014}}</ref>
** [[Streaming XML|Streaming transformations]]: in previous versions the entire input document had to be read into memory before it could be processed,<ref>
** [[Streaming XML|Streaming transformations]]: in previous versions the entire input document had to be read into memory before it could be processed,<ref>
{{cite web|last=Kay|first=Michael|title=A Streaming XSLT Processor|url=http://www.balisage.net/Proceedings/vol5/html/Kay01/BalisageVol5-Kay01.html|publisher=Balisage: The Markup Conference 2010 Proceedings|accessdate=15 February 2012}}</ref> and output could not be written until processing had finished (although Saxon does have a streaming extension). The working draft allows [[Streaming XML|XML streaming]] which will be useful for processing documents too large to fit in memory, or when transformations are chained in [[XML Pipelines]].
{{cite web|last=Kay|first=Michael|title=A Streaming XSLT Processor|url=http://www.balisage.net/Proceedings/vol5/html/Kay01/BalisageVol5-Kay01.html|publisher=Balisage: The Markup Conference 2010 Proceedings|access-date=15 February 2012}}</ref> and output could not be written until processing had finished. XSLT 3.0 allows [[Streaming XML|XML streaming]] which is useful for processing documents too large to fit in memory or when transformations are chained in [[XML Pipelines]].
** Improvements to the modularity of large stylesheets.
** Packages, to improve the modularity of large stylesheets.
** Improved handling of dynamic errors with, for example, an xsl:try instruction.
** 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.
** Functions can now be arguments to other (higher-order) functions.


==Design and processing model==
==Design and processing model==
[[Image:XSLT en.svg|right|thumb|Diagram of the basic elements and process flow of eXtensible Stylesheet Language Transformations.]]
[[Image:XSLT en.svg|right|thumb|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 an output document. In contrast to widely implemented [[Imperative programming|imperative]] programming languages like [[C (programming language)|C]], XSLT is [[Declarative programming|declarative]].<ref>{{cite web|title=Discover the Wonders of XSLT: XSLT Quirks | url=http://www.developer.com/xml/article.php/3357231#Coding%20styles | quote=XSLT is a very specialized language with a distinct declarative flavor.}}</ref> The basic processing paradigm is pattern matching.<ref name=IBM>{{cite web|last=Kay|first=Michael|title=What kind of language is XSLT?|url=http://www.ibm.com/developerworks/library/x-xslt/|publisher=IBM|accessdate=13 November 2013}}</ref> 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 programming|functional]] [[expression (programming)|expressions]] that directly represent their evaluated form: the result tree, which is the basis of the processor's output.
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.<ref>{{Citation |last1=Flatt |first1=Amelie |title=Phase III: Generating Artifacts from the Model |date=2022 |url=https://link.springer.com/10.1007/978-3-031-14132-4_5 |work=Model-Driven Development of Akoma Ntoso Application Profiles |pages=31–37 |place=Cham |publisher=Springer International Publishing |language=en |doi=10.1007/978-3-031-14132-4_5 |isbn=978-3-031-14131-7 |access-date=2023-01-07 |last2=Langner |first2=Arne |last3=Leps |first3=Olof}}</ref><ref>{{Cite web |title=XSL Transformations (XSLT) Version 2.0 (Second Edition) |url=https://www.w3.org/TR/xslt20/#creating-result-trees |access-date=2023-02-07 |website=www.w3.org |quote=Example: Multiple Result Documents}}</ref> In contrast to widely implemented [[imperative programming]] languages like [[C (programming language)|C]], XSLT is [[Declarative programming|declarative]].<ref>{{cite web |title=Discover the Wonders of XSLT: XSLT Quirks |url=http://www.developer.com/xml/article.php/3357231#Coding%20styles |quote=XSLT is a very specialized language with a distinct declarative flavor. |access-date=2011-02-11 |archive-date=2011-07-09 |archive-url=https://web.archive.org/web/20110709015528/http://www.developer.com/xml/article.php/3357231#Coding%20styles |url-status=dead }}</ref> The basic processing paradigm is pattern matching.<ref name=IBM>{{cite web|last=Kay|first=Michael|title=What kind of language is XSLT?|url=http://www.ibm.com/developerworks/library/x-xslt/|publisher=IBM|access-date=13 November 2013}}</ref> 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 programming|functional]] [[expression (programming)|expressions]] that directly represent their evaluated form: the result tree, which is the basis of the processor's output.

The processor follows a fixed algorithm.<ref>{{cite web| title=XSLT Definitions | url=http://microsoftechies.wordpress.com/2010/09/16/xslt-definitions/ | quote=XSLT declarations define a set of rules and guidelines that are applied during processing according to a predefined algorithm.}}</ref> First, assuming a stylesheet has already been read and prepared, the processor builds a source [[Tree data structure|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. Output derives from the result tree.


A typical processor behaves as follows. First, assuming a stylesheet has already been read and prepared, the processor builds a source [[Tree data structure|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==
==XPath==
Line 71: Line 88:
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 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 [[web template|web template language]]), or on paper. XQuery was primarily conceived as a [[database query language]] in the tradition of [[SQL]].
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|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 handling
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.{{Citation needed|date=July 2009}}
of narrative documents with more flexible structure, while XQuery is stronger in its data handling, for example when performing relational joins.<ref>{{Cite web |title=Saxonica: XSLT and XQuery |url=https://www.saxonica.com/technology/xslt-and-xquery.xml |access-date=2022-06-29 |website=www.saxonica.com}}</ref>


==Media types==
==Media types==
The <code>&lt;output&gt;</code> element can optionally take the attribute <code>media-type</code>, which allows one to set the [[Internet media type|media type]] (or MIME type) for the resulting output, for example: <code><xsl:output output="xml" media-type="application/xml"/></code>. The XSLT 1.0 recommendation recommends the more general attribute types <code>text/xml</code> and <code>application/xml</code> since for a long time there was no registered media type for XSLT. During this time <code>text/xsl</code> became the de facto standard. In XSLT 1.0 it was not specified how the <code>media-type</code> values should be used.
The <code>&lt;output&gt;</code> element can optionally take the attribute <code>media-type</code>, which allows one to set the [[Internet media type|media type]] (or MIME type) for the resulting output, for example: <code><xsl:output output="xml" media-type="application/xml"/></code>. The XSLT 1.0 recommendation recommends the more general attribute types <code>text/xml</code> and <code>application/xml</code> since for a long time there was no registered media type for XSLT. During this time <code>text/xsl</code> became the de facto standard. In XSLT 1.0 it was not specified how the <code>media-type</code> values should be used.


With the release of the XSLT 2.0, the W3C recommended the registration of the MIME media type <code>application/xslt+xml</code><ref>{{cite web|title=XSL Transformations (XSLT) Version 2.0|url=http://www.w3.org/TR/2007/REC-xslt20-20070123/#media-type-registration|publisher=W3C|accessdate=19 October 2012}}</ref> and it was later registered with the [[Internet Assigned Numbers Authority]].<ref>{{cite web|title=Application Media Types|url=http://www.iana.org/assignments/media-types/application/index.html|publisher=IANA|accessdate=19 October 2012}}</ref>
With the release of the XSLT 2.0, the W3C recommended in 2007 the registration of the MIME media type <code>application/xslt+xml</code><ref>{{cite web|title=XSL Transformations (XSLT) Version 2.0|url=http://www.w3.org/TR/2007/REC-xslt20-20070123/#media-type-registration|publisher=W3C|access-date=19 October 2012}}</ref> and it was later registered with the [[Internet Assigned Numbers Authority]].<ref>{{cite web|title=Application Media Types|url=https://www.iana.org/assignments/media-types/application/index.html|publisher=IANA|access-date=19 October 2012}}</ref>


Pre-1.0 working drafts of XSLT used <code>text/xsl</code> in their embedding examples, and this type was implemented and continues to be promoted by Microsoft in Internet Explorer<ref>{{cite web|title=XSLT Requirements for Viewing XML in a Browser|url=http://msdn.microsoft.com/en-us/library/windows/desktop/ms757857(v=vs.85).aspx|publisher=Microsoft|accessdate=19 October 2012}}</ref> and MSXML. It is also widely recognized in the <code>xml-stylesheet</code> processing 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.<ref>{{cite book | last=Kay | first=Michael | title=XSLT 2.0 and XPath 2.0 Programmer's Reference | year=2008 | publisher=Wiley | ISBN=978-0-470-19274-0 | page=100}}</ref>
Pre-1.0 working drafts of XSLT used <code>text/xsl</code> in their embedding examples, and this type was implemented and continued to be promoted by Microsoft in Internet Explorer<ref>{{cite web|title=XSLT Requirements for Viewing XML in a Browser|url=http://msdn.microsoft.com/en-us/library/windows/desktop/ms757857(v=vs.85).aspx|publisher=Microsoft|access-date=19 October 2012}}</ref> and MSXML circa 2012. It is also widely recognized in the <code>xml-stylesheet</code> 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.<ref>{{cite book | last=Kay | first=Michael | title=XSLT 2.0 and XPath 2.0 Programmer's Reference | url=https://archive.org/details/xsltxpathprogram00kaym_646 | url-access=limited | year=2008 | publisher=Wiley | isbn=978-0-470-19274-0 | page=[https://archive.org/details/xsltxpathprogram00kaym_646/page/n151 100]}}</ref>


==Examples==
==Examples==
These examples use the following incoming XML document<source lang="xml">
These examples use the following incoming XML document<syntaxhighlight lang="xml">
<?xml version="1.0" ?>
<?xml version="1.0" ?>
<persons>
<persons>
Line 96: Line 113:
</person>
</person>
</persons>
</persons>
</syntaxhighlight>
</source>


===Example 1 (transforming XML to XSLT)===
===Example 1 (transforming XML to XML)===
This XSLT stylesheet provides templates to transform the XML document:<source lang="xml">
This XSLT stylesheet provides templates to transform the XML document:<syntaxhighlight lang="xml">
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
Line 117: Line 134:


</xsl:stylesheet>
</xsl:stylesheet>
</syntaxhighlight>
</source>


Its evaluation results in a new XML document, having another structure:
Its evaluation results in a new XML document, having another structure:


<source lang="xml">
<syntaxhighlight lang="xml">
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<root>
<root>
Line 127: Line 144:
<name username="MI1">Morka</name>
<name username="MI1">Morka</name>
</root>
</root>
</syntaxhighlight>
</source>


===Example 2 (transforming XML to XHTML)===
===Example 2 (transforming XML to XHTML)===
Processing the following example XSLT file
Processing the following example XSLT file
<source lang="xml">
<syntaxhighlight lang="xml">
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
<xsl:stylesheet
Line 161: Line 178:


</xsl:stylesheet>
</xsl:stylesheet>
</syntaxhighlight>
</source>
with the XML input file shown above results in the following [[XHTML]] ([[whitespace (computer science)|whitespace]] has been adjusted here for clarity):
with the XML input file shown above results in the following [[XHTML]] ([[whitespace (computer science)|whitespace]] has been adjusted here for clarity):
<source lang="xml">
<syntaxhighlight lang="xml">
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml">
<html xmlns="http://www.w3.org/1999/xhtml">
Line 175: Line 192:
</body>
</body>
</html>
</html>
</syntaxhighlight>
</source>
This XHTML generates the output below when rendered in a web browser.
This XHTML generates the output below when rendered in a web browser.
[[Image:xslt ex2.png|thumb|center|Rendered XHTML generated from an XML input file and an XSLT transformation.]]
[[Image:xslt ex2.png|thumb|center|Rendered XHTML generated from an XML input file and an XSLT transformation.]]


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:<ref name=w3cxslt>{{cite web | url = http://www.w3.org/TR/xslt#section-Embedding-Stylesheets | title = XSL Transformations (XSLT) Version 1.0: W3C Recommendation&nbsp;– Embedding Stylesheets | date = 16 November 1999 | publisher = W3C | accessdate = 20 September 2016}}</ref>
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:<ref name=w3cxslt>{{cite web | url = http://www.w3.org/TR/xslt#section-Embedding-Stylesheets | title = XSL Transformations (XSLT) Version 1.0: W3C Recommendation&nbsp;– Embedding Stylesheets | date = 16 November 1999 | publisher = W3C | access-date = 20 September 2016}}</ref>


<source lang="xml">
<syntaxhighlight lang="xml">
<?xml-stylesheet href="example2.xsl" type="text/xsl" ?>
<?xml-stylesheet href="example2.xsl" type="text/xsl" ?>
</syntaxhighlight>
</source>


In this example, <code>text/xsl</code> is technically incorrect according to the W3C specifications<ref name=w3cxslt /> (which say the type should be <code>application/xslt+xml</code>), but it is the only media type that is widely supported across browsers as of 2009.
In this example, <code>text/xsl</code> is technically incorrect according to the W3C specifications<ref name=w3cxslt /> (which say the type should be <code>application/xslt+xml</code>), 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==
==Processor implementations==
*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.
*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 [[IBM WebSphere DataPower SOA Appliances|Datapower]] brand.
*[[IBM]] offers XSLT processing embedded in a special-purpose hardware appliance under the [[IBM WebSphere DataPower SOA Appliances|Datapower]] brand.
*[[libxslt]] is a [[free software|free]] [[library (computing)|library]] released under the [[MIT License]] that can be reused in commercial applications. It is based on [[libxml]] and implemented in [[C (programming language)|C]] for speed and portability. It supports XSLT 1.0 and [[EXSLT]] extensions.<ref>{{cite web|title=The XSLT C library for GNOME: libxslt|url=http://xmlsoft.org/XSLT/index.html|accessdate=23 November 2012}}</ref>
*[[libxslt]] is a [[free software|free]] [[library (computing)|library]] released under the [[MIT License]] that can be reused in commercial applications. It is based on [[libxml]] and implemented in [[C (programming language)|C]] for speed and portability. It supports XSLT 1.0 and [[EXSLT]] extensions.<ref>{{cite web|title=The XSLT C library for GNOME: libxslt|url=http://xmlsoft.org/XSLT/index.html|access-date=23 November 2012}}</ref>
**It can be used at the command line via xsltproc<ref>{{cite web|title=The XSLT C library for GNOME: The xsltproc tool|url=http://xmlsoft.org/XSLT/xsltproc2.html|accessdate=23 November 2012}}</ref> which is included in [[macOS]]<ref>{{cite web|title=xsltproc man page|url=https://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man1/xsltproc.1.html|accessdate=23 November 2012}}</ref> and many [[Linux distributions]], and can be used on [[Microsoft Windows|Windows]] via [[Cygwin]].<ref>{{cite web|title=New package: libxslt|url=http://www.cygwin.com/ml/cygwin-announce/2002/msg00018.html|accessdate=23 November 2012}}</ref>
**It can be used at the command line via xsltproc<ref>{{cite web|title=The XSLT C library for GNOME: The xsltproc tool|url=http://xmlsoft.org/XSLT/xsltproc2.html|access-date=23 November 2012}}</ref> which is included in [[macOS]]<ref>{{cite web|title=xsltproc man page|url=https://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man1/xsltproc.1.html|access-date=23 November 2012}}</ref> and many [[Linux distributions]], and can be used on [[Microsoft Windows|Windows]] via [[Cygwin]].<ref>{{cite web|title=New package: libxslt|url=http://www.cygwin.com/ml/cygwin-announce/2002/msg00018.html|access-date=23 November 2012}}</ref>
**The [[WebKit]] and [[Blink (layout engine)|Blink]] layout engines, used for example in the [[Apple Safari|Safari]] and [[Google Chrome|Chrome]] web browsers respectively, uses the libxslt library to do XSL transformations.<ref>{{cite web | url=http://webkit.org/projects/xslt/index.html | title=The WebKit Open Source Project - XSLT | accessdate=2009-10-25}}</ref>
**The [[WebKit]] and [[Blink (layout engine)|Blink]] layout engines, used for example in the [[Apple Safari|Safari]] and [[Google Chrome|Chrome]] web browsers respectively, uses the libxslt library to do XSL transformations.<ref>{{cite web | url=http://webkit.org/projects/xslt/index.html | title=The WebKit Open Source Project - XSLT | access-date=2009-10-25}}</ref>
**[[Language binding|Bindings]] exist for [[Python (programming language)|Python]],<ref>{{cite web|title=The XML C parser and toolkit of Gnome: Python and bindings|url=http://xmlsoft.org/python.html|accessdate=23 November 2012}}</ref> [[Perl]],<ref>{{cite web|title=XML::LibXSLT - Interface to the GNOME libxslt library|url=https://metacpan.org/module/XML::LibXSLT|publisher=CPAN|accessdate=23 November 2012}}</ref> [[Ruby (programming language)|Ruby]],<ref>{{cite web|title=libxslt-ruby|url=http://rubygems.org/gems/libxslt-ruby|accessdate=23 November 2012}}</ref> [[PHP]],<ref>{{cite web|title=libxml|url=http://www.php.net/manual/en/book.libxml.php|accessdate=23 November 2012}}</ref> [[Common Lisp]],<ref>{{cite web|title=cl-libxml2 High-level wrapper around libxml2 and libxslt libraries |url=https://code.google.com/p/cl-libxml2/}}</ref> [[Tcl]],<ref>{{cite web|title=TclXML|url=http://tclxml.sourceforge.net/|accessdate=21 May 2013}}</ref> and [[C++]].<ref>{{cite web|title=libxml++|url=http://libxmlplusplus.sourceforge.net/|publisher=sourceforge.net|accessdate=23 November 2012}}</ref>
**[[Language binding|Bindings]] exist for [[Python (programming language)|Python]],<ref>{{cite web|title=The XML C parser and toolkit of Gnome: Python and bindings|url=http://xmlsoft.org/python.html|access-date=23 November 2012}}</ref> [[Perl]],<ref>{{cite web|title=XML::LibXSLT - Interface to the GNOME libxslt library|url=https://metacpan.org/module/XML::LibXSLT|publisher=CPAN|access-date=23 November 2012}}</ref> [[Ruby (programming language)|Ruby]],<ref>{{cite web|title=libxslt-ruby|url=http://rubygems.org/gems/libxslt-ruby|access-date=23 November 2012}}</ref> [[PHP]],<ref>{{cite web|title=libxml|url=http://www.php.net/manual/en/book.libxml.php|access-date=23 November 2012}}</ref> [[Common Lisp]],<ref>{{cite web|title=cl-libxml2 High-level wrapper around libxml2 and libxslt libraries |url=https://code.google.com/p/cl-libxml2/}}</ref> [[Tcl]],<ref>{{cite web|title=TclXML|url=http://tclxml.sourceforge.net/|access-date=21 May 2013}}</ref> and [[C++]].<ref>{{cite web|title=libxml++|url=http://libxmlplusplus.sourceforge.net/|publisher=sourceforge.net|access-date=23 November 2012}}</ref>
*[[Microsoft]] provides two XSLT processors (both XSLT 1.0 only). The earlier processor [[MSXML]] provides COM interfaces; from MSXML 4.0 it also includes the command line utility <code>msxsl.exe</code>.<ref>{{cite web|title=Command Line Transformation Utility (msxsl.exe)|url=http://www.microsoft.com/en-us/download/details.aspx?id=21714|publisher=Microsoft|accessdate=22 October 2012}}</ref> The .NET runtime includes a separate built-in XSLT processor in its <code>System.Xml.Xsl</code> library.
*[[Microsoft]] provides two XSLT processors (both XSLT 1.0 only). The earlier processor [[MSXML]] provides COM interfaces; from MSXML 4.0 it also includes the command line utility <code>msxsl.exe</code>.<ref>{{cite web|title=Command Line Transformation Utility (msxsl.exe)|url=http://www.microsoft.com/en-us/download/details.aspx?id=21714|publisher=Microsoft|access-date=22 October 2012}}</ref> The .NET runtime includes a separate built-in XSLT processor in its <code>System.Xml.Xsl</code> library.
*[[Saxon XSLT|Saxon]] is an XSLT 3.0 and XQuery 3.1 processor with [[open-source software|open-source]] and [[proprietary software|proprietary]] versions for stand-alone operation and for [[Java (programming language)|Java]], [[JavaScript]] and .NET. A derived product Saxon-JS<ref>{{cite web|title=Saxon-JS|url=http://www.saxonica.com/saxon-js/index.xml|publisher=Saxonica|accessdate=6 September 2018}}</ref> offers XSLT 3.0 processing in the browser.
*[[Saxon XSLT|Saxon]] is an XSLT 3.0 and XQuery 3.1 processor with [[open-source software|open-source]] and [[proprietary software|proprietary]] versions for stand-alone operation and for [[Java (programming language)|Java]], [[JavaScript]] and .NET. A separate product Saxon-JS<ref>{{cite web|title=Saxon-JS|url=http://www.saxonica.com/saxon-js/index.xml|publisher=Saxonica|access-date=6 September 2018}}</ref> offers XSLT 3.0 processing on [[Node.js]] and in the browser.
*[[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.
*[[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 (only). 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.<ref>{{cite web |url= https://stackoverflow.com/questions/4094912/cant-read-an-xml-and-or-xslt-in-google-chrome |title=Can't read an XML and/or XSLT in Google Chrome |work=Stack Overflow |year=2014 |accessdate=12 July 2014}}</ref>
*Web browsers: Safari, Chrome, Firefox, Opera and Internet Explorer all support XSLT 1.0 (only). 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.<ref>{{cite web |url= https://bugs.chromium.org/p/chromium/issues/detail?id=58151 |title=Issue 58151: Fails to load xml file on local file system using XMLHttpRequest }}</ref>
*Adobe AXSLE engine, a proprietary library


===Performance===
===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.<ref>[http://www-128.ibm.com/developerworks/xml/library/x-xslt2/ Saxon: Anatomy of an XSLT processor] - Article describing implementation &amp; optimization details of a popular XSLT processor.</ref> This separation is reflected in the design of XSLT processing APIs (such as [[Java API for XML Processing|JAXP]]).
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.<ref>[http://www-128.ibm.com/developerworks/xml/library/x-xslt2/ Saxon: Anatomy of an XSLT processor] - Article describing implementation &amp; optimization details of a popular XSLT processor.</ref> This separation is reflected in the design of XSLT processing APIs (such as [[Java API for XML Processing|JAXP]]).


Early XSLT processors had very few optimizations. Stylesheet documents were read into [[Document Object Model]]s 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 <code>following-sibling::*[1]</code> without a complete evaluation of all subexpressions). Many processors also use tree representations that are significantly more efficient (in both space and time)<ref>{{Cite journal|title=Improving Pattern Matching Performance in XSLT|first1=John|last1=Lumley|first2=Michael|last2=Kay|date=June 2015|journal=XML London 2015|doi=10.14337/XMLLondon15.Lumley01|url=http://xmllondon.com/2015/presentations/lumley|pages=9–25|isbn=978-0-9926471-2-4}}</ref> than general-purpose DOM implementations.
Early XSLT processors had very few optimizations. Stylesheet documents were read into [[Document Object Model]]s 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 <code>following-sibling::*[1]</code> without a complete evaluation of all subexpressions). Many processors also use tree representations that are significantly more efficient (in both space and time)<ref>{{Cite journal|title=Improving Pattern Matching Performance in XSLT|first1=John|last1=Lumley|first2=Michael|last2=Kay|date=June 2015|journal=XML London 2015|doi=10.14337/XMLLondon15.Lumley01|url=http://xmllondon.com/2015/presentations/lumley|pages=9–25|doi-broken-date=2024-02-27 |isbn=978-0-9926471-2-4|doi-access=free}}</ref> than general-purpose DOM implementations.


In June 2014, Debbie Lockett and [[Michael Howard Kay|Michael Kay]] introduced an open-source benchmarking framework for XSLT processors called XT-Speedo.<ref>{{Cite journal|title=Benchmarking XSLT Performance|first1=Michael|last1=Kay|first2=Debbie|last2=Lockett|date=June 2014|journal=XML London 2014|doi=10.14337/XMLLondon14.Kay01|url=http://xmllondon.com/2014/presentations/kay|pages=10–23|isbn=978-0-9926471-1-7}}</ref>
In June 2014, Debbie Lockett and [[Michael Howard Kay|Michael Kay]] introduced an open-source benchmarking framework for XSLT processors called XT-Speedo.<ref>{{Cite journal|title=Benchmarking XSLT Performance|first1=Michael|last1=Kay|first2=Debbie|last2=Lockett|date=June 2014|journal=XML London 2014|doi=10.14337/XMLLondon14.Kay01|url=http://xmllondon.com/2014/presentations/kay|pages=10–23|doi-broken-date=2024-02-27 |isbn=978-0-9926471-1-7|doi-access=free}}</ref>


==See also==
==See also==
Line 236: Line 254:
* [http://www.w3.org/TR/xslt20/ XSLT 2.0 W3C Recommendation]
* [http://www.w3.org/TR/xslt20/ XSLT 2.0 W3C Recommendation]
* [http://www.w3.org/TR/xslt-30/ XSLT 3.0 W3C Recommendation]
* [http://www.w3.org/TR/xslt-30/ XSLT 3.0 W3C Recommendation]
* [https://developer.mozilla.org/en/XSLT XSLT - MDC Docs] by [https://developer.mozilla.org Mozilla Developer Network]
* [https://developer.mozilla.org/en/XSLT XSLT - MDC Docs] {{Webarchive|url=https://web.archive.org/web/20111226235131/https://developer.mozilla.org/en/XSLT |date=2011-12-26 }} by [https://developer.mozilla.org Mozilla Developer Network]
* [http://msdn.microsoft.com/en-us/library/ms256069.aspx XSLT Reference (MSDN)]
* [http://msdn.microsoft.com/en-us/library/ms256069.aspx XSLT Reference (MSDN)]
* [http://saxon.sourceforge.net/saxon7.5/xsl-elements.html XSLT Elements (Saxon)]
* [http://saxon.sourceforge.net/saxon7.5/xsl-elements.html XSLT Elements (Saxon)]
Line 242: Line 260:


; XSLT code libraries
; XSLT code libraries
* [http://www.exslt.org/ EXSLT] is a widespread community initiative to provide extensions to XSLT.
* [http://exslt.github.io EXSLT] is a widespread community initiative to provide extensions to XSLT.
* [http://fxsl.sf.net/ FXSL] is a library implementing support for [[Higher-order function]]s in XSLT. FXSL is written in XSLT itself.
* [http://fxsl.sf.net/ FXSL] is a library implementing support for [[Higher-order function]]s in XSLT. FXSL is written in XSLT itself.
* [http://xsltsl.sourceforge.net/ 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.
* [http://xsltsl.sourceforge.net/ 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.
Line 254: Line 272:


{{DEFAULTSORT:Xsl Transformations}}
{{DEFAULTSORT:Xsl Transformations}}
[[Category:1998 software]]
[[Category:Articles with example code]]
[[Category:Declarative programming languages]]
[[Category:Declarative programming languages]]
[[Category:Functional languages]]
[[Category:Functional languages]]
[[Category:High-level programming languages]]
[[Category:Homoiconic programming languages]]
[[Category:Markup languages]]
[[Category:Markup languages]]
[[Category:Programming languages]]
[[Category:Programming languages created in 1998]]
[[Category:Transformation languages]]
[[Category:Transformation languages]]
[[Category:World Wide Web Consortium standards]]
[[Category:World Wide Web Consortium standards]]
[[Category:XML-based programming languages]]
[[Category:XML-based programming languages]]
[[Category:XML-based standards]]
[[Category:XML-based standards]]
[[Category:Articles with example code]]
[[Category:Programming languages created in 1998]]
[[Category:1998 software]]
[[Category:High-level programming languages]]
[[Category:Homoiconic programming languages]]

Revision as of 23:26, 27 February 2024

XSLT
ParadigmDeclarative
DeveloperWorld Wide Web Consortium (W3C)
First appeared1998
Stable release
3.0 / June 8, 2017; 7 years ago (2017-06-08)
Filename extensions.xslt
Websitewww.w3.org/TR/xslt-30/
Major implementations
libxslt, Saxon, Xalan
Influenced by
DSSSL
XSLT
Filename extension
.xslt
Internet media type
application/xslt+xml
Uniform Type Identifier (UTI)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.

As of August 2022, 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]

  • XSLT 2.0: after an abortive attempt to create a version 1.1 in 2001,[10] the XSL working group joined forces with the XQuery working group to create XPath 2.0,[11] with a richer data model and type system based on XML Schema. Building on this is XSLT 2.0,[12] developed under the editorship of Michael Kay, which reached recommendation status in January 2007.[13] 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 (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 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 (higher-order) functions.

Design and processing model

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 implemented imperative programming languages like C, XSLT is declarative.[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 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 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

The <output> 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[21] and it was later registered with the Internet Assigned Numbers Authority.[22]

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[23] 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.[24]

Examples

These examples use the following incoming XML document

<?xml version="1.0" ?>
<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>

Example 1 (transforming XML to XML)

This XSLT stylesheet provides templates to transform the XML document:

<?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>

Its evaluation results in a new XML document, having another structure:

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <name username="JS1">John</name>
  <name username="MI1">Morka</name>
</root>

Example 2 (transforming XML to XHTML)

Processing 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:output method="xml" indent="yes" encoding="UTF-8"/>

  <xsl:template match="/persons">
    <html>
      <head> <title>Testing XML Example</title> </head>
      <body>
        <h1>Persons</h1>
        <ul>
          <xsl:apply-templates select="person">
            <xsl:sort select="family-name" />
          </xsl:apply-templates>
        </ul>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="person">
    <li>
      <xsl:value-of select="family-name"/><xsl:text>, </xsl:text><xsl:value-of select="name"/>
    </li>
  </xsl:template>

</xsl:stylesheet>

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

<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml">
  <head> <title>Testing XML Example</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.

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]

<?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 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

  • 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.[26]
  • Microsoft provides two XSLT processors (both XSLT 1.0 only). The earlier processor MSXML provides 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 its System.Xml.Xsl library.
  • Saxon is an XSLT 3.0 and XQuery 3.1 processor with open-source and proprietary versions for stand-alone operation and for Java, JavaScript and .NET. A separate product Saxon-JS[39] offers XSLT 3.0 processing on Node.js and in the browser.
  • 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 (only). 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.[40]
  • Adobe AXSLE engine, a proprietary library

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.[41] 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)[42] 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.[43]

See also

References

  1. ^ a b "Transformation". 2012-09-19.
  2. ^ "XML Output Method". 2012-09-19.
  3. ^ "What is XSLT Used For?". 2018-02-07.
  4. ^ "Introduction". XSL Transformations (XSLT) Version 1.0 W3C Recommendation. W3C. 16 November 1999. Retrieved November 7, 2012.
  5. ^ XSLT Version 2.0 Is Turing-Complete: A Purely Transformation Based Proof
  6. ^ Michael Kay. "What kind of language is XSLT?". IBM. Retrieved July 8, 2016.
  7. ^ "A Proposal for XSL". W3C. Retrieved November 7, 2012.
  8. ^ "XML and Semantic Web W3C Standards Timeline" (PDF). Archived from the original (PDF) on 2013-04-24. Retrieved 2012-02-04.
  9. ^ "XSL Transformations (XSLT)". W3.org. 1999-11-16. Retrieved 2014-07-12.
  10. ^ "XSL Transformations (XSLT) Version 1.1". W3.org. 2001-08-24. Retrieved 2014-07-12.
  11. ^ "XML Path Language (XPath) 2.0 (Second Edition)". W3.org. 2010-12-14. Retrieved 2014-07-12.
  12. ^ "XSL Transformations (XSLT) Version 2.0". W3.org. 2007-01-23. Retrieved 2014-07-12.
  13. ^ "XML and Semantic Web W3C Standards Timeline" (PDF). 2012-02-04. Archived from the original (PDF) on 2013-04-24. Retrieved 2012-02-04.
  14. ^ "What's New in XSLT 3.0?". w3. Retrieved 6 January 2014.
  15. ^ Kay, Michael. "A Streaming XSLT Processor". Balisage: The Markup Conference 2010 Proceedings. Retrieved 15 February 2012.
  16. ^ 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, ISBN 978-3-031-14131-7, retrieved 2023-01-07
  17. ^ "XSL Transformations (XSLT) Version 2.0 (Second Edition)". www.w3.org. Retrieved 2023-02-07. Example: Multiple Result Documents
  18. ^ "Discover the Wonders of XSLT: XSLT Quirks". Archived from the original on 2011-07-09. Retrieved 2011-02-11. XSLT is a very specialized language with a distinct declarative flavor.
  19. ^ Kay, Michael. "What kind of language is XSLT?". IBM. Retrieved 13 November 2013.
  20. ^ "Saxonica: XSLT and XQuery". www.saxonica.com. Retrieved 2022-06-29.
  21. ^ "XSL Transformations (XSLT) Version 2.0". W3C. Retrieved 19 October 2012.
  22. ^ "Application Media Types". IANA. Retrieved 19 October 2012.
  23. ^ "XSLT Requirements for Viewing XML in a Browser". Microsoft. Retrieved 19 October 2012.
  24. ^ Kay, Michael (2008). XSLT 2.0 and XPath 2.0 Programmer's Reference. Wiley. p. 100. ISBN 978-0-470-19274-0.
  25. ^ a b "XSL Transformations (XSLT) Version 1.0: W3C Recommendation – Embedding Stylesheets". W3C. 16 November 1999. Retrieved 20 September 2016.
  26. ^ "The XSLT C library for GNOME: libxslt". Retrieved 23 November 2012.
  27. ^ "The XSLT C library for GNOME: The xsltproc tool". Retrieved 23 November 2012.
  28. ^ "xsltproc man page". Retrieved 23 November 2012.
  29. ^ "New package: libxslt". Retrieved 23 November 2012.
  30. ^ "The WebKit Open Source Project - XSLT". Retrieved 2009-10-25.
  31. ^ "The XML C parser and toolkit of Gnome: Python and bindings". Retrieved 23 November 2012.
  32. ^ "XML::LibXSLT - Interface to the GNOME libxslt library". CPAN. Retrieved 23 November 2012.
  33. ^ "libxslt-ruby". Retrieved 23 November 2012.
  34. ^ "libxml". Retrieved 23 November 2012.
  35. ^ "cl-libxml2 High-level wrapper around libxml2 and libxslt libraries".
  36. ^ "TclXML". Retrieved 21 May 2013.
  37. ^ "libxml++". sourceforge.net. Retrieved 23 November 2012.
  38. ^ "Command Line Transformation Utility (msxsl.exe)". Microsoft. Retrieved 22 October 2012.
  39. ^ "Saxon-JS". Saxonica. Retrieved 6 September 2018.
  40. ^ "Issue 58151: Fails to load xml file on local file system using XMLHttpRequest".
  41. ^ Saxon: Anatomy of an XSLT processor - Article describing implementation & optimization details of a popular XSLT processor.
  42. ^ Lumley, John; Kay, Michael (June 2015). "Improving Pattern Matching Performance in XSLT". XML London 2015: 9–25. doi:10.14337/XMLLondon15.Lumley01 (inactive 2024-02-27). ISBN 978-0-9926471-2-4.{{cite journal}}: CS1 maint: DOI inactive as of February 2024 (link)
  43. ^ Kay, Michael; Lockett, Debbie (June 2014). "Benchmarking XSLT Performance". XML London 2014: 10–23. doi:10.14337/XMLLondon14.Kay01 (inactive 2024-02-27). ISBN 978-0-9926471-1-7.{{cite journal}}: CS1 maint: DOI inactive as of February 2024 (link)

Further reading

Documentation
XSLT code libraries
  • EXSLT is a widespread community initiative to provide extensions to XSLT.
  • FXSL is a library implementing support for Higher-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.
  • xslt.js – Transform XML with XSLT JavaScript library that transforms XML with XSLT in the browser.