XML technologies / XSLT / Reference: XSLT 1.0 and 2.0 elements / stylesheet

(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 6.)

A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z

 

The element xsl:stylesheet defines the body of an XSLT stylesheet module. It establishes global attribute defaults and namespaces valid throughout its scope. The version attribute of xsl:stylesheet also identifies the XSLT version underlying the module.

Classification

Root element

Function group

Root element of an XSLT stylesheet

Introduced in

XSLT 1.0

Stylesheet position and permitted contents:

The xsl:stylesheet element, together with xsl:transform, belongs to the root-element group and serves as an XSLT stylesheet’s root. It therefore has no parent element.

The xsl:stylesheet element contains the entire stylesheet body as XSLT top-level elements, such as template rules and other declarations, which may themselves contain sequence constructors. Extension instructions, user-defined data elements, comments and whitespace text nodes are also permitted.

Attributes:

The root element xsl:stylesheet uses standard attributes, described again below where their meanings differ here. Besides the required version attribute, optional attributes control default stylesheet and instruction behavior for validation and namespaces.

The stylesheet element also usually uses xmlns pseudoattributes for global namespace definitions applying throughout the module.

default-collation (also a standard attribute)

Value

uri-list

Usage

Optional

Introduced in

XSLT 2.0

XPath expressions always use a collation for string comparisons or sorting, normally the “Unicode Codepoint Collation”, which compares the compared characters’ code points.

The default-collation attribute specifies an alternative collation for XPath expressions within the marked element’s scope, identified by a URI string.

Multiple space-separated collation URIs may be supplied because recognizing collations depends heavily on the implementation: the processor must scan the list until it recognizes one. Put Unicode Codepoint Collation last, since it is always supported, avoiding an error.

It also affects the instructions xsl:key and xsl:for-each-group within the attribute’s scope, but not xsl:sort, which always uses the default collation.

Note: XPath 1.0 always compares code points; another comparison or sorting rule cannot be chosen.

Note: This explanation also applies to the same-named attribute of xsl:transform. The attribute does not exist in XSLT 1.0.

default-validation

Value

"preserve" | "strip"

Usage

Optional

Introduced in

XSLT 2.0

The default-validation attribute sets default behavior for validation during processing of this module unless a more explicit local validation attribute overrides it. (Note: This never applies to document nodes or temporary trees created with xsl:message, regardless of the presence or value of the default-validation attribute.)

If the attribute is absent, the default is "strip". Allowed values are "preserve" and "strip".

First, it defines the default value of the validation attribute of all the stylesheet’s xsl:element, xsl:attribute, xsl:copy, xsl:copy-of and xsl:result-document instructions. It also affects literal result elements, each of which can alternatively have its own xsl:validation attribute.

Second, the default-validation attribute determines validation of the primary result tree. Secondary result trees can have separate validation rules in the corresponding xsl:result-document instruction through its validation attribute.

Its scope is limited to the module containing it, excluding external modules that it imports or includes.

The attribute’s values have these effects:

default-validation="preserve"
The value "preserve" preserves any existing binding to a datatype.

default-validation="strip" (default)
The value "strip" removes any datatype binding and assigns attribute nodes the type xs:untypedAtomic and element nodes the type xs:untyped.

Note: This also applies to the same-named attribute of xsl:transform. The default-validation attribute does not exist in XSLT 1.0.

exclude-result-prefixes (also a standard attribute)

Value

tokens | "#default" | "#all"

Usage

optional

Introduced in

XSLT 1.0

The attribute exclude-result-prefixes prevents the listed namespace prefixes and their namespaces from being copied to the result document. Its value is a space-separated token list of prefixes to suppress. All namespace URIs bound to these prefixes count as excluded (excluded).

The default namespace, identified by the attribute xmlns without an attached prefix, can be added to the exclusion list with #default.

The abbreviation #all excludes every namespace from copying to the result document. Other prefixes listed are then not evaluated, since their namespaces are already excluded.

The exclusion list applies to the entire content of the xsl:stylesheet element carrying the attribute, but only within that module. It does not extend to imported or included external modules, where it must be set separately.

Note: This also applies to the same-named attribute of xsl:transform.

extension-element-prefixes (also a standard attribute)

Value

tokens | "#default"

Usage

Optional

Introduced in

XSLT 1.0

