XSLT (Extensible Stylesheet Language Transformations) is a language originally designed for
transforming XML
Extensible Markup Language (XML) is a markup language and file format for storing, transmitting, and reconstructing data. It defines a set of rules for encoding electronic document, documents in a format that is both human-readable and Machine-r ...
documents into other XML documents,
or other formats such as
HTML
Hypertext Markup Language (HTML) is the standard markup language for documents designed to be displayed in a web browser. It defines the content and structure of web content. It is often assisted by technologies such as Cascading Style Sheets ( ...
for
web page
A web page (or webpage) is a World Wide Web, Web document that is accessed in a web browser. A website typically consists of many web pages hyperlink, linked together under a common domain name. The term "web page" is therefore a metaphor of pap ...
s,
plain text
In computing, plain text is a loose term for data (e.g. file contents) that represent only characters of readable material but not its graphical representation nor other objects ( floating-point numbers, images, etc.). It may also include a lim ...
, or
XSL Formatting Objects. These formats can be subsequently converted to formats such as
PDF,
PostScript
PostScript (PS) is a page description language and dynamically typed, stack-based programming language. It is most commonly used in the electronic publishing and desktop publishing realm, but as a Turing complete programming language, it c ...
, and
PNG. Support for JSON and plain-text transformation was added in later updates to the XSLT 1.0 specification.
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.
The XSLT document transformation specifies how to transform an XML document into new document (usually XML, but other formats, such as plain text are supported). 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
A relational database (RDB) is a database based on the relational model of data, as proposed by E. F. Codd in 1970.
A Relational Database Management System (RDBMS) is a type of database management system that stores data in a structured for ...
tables or
geographical information systems.
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.
History
XSLT is influenced by
functional languages, and by text-based pattern matching languages like
SNOBOL
SNOBOL ("StriNg Oriented and symBOlic Language") is a series of programming languages developed between 1962 and 1967 at AT&T Bell Laboratories by David J. Farber, Ralph Griswold and Ivan P. Polonsky, culminating in SNOBOL4. It was one of a ...
and
AWK. Its most direct predecessor is
DSSSL, which did for
SGML
The Standard Generalized Markup Language (SGML; International Organization for Standardization, ISO 8879:1986) is a standard for defining generalized markup languages for documents. ISO 8879 Annex A.1 states that generalized markup is "based on t ...
what XSLT does for XML.
* XSLT 1.0: XSLT was part of the
World Wide Web Consortium
The World Wide Web Consortium (W3C) is the main international standards organization for the World Wide Web. Founded in 1994 by Tim Berners-Lee, the consortium is made up of member organizations that maintain full-time staff working together in ...
(W3C)'s
eXtensible Stylesheet Language (XSL) development effort of 1998–1999, a project that also produced
XSL-FO and
XPath
XPath (XML Path Language) is an expression language designed to support the query or transformation of XML documents. It was defined by the World Wide Web Consortium (W3C) in 1999, and can be used to compute values (e.g., strings, numbers, or ...
. Some members of the standards committee that developed XSLT, including
James Clark, the editor, had previously worked on DSSSL. XSLT 1.0 was published as a
W3C recommendation
The World Wide Web Consortium (W3C) is the main international standards organization for the World Wide Web. Founded in 1994 by Tim Berners-Lee, the consortium is made up of member organizations that maintain full-time staff working together in ...
in November 1999.
Despite its age, XSLT 1.0 is still widely used (), since later versions are not supported natively in
web browser
A web browser, often shortened to browser, is an application for accessing websites. When a user requests a web page from a particular website, the browser retrieves its files from a web server and then displays the page on the user's scr ...
s or for environments like
LAMP.
* XSLT 2.0: after an abortive attempt to create a version 1.1 in 2001, the XSL working group joined forces with the
XQuery working group to create
XPath 2.0, with a richer data model and type system based on
XML Schema
An XML schema is a description of a type of XML document, typically expressed in terms of constraints on the structure and content of documents of that type, above and beyond the basic syntactical constraints imposed by XML itself. These constrai ...
. Building on this is XSLT 2.0, developed under the editorship of
Michael Kay, which reached recommendation status in January 2007. The most important innovations in XSLT 2.0 include:
** String manipulation using
regular expressions
A regular expression (shortened as regex or regexp), sometimes referred to as rational expression, is a sequence of character (computing), characters that specifies a pattern matching, match pattern in string (computer science), text. Usually ...
** 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:
**
Streaming transformations: in previous versions the entire input document had to be read into memory before it could be processed, and output could not be written until processing had finished. XSLT 3.0 allows
XML streaming which is useful for processing documents too large to fit in memory or when transformations are chained in
XML Pipelines.
** Packages, to improve the modularity of large stylesheets.
** Improved handling of dynamic errors with, for example, an xsl:try instruction.
** Support for maps and arrays, enabling XSLT to handle JSON as well as XML.
** Functions can now be arguments to other (higher-order) functions.
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. In contrast to widely implemented
imperative programming
In computer science, imperative programming is a programming paradigm of software that uses Statement (computer science), statements that change a program's state (computer science), state. In much the same way that the imperative mood in natural ...
languages like
C, XSLT is
declarative. The basic processing paradigm is pattern matching.
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
In botany, a tree is a perennial plant with an elongated stem, or trunk, usually supporting branches and leaves. In some usages, the definition of a tree may be narrower, e.g., including only woody plants with secondary growth, only ...
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
XPath (XML Path Language) is an expression language designed to support the query or transformation of XML documents. It was defined by the World Wide Web Consortium (W3C) in 1999, and can be used to compute values (e.g., strings, numbers, or ...
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
The World Wide Web Consortium (W3C) is the main international standards organization for the World Wide Web. Founded in 1994 by Tim Berners-Lee, the consortium is made up of member organizations that maintain full-time staff working together in ...
, working together to ensure a common approach where appropriate. They share the same data model, type system, and function library, and both include
XPath
XPath (XML Path Language) is an expression language designed to support the query or transformation of XML documents. It was defined by the World Wide Web Consortium (W3C) in 1999, and can be used to compute values (e.g., strings, numbers, or ...
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.
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:
. 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
and it was later registered with the
Internet Assigned Numbers Authority
The Internet Assigned Numbers Authority (IANA) is a standards organization that oversees global IP address allocation, Autonomous system (Internet), autonomous system number allocation, DNS root zone, root zone management in the Domain Name Syste ...
.
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 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.
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
Extensible HyperText Markup Language (XHTML) is part of the family of XML markup languages which mirrors or extends versions of the widely used HyperText Markup Language (HTML), the language in which Web pages are formulated.
While HTML, pr ...
(
whitespace has been adjusted here for clarity):
Testing XML Example
Persons
- Ismincius, Morka
- Smith, John
This XHTML generates the output below when rendered in a web browser.
In order for a web browser to be able to apply an XSL transformation to an XML document on display, an XML stylesheet processing instruction can be inserted into XML. So, for example, if the stylesheet in Example 2 above were available as "example2.xsl", the following instruction could be added to the original incoming XML:
In this example,
text/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
*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
International Business Machines Corporation (using the trademark IBM), nicknamed Big Blue, is an American Multinational corporation, multinational technology company headquartered in Armonk, New York, and present in over 175 countries. It is ...
offers XSLT processing embedded in a special-purpose hardware appliance under the
Datapower brand.
*
libxslt is a
free library
A library is a collection of Book, books, and possibly other Document, materials and Media (communication), media, that is accessible for use by its members and members of allied institutions. Libraries provide physical (hard copies) or electron ...
released under the
MIT License
The MIT License is a permissive software license originating at the Massachusetts Institute of Technology (MIT) in the late 1980s. As a permissive license, it puts very few restrictions on reuse and therefore has high license compatibility.
Unl ...
that can be reused in commercial applications. It is based on
libxml and implemented in
C for speed and portability. It supports XSLT 1.0 and
EXSLT extensions.
**It can be used at the command line via xsltproc which is included in
macOS
macOS, previously OS X and originally Mac OS X, is a Unix, Unix-based operating system developed and marketed by Apple Inc., Apple since 2001. It is the current operating system for Apple's Mac (computer), Mac computers. With ...
and many
Linux distributions, and can be used on
Windows
Windows is a Product lining, product line of Proprietary software, proprietary graphical user interface, graphical operating systems developed and marketed by Microsoft. It is grouped into families and subfamilies that cater to particular sec ...
via
Cygwin.
**The
WebKit
WebKit is a browser engine primarily used in Apple's Safari web browser, as well as all web browsers on iOS and iPadOS. WebKit is also used by the PlayStation consoles starting with the PS3, the Tizen mobile operating systems, the Amazon K ...
and
Blink layout engines, used for example in the
Safari
A safari (; originally ) is an overland journey to observe wildlife, wild animals, especially in East Africa. The so-called big five game, "Big Five" game animals of Africa – lion, African leopard, leopard, rhinoceros, African elephant, elep ...
and
Chrome web browsers respectively, uses the libxslt library to do XSL transformations.
**
Bindings exist for
Python,
Perl
Perl is a high-level, general-purpose, interpreted, dynamic programming language. Though Perl is not officially an acronym, there are various backronyms in use, including "Practical Extraction and Reporting Language".
Perl was developed ...
,
Ruby
Ruby is a pinkish-red-to-blood-red-colored gemstone, a variety of the mineral corundum ( aluminium oxide). Ruby is one of the most popular traditional jewelry gems and is very durable. Other varieties of gem-quality corundum are called sapph ...
,
PHP,
Common Lisp
Common Lisp (CL) is a dialect of the Lisp programming language, published in American National Standards Institute (ANSI) standard document ''ANSI INCITS 226-1994 (S2018)'' (formerly ''X3.226-1994 (R1999)''). The Common Lisp HyperSpec, a hyperli ...
,
Tcl, and
C++.
*
Microsoft
Microsoft Corporation is an American multinational corporation and technology company, technology conglomerate headquartered in Redmond, Washington. Founded in 1975, the company became influential in the History of personal computers#The ear ...
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
. 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
Java is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea (a part of Pacific Ocean) to the north. With a population of 156.9 million people (including Madura) in mid 2024, proje ...
,
JavaScript
JavaScript (), often abbreviated as JS, is a programming language and core technology of the World Wide Web, alongside HTML and CSS. Ninety-nine percent of websites use JavaScript on the client side for webpage behavior.
Web browsers have ...
and .NET. A separate product Saxon-JS offers XSLT 3.0 processing on
Node.js and in the browser.
xjsltis an
open-source XSLT 2.0 compiler for
JavaScript
JavaScript (), often abbreviated as JS, is a programming language and core technology of the World Wide Web, alongside HTML and CSS. Ninety-nine percent of websites use JavaScript on the client side for webpage behavior.
Web browsers have ...
supporting
Node.js and the browser.
*
Xalan is an open source XSLT 1.0 processor from the
Apache Software Foundation
The Apache Software Foundation ( ; ASF) is an American nonprofit corporation (classified as a 501(c)(3) organization in the United States) to support a number of open-source software projects. The ASF was formed from a group of developers of the ...
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.
*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
Java bytecode is the instruction set of the Java virtual machine (JVM), the language to which Java and other JVM-compatible source code is compiled. Each instruction is represented by a single byte, hence the name bytecode, making it a compact ...
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. 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
XPath (XML Path Language) is an expression language designed to support the query or transformation of XML documents. It was defined by the World Wide Web Consortium (W3C) in 1999, and can be used to compute values (e.g., strings, numbers, or ...
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
Memory footprint refers to the amount of main memory that a program uses or references while running.
The word footprint generally refers to the extent of physical dimensions that an object occupies, giving a sense of its size. In computing, t ...
of intermediate results (and allow "early exit" when the processor can evaluate an expression such as
following-sibling::* /code> without a complete evaluation of all subexpressions). Many processors also use tree representations that are significantly more efficient (in both space and time) 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.
See also
* 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 objects or ''XSL-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 – a Java
Java is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea (a part of Pacific Ocean) to the north. With a population of 156.9 million people (including Madura) in mid 2024, proje ...
-based framework for processing data with XSLT and other transformers.
References
Further reading
* ''XSLT'' by Doug Tidwell, published by O’Reilly ()
* ''XSLT Cookbook'' by Sal Mangano, published by O’Reilly ()
* ''XSLT 2.0 Programmer's Reference'' by Michael Kay ()
* ''XSLT 2.0 and XPath 2.0 Programmer's Reference'' by Michael Kay ()
* ''XSLT 2.0 Web Development'' by Dmitry Kirsanov ()
* ''XSL Companion, 2nd Edition'' by Neil Bradley, published by Addison-Wesley ()
* ''XSLT and XPath on the Edge (Unlimited Edition)'' by Jeni Tennison, published by Hungry Minds Inc, U.S. ()
* ''XSLT & XPath, A Guide to XML Transformations'' by John Robert Gardner and Zarella Rendon, published by Prentice-Hall ()
* ''XSL-FO'' by Dave Pawson, published by O'Reilly ()
External links
; Documentation
XSLT 1.0 W3C Recommendation
XSLT 2.0 W3C Recommendation
XSLT 3.0 W3C Recommendation
*
XSLT Reference (MSDN)
XSLT introduction and reference
; XSLT code libraries
EXSLT
is a widespread community initiative to provide extensions to XSLT.
FXSL
is a library implementing support for Higher-order function In mathematics and computer science, a higher-order function (HOF) is a function that does at least one of the following:
* takes one or more functions as arguments (i.e. a procedural parameter, which is a parameter of a procedure that is itself ...
s 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.
{{DEFAULTSORT:Xsl Transformations
1998 software
Articles with example code
Declarative programming languages
Functional languages
High-level programming languages
Homoiconic programming languages
Markup languages
Programming languages
Programming languages created in 1998
Transformation languages
World Wide Web Consortium standards
XML-based programming languages
XML-based standards