XSLT, XPath, and XQuery
XSLT, XPath, and XQuery
XSLT is developed by the W3C XSLT Working Group (members only) whose charter is to
develop the next version of XSLT. XSLT is part of W3C's XML Activity, whose work is
described in the XML Activity Statement.
❮ PreviousNext ❯
XSL (eXtensible Stylesheet Language) is a styling language for XML.
This tutorial will teach you how to use XSLT to transform XML documents into
other formats (like transforming XML into HTML).
XSLT Example
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
The language was designed primarily for writing stylesheets that enable XML
documents to be displayed to their readers. By writing more than one
stylesheet, you can display the same information in different ways to different
audiences, and tailor the presentation to the capabilities of different display
devices, including web browsers, conventional print media, handheld devices,
and digital TV.
By authoring your information in XML, and rendering it using XSLT, you separate
the tasks of the content author and the graphic designer, and you ensure that a
uniform design is achieved across all your content. This approach also allows
you to change the visual design without rewriting the content.
XSLT is also used increasingly for transforming data. For example, if you need to
send electronic orders to your suppliers, then the XML message used for this
purpose will have to conform to a schema agreed with the suppliers. If the data
starts life in an Excel spreadsheet, you can export it in XML using the built-in
capabilities of Excel, and then transform it to the format required by the agreed
schema, by means of an XSLT transformation.
XSLT 1.0 was defined by the World Wide Web Consortium (W3C) in 1999. Saxon
was one of the first implementations. Many others followed, including products
from major vendors such as Microsoft, Oracle, and IBM. Despite this competition,
the Saxon XSLT 1.0 processor was downloaded nearly 200,000 times, as well as
being bundled with many other software products. It featured regularly in the
list of the top 100 open-source software products on the SourceForge site.
XSLT 2.0 followed in 2007, and Saxon led the way in implementing the standard,
which offers immense improvements in functionality and productivity over the
original version. Many of the XSLT 1.0 implementations (for example, those from
Microsoft) dropped out of the race, confirming Saxon's leadership position. In
2012 Saxonica produced Saxon-CE, which was not only the first-ever
implementation of XSLT 2.0 able to run in the browser, but also the first to
handle user interaction as well as pure batch transformation.
The specifications for XSLT 2.0 reached the status of a W3C Recommendation in
January 2007, after a lengthy development process. Throughout this process,
regular new Saxon releases were produced implementing the new features of
successive drafts of the specification. This gave the user community the
opportunity to try out new features before they were frozen, and to give
feedback to the working groups developing the language. As editor of the
specification and developer of Saxon, Michael Kay was especially well-placed to
ensure that the users' voice was heard.
XSLT 2.0 added many new features desperately needed by existing users. These
include more powerful facilities for handling text (regular expressions) and
structured data (grouping), which greatly increase the range of tasks to which
XSLT can be applied, especially when converting legacy data and document
formats to XML.
One of the most significant additions was that XSLT became schema-aware. This
means that the XML Schema used to define the source and result documents of
a transformation can now be used to guide the compilation and execution of the
stylesheet. This makes stylesheet code more robust, it speeds the debugging
cycle, and it creates the potential for significant performance improvements.
Since XSLT 2.0 was finalized in 2007, the W3C Working Group has turned its
attention to the development of XSLT 3.0, and once again Saxon is in the lead
implementing new features as they emerge. The focus in XSLT 3.0 is on
streaming transformations (transforming large documents without first reading
them into memory in their entirety), and many of the new features to enable
this have been productized in the 9.x series of Saxon-EE releases.
It can be used to extract information from one or more XML documents, for
example:
All the major relational database vendors are adding XML support to their
existing products, and new Native XML Databases are also appearing. These
products all use XQuery to access the data.
Like XSLT, XQuery can also be used to transform XML messages from one
format to another. The language is less powerful than XSLT 2.0, but usability
studies have shown that it is easier for users to learn, and there are also
indications that it is easier for vendors to optimize.
XPath
XQuery and XSLT have much in common. Both languages make use of XPath, a
syntax for finding your way around the structure of an XML document and it can
also be used directly from programming languages such as Java and C#. It is
also used within other W3C languages such as XML Schema and XForms. Both
languages share the same data model and type system, and the same function
library, which means that the two languages can work together well in a single
application. For example, you can use XQuery to extract data from an XML
database, and XSLT to present the results to users on the web.
Functions Reference
Accessor AnyURI
Node
Error and Trace Boolean
Sequence
Numeric Duration/Date/Time
Context
String QName
Tip: Functions are often called with the fn: prefix, such as fn:string(). However,
since fn: is the default prefix of the namespace, the function names do not need
to be prefixed when called.
Accessor Functions
Name Description
fn:node-name(node) Returns the node-name of the argument node
Name Description
Name Description
Example: number('100')
Result: 100
Example: abs(3.14)
Result: 3.14
Example: abs(-3.14)
Result: 3.14
Example: ceiling(3.14)
Result: 4
fn:floor(num) Returns the largest integer that is not greater than the
number argument
Example: floor(3.14)
Result: 3
Example: round(3.14)
Result: 3
Example: round-half-to-even(2.5)
Result: 2
Functions on Strings
Name Description
fn:string(arg) Returns the string value of the argument. The argument could
be a number, boolean, or node-set
Example: string(314)
Result: "314"
Example: string-to-codepoints("Thérèse")
Result: (84, 104, 233, 114, 232, 115, 101)
Example:string-join((), 'sep')
Result: ''
fn:substring(string,start,len) Returns the substring from the start position to the specified
fn:substring(string,start) length. Index of the first character is 1. If length is omitted it
returns the substring from the start position to the end
Example: substring('Beatles',1,4)
Result: 'Beat'
Example: substring('Beatles',2)
Result: 'eatles'
Example: string-length('Beatles')
Result: 7
fn:normalize-space(string) Removes leading and trailing spaces from the specified
fn:normalize-space() string, and replaces all internal sequences of white space with
one and returns the result. If there is no string argument it
does the same on the current node
fn:normalize-unicode()
Example: translate('12:30','30','45')
Result: '12:45'
Example: translate('12:30','03','54')
Result: '12:45'
Example: translate('12:30','0123','abcd')
Result: 'bc:da'
Example: contains('XML','XM')
Result: true
Example: starts-with('XML','X')
Result: true
Example: ends-with('XML','X')
Result: false
Example: substring-before('12/10','/')
Result: '12'
Example: substring-after('12/10','/')
Result: '10'
Name Description
fn:resolve-uri(relative,base)
Name Description
Example: not(true())
Result: false
Example: true()
Result: true
Name Description
Example: year-from-dateTime(xs:dateTime("2005-01-10T12:30-
04:10"))
Result: 2005
Example: month-from-dateTime(xs:dateTime("2005-01-10T12:30-
04:10"))
Result: 01
Example: day-from-dateTime(xs:dateTime("2005-01-10T12:30-
04:10"))
Result: 10
Example: hours-from-dateTime(xs:dateTime("2005-01-10T12:30-
04:10"))
Result: 12
Example: minutes-from-dateTime(xs:dateTime("2005-01-
10T12:30-04:10"))
Result: 30
Example: seconds-from-dateTime(xs:dateTime("2005-01-
10T12:30:00-04:10"))
Result: 0
fn:year-from-date(date) Returns an integer that represents the year in the localized value
of the argument
Example: year-from-date(xs:date("2005-04-23"))
Result: 2005
Example: month-from-date(xs:date("2005-04-23"))
Result: 4
fn:day-from-date(date) Returns an integer that represents the day in the localized value
of the argument
Example: day-from-date(xs:date("2005-04-23"))
Result: 23
Example: hours-from-time(xs:time("10:22:00"))
Result: 10
Example: minutes-from-time(xs:time("10:22:00"))
Result: 22
fn:seconds-from-time(time) Returns an integer that represents the seconds component in the
localized value of the argument
Example: seconds-from-time(xs:time("10:22:00"))
Result: 0
Name Description
fn:QName()
fn:local-name-from-QName()
fn:namespace-uri-from-QName()
fn:namespace-uri-for-prefix()
fn:in-scope-prefixes()
fn:resolve-QName()
Functions on Nodes
Name Description
fn:name() Returns the name of the current node or the first node in
fn:name(nodeset) the specified node set
fn:local-name() Returns the name of the current node or the first node in
fn:local- the specified node set - without the namespace prefix
name(nodeset)
fn:namespace-uri() Returns the namespace URI of the current node or the first
fn:namespace- node in the specified node set
uri(nodeset)
fn:root() Returns the root of the tree to which the current node or the
fn:root(node) specified belongs. This will usually be a document node
Functions on Sequences
General Functions on Sequences
Name Description
fn:index-of((item,item,...),searchitem) Returns the positions within the sequence of items that are
equal to the searchitem argument
fn:remove((item,item,...),position) Returns a new sequence constructed from the value of the item
arguments - with the item specified by the position argument
removed
fn:insert- Returns a new sequence constructed from the value of the item
before((item,item,...),pos,inserts) arguments - with the value of the inserts argument inserted in
the position specified by the pos argument
Example: reverse(("ab"))
Result: ("ab")
Name Description
Name Description
Aggregate Functions
Name Description
Example: avg((1,2,3))
Result: 2
Example: max((1,2,3))
Result: 3
Example: min((1,2,3))
Result: 1
Name Description
fn:doc(URI)
fn:collection()
fn:collection(string)
Context Functions
Name Description
Example: //book[position()<=3]
Result: Selects the first three book elements
Example: //book[last()]
Result: Selects the last book element
XSLT Functions
In addition, there are the following built-in XSLT functions:
Name Description