Elements in stylesheets can be marked as extension instructions by binding them to a special namespace declared in xsl:stylesheet. This namespace is also listed in the token list of the attribute extension-element-prefixes to identify it as belonging to extension instructions. This also prevents the namespace from being copied to the result document.

The prefix designation applies from the containing element, inclusively, throughout its descendant stylesheet branch—here the whole module. The attribute is cumulative: reapplying it to a subbranch adds declarations instead of replacing existing ones.

The default namespace from the xmlns declaration can be added to the token list. Since it has no prefix, the value #default identifies it.

Its scope is only the containing module, excluding external modules imported or included by it.

Note: This also applies to the same-named attribute of xsl:transform.

id (also a standard attribute)

Value

id

Usage

optional

Introduced in

XSLT 1.0

The id attribute here allows inclusion of a stylesheet embedded as an xsl:stylesheet element within the XML document it is to process (embedded stylesheet).

It is usually invoked directly from the containing XML document through an XSL processing instruction using the identifier as a fragment identifier, for example:

<?xml-stylesheet type="text/xml" href="#identifier"?>

Note: This also applies to the same-named attribute of xsl:transform.

input-type-annotations

Value

"preserve" | "strip" | "unspecified"

Usage

optional

Introduced in

XSLT 2.0

The attribute input-type-annotations removes all type annotations from the input tree, replacing element annotations with xs:untyped and attribute annotations with xs:untypedAtomic.

The stylesheet can thus produce the same output whether or not its input was schema-validated and received type annotations.

In a modular stylesheet, every module using this attribute must use the same value. It is a static error (ERR XTSE0265) if one module requests input-type-annotations="strip" and another input-type-annotations="preserve". Setting the attribute in a single module, whichever one, suffices.

The values "preserve" and "unspecified" preserve type annotations. The value "strip" affects the input tree as follows:

  • Every element node’s type annotation becomes xs:untyped.

  • Every attribute node’s type annotation becomes xs:untypedAtomic.

  • Each element and attribute node’s typed value is its string value, treated as an instance of xs:untypedAtomic.

  • The nilled property of every element node becomes false. 

The values of the is-id and is-idrefs properties remain unchanged.

Note: This also applies to the same-named attribute of xsl:transform. The attribute does not exist in XSLT 1.0.

version (also a standard attribute)

Value

number

Usage

required

Introduced in

XSLT 1.0

The version attribute is required. A positive decimal number xs:decimal identifies the XSLT version used in this stylesheet or module. A nondecimal value is an error (ERR XTSE0110). For XSLT 2.0 use version="2.0"; for XSLT 1.0 use version="1.0". (Note: The value "1.1" for XSLT 1.1 should no longer be used because that specification was withdrawn, though some processors support it.)

Note: The versions attribute determines whether processing is normal, forwards-compatible (FC mode) or backwards-compatible (BC mode). See Forwards and backwards compatibility.

Note: This also applies to the same-named attribute of xsl:transform.

xpath-default-namespace (also a standard attribute)

Value

uri-String

Usage

optional

Introduced in

XSLT 2.0

This attribute declares a default namespace for all unprefixed element names in XPath expressions. (Note: This expressly differs from the “xmlns” default namespace for unprefixed elements inside the stylesheet itself!) It thus affects the interpretation of XPath patterns.

The value of xpath-default-namespace is a URI identifying this namespace. If absent, the default is the empty string, meaning the null namespace.

This attribute addresses problems with XPath expressions targeting unprefixed elements within the scope of a xmlns default namespace declaration, thus in a non-null namespace. However, a NameTest targeting an unprefixed element in an XPath expression always assumes and tests a null namespace under the specification’s rules; its expanded name then equals its local name.

A default namespace declared with xmlns="http://defaultnamensraum" applies to unprefixed elements in the document but is ignored in XPath, so the target node would not be recognized because the namespaces appear not to match. The xpath-default-namespace attribute supplies another namespace URI instead of the default null namespace.

The namespace defined with xpath-default-namespace applies to all XPath expressions in the containing module and throughout its descendant branch, but can be overridden in subbranches by new declarations.

This namespace does not apply to the attribute or namespace axes. Within XPath expressions, it applies only to element names tested by NameTests, not other QNames such as function or variable names or names passed to the function key(), even if unprefixed within XPath.

Note: This also applies to the same-named attribute of xsl:transform. The xpath-default-namespace attribute does not exist in XSLT 1.0.

Purpose:

