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 arbitrary data. It defines a set of rules for encoding documents in a format that is both human-readable and machine-readable ...
documents into other XML documents,
or other formats such as
HTML
The HyperText Markup Language or HTML is the standard markup language for documents designed to be displayed in a web browser. It can be assisted by technologies such as Cascading Style Sheets (CSS) and scripting languages such as JavaSc ...
for
web pages,
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 limit ...
or
XSL Formatting Objects
XSL-FO (XSL Formatting Objects) is a markup language for XML document formatting that is most often used to generate PDF files. XSL-FO is part of Extensible Stylesheet Language, XSL (Extensible Stylesheet Language), a set of W3C technologies design ...
, which may subsequently be converted to other formats, such as
PDF,
PostScript and
PNG. 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.
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. Typically, input documents are XML files, but anything from which the processor can build an
XQuery and XPath Data Model
The XQuery and XPath Data Model (XDM) is the data model shared by the XPath 2.0, XSLT 2.0, XQuery, and XForms programming languages. It is defined in a W3C recommendation.Anders Berglund, ''et al.''XQuery 1.0 and XPath 2.0 Data Model W3C, 2010, r ...
can be used, such as
relational database tables or
geographical information systems
A geographic information system (GIS) is a type of database containing geographic data (that is, descriptions of phenomena for which location is relevant), combined with software tools for managing, analyzing, and visualizing those data. In a b ...
.
While XSLT was originally designed as a special-purpose language for XML transformation, the language is
Turing-complete
In computability theory, a system of data-manipulation rules (such as a computer's instruction set, a programming language, or a cellular automaton) is said to be Turing-complete or computationally universal if it can be used to simulate any ...
, making it theoretically capable of arbitrary computations.
History
XSLT is influenced by
functional languages
In computer science, functional programming is a programming paradigm where programs are constructed by applying and composing functions. It is a declarative programming paradigm in which function definitions are trees of expressions that m ...
, 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 E. Griswold and Ivan P. Polonsky, culminating in SNOBOL4. It was one of ...
and
AWK
AWK (''awk'') is a domain-specific language designed for text processing and typically used as a data extraction and reporting tool. Like sed and grep, it is a filter, and is a standard feature of most Unix-like operating systems.
The AWK lang ...
. Its most direct predecessor is
DSSSL
The Document Style Semantics and Specification Language (DSSSL) is an international standard developed to provide stylesheets for SGML documents.
DSSSL consists of two parts: a tree transformation process that can be used to manipulate the tree ...
, which did for
SGML
The Standard Generalized Markup Language (SGML; 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 two postulates":
* Declarative: Markup should des ...
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 and led by Tim Berners-Lee, the consortium is made up of member organizations that maintain full-time staff working to ...
(W3C)'s
eXtensible Stylesheet Language
In computing, the term Extensible Stylesheet Language (XSL) is used to refer to a family of
languages used to transform and render XML documents.
Historically, the W3C XSL Working Group produced a draft specification under the name "XSL," which ...
(XSL) development effort of 1998–1999, a project that also produced
XSL-FO
XSL-FO (XSL Formatting Objects) is a markup language for XML document formatting that is most often used to generate PDF files. XSL-FO is part of XSL (Extensible Stylesheet Language), a set of W3C technologies designed for the transformation and ...
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) and can be used to compute values (e.g., strings, numbers, or Boolean v ...
. 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 and led by Tim Berners-Lee, the consortium is made up of member organizations that maintain full-time staff working to ...
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 is application software 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 screen. Browsers are used on ...
s or for environments like
LAMP
Lamp, Lamps or LAMP may refer to:
Lighting
* Oil lamp, using an oil-based fuel source
* Kerosene lamp, using kerosene as a fuel
* Electric lamp, or light bulb, a replaceable component that produces light from electricity
* Light fixture, or li ...
.
* XSLT 2.0: after an abortive attempt to create a version 1.1 in 2001, the XSL working group joined forces with the
XQuery
XQuery (XML Query) is a query and functional programming language that queries and transforms collections of structured and unstructured data, usually in the form of XML, text and with vendor-specific extensions for other data formats (JSON, bin ...
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 Extensible Markup Language, 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 ...
. 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 characters that specifies a search pattern in text. Usually such patterns are used by string-searching algorithms for "find" or ...
** 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 an output document. In contrast to widely implemented
imperative programming
In computer science, imperative programming is a programming paradigm of software that uses statements that change a program's state. In much the same way that the imperative mood in natural languages expresses commands, an imperative program c ...
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
Functional may refer to:
* Movements in architecture:
** Functionalism (architecture)
** Form follows function
* Functional group, combination of atoms within molecules
* Medical conditions without currently visible organic basis:
** Functional sy ...
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, including only woody plants with secondary growth, plants that are ...
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) and can be used to compute values (e.g., strings, numbers, or Boolean v ...
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
XQuery (XML Query) is a query and functional programming language that queries and transforms collections of structured and unstructured data, usually in the form of XML, text and with vendor-specific extensions for other data formats (JSON, bin ...
, 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 and led by Tim Berners-Lee, the consortium is made up of member organizations that maintain full-time staff working to ...
, 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) and can be used to compute values (e.g., strings, numbers, or Boolean v ...
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
The World Wide Web (WWW), commonly known as the Web, is an information system enabling documents and other web resources to be accessed over the Internet.
Documents and downloadable media are made available to the network through web se ...
(as a
web template language), or on paper. XQuery was primarily conceived as a
database query language
Query languages, data query languages or database query languages (DQL) are computer languages used to make queries in databases and information systems. A well known example is the Structured Query Language (SQL).
Types
Broadly, 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
A media type (also known as a MIME type) is a two-part identifier for file formats and format contents transmitted on the Internet. The Internet Assigned Numbers Authority (IANA) is the official authority for the standardization and publication o ...
(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 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 number allocation, root zone management in the Domain Name System (DNS), media types, and other Interne ...
.
Pre-1.0 working drafts of XSLT used
text/xsl
in their embedding examples, and this type was implemented and continues to be promoted by Microsoft in Internet Explorer and MSXML. 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 are 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. It mirrors or extends versions of the widely used HyperText Markup Language (HTML), the language in which Web pages are formulated.
While HTML, prior ...
(
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
Altova is a commercial software development company with headquarters in Beverly, MA, United States and Vienna, Austria that produces integrated XML, JSON, database, UML, and data management software development tools.
Company
Altova was founded ...
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
libxslt is the XSLT C library developed for the GNOME project. It provides an implementation of XSLT 1.0, plus most of the EXSLT set of processor-portable extensions functions and some of Saxon's evaluate and expressions extensions. libxslt i ...
is a
free library
A library is a collection of materials, books or media that are accessible for use and not just for display purposes. A library provides physical (hard copies) or digital access (soft copies) materials, and may be a physical location or a vir ...
released under the
MIT License
The MIT License is a permissive free software license originating at the Massachusetts Institute of Technology (MIT) in the late 1980s. As a permissive license, it puts only very limited restriction on reuse and has, therefore, high license comp ...
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
EXSLT is a community initiative to provide extensions to XSLT, which are broken down into a number of modules, listed below.
The creators (Jeni Tennison, Uche Ogbuji, Jim Fuller, Dave Pawson, et al.) of EXSLT aim to encourage the implementers of ...
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 operating system developed and marketed by Apple Inc. since 2001. It is the primary operating system for Apple's Mac computers. Within the market of desktop and lapt ...
and many
Linux distributions
A Linux distribution (often abbreviated as distro) is an operating system made from a software collection that includes the Linux kernel and, often, a package management system. Linux users usually obtain their operating system by downloading one ...
, and can be used on
Windows
Windows is a group of several proprietary graphical operating system families developed and marketed by Microsoft. Each family caters to a certain sector of the computing industry. For example, Windows NT for consumers, Windows Server for serv ...
via
Cygwin
Cygwin ( ) is a POSIX-compatible programming and runtime environment that runs natively on Microsoft Windows. Under Cygwin, source code designed for Unix-like operating systems may be compiled with minimal modification and executed.
The Cygwin in ...
.
**The
WebKit
WebKit is a browser engine developed by Apple and primarily used in its Safari web browser, as well as on the iOS and iPadOS version of any web browser. WebKit is also used by the BlackBerry Browser, PlayStation consoles beginning from the PS ...
and
Blink
Blinking is a bodily function; it is a semi-autonomic rapid closing of the eyelid. A single blink is determined by the forceful closing of the eyelid or inactivation of the levator palpebrae superioris and the activation of the palpebral portio ...
layout engines, used for example in the
Safari
A safari (; ) is an overland journey to observe wild animals, especially in eastern or southern Africa. The so-called "Big Five" game animals of Africa – lion, leopard, rhinoceros, elephant, and Cape buffalo – particularly form an importa ...
and
Chrome web browsers respectively, uses the libxslt library to do XSL transformations.
**
Bindings exist for
Python
Python may refer to:
Snakes
* Pythonidae, a family of nonvenomous snakes found in Africa, Asia, and Australia
** ''Python'' (genus), a genus of Pythonidae found in Africa and Asia
* Python (mythology), a mythical serpent
Computing
* Python (pro ...
,
Perl
Perl is a family of two high-level, general-purpose, interpreted, dynamic programming languages. "Perl" refers to Perl 5, but from 2000 to 2019 it also referred to its redesigned "sister language", Perl 6, before the latter's name was offici ...
,
Ruby
A 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 sa ...
,
PHP
PHP is a general-purpose scripting language geared toward web development. It was originally created by Danish-Canadian programmer Rasmus Lerdorf in 1993 and released in 1995. The PHP reference implementation is now produced by The PHP Group ...
,
Common Lisp
Common Lisp (CL) is a dialect of the Lisp programming language, published in ANSI standard document ''ANSI INCITS 226-1994 (S20018)'' (formerly ''X3.226-1994 (R1999)''). The Common Lisp HyperSpec, a hyperlinked HTML version, has been derived fro ...
,
Tcl
TCL or Tcl or TCLs may refer to:
Business
* TCL Technology, a Chinese consumer electronics and appliance company
**TCL Electronics, a subsidiary of TCL Technology
* Texas Collegiate League, a collegiate baseball league
* Trade Centre Limited ...
, and
C++
C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
.
*
Microsoft
Microsoft Corporation is an American multinational technology corporation producing computer software, consumer electronics, personal computers, and related services headquartered at the Microsoft Redmond campus located in Redmond, Washing ...
provides two XSLT processors (both XSLT 1.0 only). The earlier processor
MSXML
Microsoft XML Core Services (MSXML) are set of services that allow applications written in JScript, VBScript, and Microsoft development tools to build Windows-native XML-based applications. It supports XML 1.0, DOM, SAX, an XSLT 1.0 processor, ...
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
The Saxons ( la, Saxones, german: Sachsen, ang, Seaxan, osx, Sahson, nds, Sassen, nl, Saksen) were a group of Germanic
*
*
*
*
peoples whose name was given in the early Middle Ages to a large country (Old Saxony, la, Saxonia) near the Nor ...
is an XSLT 3.0 and XQuery 3.1 processor with
open-source
Open source is source code that is made freely available for possible modification and redistribution. Products include permission to use the source code, design documents, or content of the product. The open-source model is a decentralized sof ...
and