XML technologies / XSLT / Introduction to XSLT 2.0 / XSLT and XPath – getting started / The root element xsl:stylesheet
(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 1.)
Examine the stylesheet more closely. Its XML prolog clearly identifies it as an XML document:
<?xml version="1.0" encoding="ISO-8859-1"?>The version attribute in the prolog specifies the XML version. Its value is 1.0 as usual, though 1.1 for XML 1.1 is theoretically allowed but uncommon. The prolog also has an encoding attribute declaring document encoding ISO-8859-1.
Root element xsl:stylesheet and its namespaces
After the prolog comes the stylesheet’s root element, xsl:stylesheet. This xsl:stylesheet also has a version attribute, but this gives the XSLT version used.
<xsl:stylesheet version="2.0" ... >For XSLT 2.0 its value is 2.0. Currently the only other allowed value is 1.0 for XSLT 1.0, since interim XSLT 1.1 was withdrawn.
Attribute xmlns declares a namespace. A freely chosen string follows its name after a colon and acts as a prefix identifying elements assigned the declared namespace. For XSLT the customary prefix is xsl, giving xmlns:xsl.
<xsl:stylesheet version="2.0" xmlns:xsl="...">
...
</xsl:stylesheet>
Note that even the root xsl:stylesheet already uses this prefix xsl although declaring it there. This is correct: a namespace applies throughout the element declaring it and to that element itself.
The purpose of a namespace
A namespace unambiguously assigns an XML element to a group or markup language. It prevents duplicate-name conflicts when processing documents from different sources together. It also identifies an application’s key concepts as belonging to a semantic domain, such as “XML Schema”, “Formatting Objects”, “SVG”, “MathML” or “XSLT”.
It uses a unique string, like an identity-card number. URI strings are ideal for uniqueness, so namespaces use them as identifiers, hence the term namespace URI.
A namespace URI need not identify an existing resource:
A common misconception is that this URI must identify a real web resource. It need not: only the unique string matters. XSLT transformation therefore requires no internet connection.
For XSLT the URI is fixed and must be exactly "http://www.w3.org/1999/XSL/Transform". Only this string identifies an element in that namespace to the processing application, the XSLT processor, as an XSLT instruction.
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
...
<!-- Der XSLT-Namensraum gilt hier weiter: -->
<xsl:template> ... </xsl:template>
...
</xsl:stylesheet>
The prefix merely abbreviates that URI. Although unusual, any other prefix could replace xsl.
The root element’s full name xsl:stylesheet therefore has two parts: its local name stylesheet and its prefix part xsl. Joined by a colon, they form the element’s qualified name, its QName.
Only the namespace URI matters during processing:
Internally, applications use expanded QNames. The namespace URI matters, never the prefix. Two names are equal when their local names and namespace URIs match; their unexpanded prefixes are irrelevant.
If one URI is bound to two prefixes, as may occur with two source documents, apparently different QNames are therefore equal to the processor.
An expanded QName example
The stylesheet element’s unexpanded QName is
xsl:stylesheet, as written in the source.Internally, its expanded form is
{http://www.w3.org/1999/XSL/Transform}:stylesheet.
The processor thus effectively ignores the prefix while processing but still knows it. This matters when converting URIs back into prefixes during result serialization.
Distinguishing XSLT 1.0 and 2.0:
Namespace URI "http://www.w3.org/1999/XSL/Transform" is identical for XSLT 1.0 and 2.0. Only the version attribute on xsl:stylesheet distinguishes versions and indirectly selects the XPath version too.
Other useful namespace declarations
Besides the required XSLT namespace, declare in xsl:stylesheet two further namespaces, though we will not yet need them:
The XPath function namespace:
xmlns:fn="http://www.w3.org/2005/xpath-functions"This identifies functions usable in stylesheet XPath expressions, marked with prefix
fn:fn:position()The namespace for XML Schema data types and constructors:
xmlns:xs="http://www.w3.org/2001/XMLSchema"This identifies XML Schema data types and constructors creating instances of those types:
xs:date()constructs a date type.
More completely, write the stylesheet element as follows:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xs="http://www.w3.org/2001/XMLSchema">
...
<!-- Hier ist jetzt auch der Namensraum für XPath-Funktionen und XML Schema bekannt. -->
...
</xsl:stylesheet>
Summary of xsl:stylesheet
The stylesheet root is either
xsl:stylesheetor, rarely but equivalently,xsl:transform.The root must declare the XSLT version and namespaces, at least XSLT’s own. Version “2.0” selects XSLT 2.0.
Declaring XPath-function and schema-type namespaces identifies functions and types with prefixes
fnandxs. Some processors require this.Namespaces declared here are available throughout the stylesheet. Their elements use the declared prefix for identification.
The XSLT namespace identifies stylesheet instructions, including the root itself, which also uses prefix
xsl.
The figure shows some still-unfamiliar structures inside the root. We will examine them shortly. First, consider element xsl:template.
Figure: Basic structure of an XSLT 2.0 stylesheet.
Copyright © Galileo Press, Bonn 2008
You may print the online version for your private use.
Otherwise, this chapter from “XSLT 2.0 & XPath 2.0 ― Das umfassende Handbuch” is subject to the same conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
Galileo Press, Rheinwerkallee 4, 53227 Bonn