The element xsl:stylesheet is the stylesheet’s root and contains its entire body. Its start tag declares global stylesheet namespaces and the version number of the XSLT version used.

Content structure of xsl:stylesheet:

XSLT top-level elements are allowed as direct children of xsl:stylesheet: twelve in XSLT 1.0 and fifteen in XSLT 2.0.

XSLT 1.0 top-level elements

xsl:attribute-set

xsl:output

xsl:decimal-format

xsl:param

xsl:import

xsl:preserve-space

xsl:include

xsl:variable

xsl:key

xsl:strip-space

xsl:namespace-alias

xsl:template

Table: XSLT 1.0 top-level elements.

XSLT 2.0 top-level elements

xsl:attribute-set

xsl:namespace-alias

xsl:character-map

xsl:output

xsl:decimal-format

xsl:param

xsl:function

xsl:preserve-space

xsl:import

xsl:variable

xsl:import-schema

xsl:strip-space

xsl:include

xsl:template

xsl:key

Table: XSLT 2.0 top-level elements

Other permitted content consists only of extension instructions or user-defined data elements marked by a prefix and nonempty namespace. Direct children in the null namespace are therefore forbidden. Text nodes containing nonwhitespace characters are also forbidden in xsl:stylesheet. Comments, as in any XML document, are allowed everywhere.

The order of declarations inside xsl:stylesheet is irrelevant to transformation execution, except that, for technical reasons, xsl:import declarations must precede all other instructions.

Order matters only for conflicting declarations, such as rules with identical match patterns, if the processor does not treat such duplicates as errors immediately.

Processors usually, but need not, treat the last declaration in document order as authoritative and discard earlier ones.

This matters for modules included with xsl:include. Using xsl:next-match can even reactivate overridden rules in XSLT 2.0 under certain conditions; XSLT 1.0 cannot do this.

For modules imported with xsl:import, the lower precedence of imported declarations prevents collisions. This does not apply to every declaration: same-named declarations such as xsl:key accumulate regardless of import precedence; with same-named duplicates of xsl:output or xsl:decimal-format, each conflicting property takes its highest-precedence value.typo3/#_ftn1

Otherwise, all declarations are first read statically and checked for correctness, like program subroutines. Static errors discovered here block processing before it starts, unlike dynamic errors that appear only at runtime.

Effects of the version attribute
The version attribute declares the stylesheet’s underlying XSLT version. The effect of the version attribute on transformation depends on the processor’s supported XSLT version.

Currently, processors can be expected to support XSLT 1.0 or 2.0. XSLT 1.1 is occasionally supported but was withdrawn as a standard.

An XSLT 2.0-compatible processor readily processes a stylesheet marked version="2.0". A stylesheet marked version="1.0" is either rejected or, more likely, processed in backwards-compatible (backwards-compatible) mode, following XSLT 1.0 and XPath 1.0 rules. A version greater than "2.0" switches the XSLT 2.0 processor to forwards-compatible mode.

An XSLT 1.0-compatible processor processes a stylesheet readily if its version is version="1.0". It does not reject a stylesheet marked version="2.0" or higher, but runs in forwards-compatible (forwards-compatible) mode. Unknown declarations are ignored; for unknown instructions, enclosed xsl:fallback elements execute.

Note — Using xsl:fallback for backwards-compatible processing:
If an older processor may need to process an XSLT 2.0 stylesheet, ensure backwards compatibility with fallback elements and checks for newer instruction and function availability.

Declaring namespaces:

Every stylesheet must declare the XSLT namespace for its instructions. Usually, but not necessarily, it is bound to the prefix xsl::

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

Other namespaces can or must also be declared for extension instructions or predefined elements or types from other XML applications, such as XSL-FO or XML Schema. The order of namespace pseudoattributes does not matter. Several prefixes may bind to the same namespace URI, but not vice versa.

In addition, xmlns="http://mein_defaultnamensraum" can add a default namespace for unprefixed elements within the stylesheet.

Example:
To use XSL Formatting Objects elements alongside XSLT instructions, declare their namespace too:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">

Common predefined namespace URIs include:

The XSLT namespace — required:
"http://www.w3.org/1999/XSL/Transform"
This namespace and its prefix identify XSLT elements, attributes and other names with special meaning under the specification. It applies to both XSLT 1.0 and 2.0. The usual prefix is xsl.

The namespace for XPath 2.0 functions:
"http://www.w3.org/2005/xpath-functions"
This namespace can identify XPath 2.0 core functions (see “XPath/XQuery Functions and Operators”). Most processors will probably recognize them by local name too. To use prefixed function names, bind the prefix to this URI. The usual prefix is fn. This namespace is not valid in XPath 1.0.

The XML Schema namespace:
"http://www.w3.org/2001/XMLSchema"
This namespace identifies predefined (built-in) datatypes from XML Schema and their constructor functions. The usual prefix is xs.

The XML namespace:
"http://www.w3.org/XML/1998/namespace"
The XML namespace is predefined for all XML documents by the specification “XML Namespaces 1.0”, so it need not be separately declared in xsl:stylesheet. It is explicitly referenced by the standard attributes xml:lang and xml:space. Its fixed prefix is xml.

Examples:

Example 1 — Simple stylesheet declaration:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <!-- Stylesheetinhalt -->
</xsl:stylesheet>

This XSLT 1.0 stylesheet declares only the version attribute and XSLT namespace. An XSLT 2.0 processor would use backwards-compatible mode.

Example 2 — XSLT 2.0 stylesheet declaration:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fn="http://www.w3.org/2003/05/xpath-functions" xmlns:xs="http://www.w3.org/2001/XMLSchema-datatypes" >
  <!-- Stylesheetinhalt -->
</xsl:stylesheet>

This XSLT 2.0 stylesheet declares its version attribute and three namespaces: XSLT, XPath functions and XML Schema datatypes. An XSLT 1.0 processor would use forwards-compatible mode.

Example 3 — Source document namespace in the stylesheet:

Source document:

<?xml version="1.0" encoding="ISO-8859-1"?>
<beispiel xmlns:spezial="http://mein.spezialnamensraum">
  <test>Ein Testelement</test>
  <spezial:test>Ein spezielles Testelement</spezial:test>
</beispiel>

The XML document declares a namespace with prefix spezial:.

Stylesheet (excerpt):

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:spezial="http://mein.spezialnamensraum" exclude-result-prefixes="spezial">
  <xsl:template match="beispiel">
    <ergebnis>
      <xsl:apply-templates/>
    </ergebnis>
  </xsl:template>
  <xsl:template match="test">
    <ausgabe>Das ist ein Testelement!</ausgabe>
  </xsl:template>
  <xsl:template match="spezial:test">
    <ausgabe>Das ist ein Spezial-Testelement!</ausgabe>
  </xsl:template>
</xsl:stylesheet>

Since two template rules must distinguish between test and spezial:test, the spezial namespace must also be declared in the stylesheet. The exclude-result-prefix attribute prevents copying it to the result document.

Result document:

<?xml version="1.0" encoding="UTF-8"?>
<ergebnis>
  <ausgabe>Das ist ein Testelement!</ausgabe>
  <ausgabe>Das ist ein Spezial-Testelement!</ausgabe>
</ergebnis>
  

The spezial namespace is absent from and unnecessary in the result document. Without the exclude-result-prefixes attribute, however, it would have been copied:

<?xml version="1.0" encoding="UTF-8"?>
<ergebnis xmlns:spezial="http://mein.spezialnamensraum">...</ergebnis>

If function and schema-datatype namespaces were present in the stylesheet, they too would need exclusion from copying. Only the XSLT namespace itself is automatically excluded.

Element definition:

XSLT 1.0:

<!-- Category: root-element -->
<xsl:stylesheet 
     id = id 
     extension-element-prefixes = tokens 
     exclude-result-prefixes = tokens 
     version = number>

     <!-- Content: (xsl:import*, top-level-elements) -->
</xsl:stylesheet>

XSLT 2.0:

<!-- Category: root-element -->
<xsl:stylesheet
      id? = id
      extension-element-prefixes? = tokens
      exclude-result-prefixes? = tokens
      version = number
      xpath-default-namespace? = uri
      default-validation? = "strict" | "lax" | "preserve" | "strip">
 
      <!-- Content: (xsl:import*, other-declarations) -->
</xsl:stylesheet>
   
<xsl:stylesheet
   id? = id
   extension-element-prefixes? = tokens
   exclude-result-prefixes? = tokens
   version = number
   xpath-default-namespace? = uri
   default-validation? = "preserve" | "strip"
   default-collation? = uri-list
   input-type-annotations? = "preserve" | "strip" | "unspecified">
 
   <!-- Content: (xsl:import*, other-declarations) -->
</xsl:stylesheet>
  

   

  

back to top