XML technologies / XSLT / Reference: XSLT 1.0 and 2.0 elements / Additional standard attributes from XSLT 2.0
(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 6.)
As noted, XSLT 2.0 permits additional standard attributes on every declaration and instruction, alongside the still valid xml:lang and xml:space. All are optional: three namespace attributes, exclude-result-prefixes, extension-element-prefixes and xpath-default-namespace, plus two processing attributes, default-collation and use-when. Attribute version is now also permitted on every XSLT element.
In modular stylesheets, these attributes apply only to the local module where set.
The version attribute is allowed on every XSLT element:
From XSLT 2.0, attribute version can specify each declaration or instruction’s version. It is optional except on xsl:stylesheet and xsl:transform, where mandatory.
default-collation
Value | URI string |
Usage | Optional standard attribute |
Introduced in | XSLT 2.0 |
String comparisons and sorting in XPath always need a string-comparison rule, a collation, normally “Unicode Codepoint Collation”, comparing character codepoints.
Attribute default-collation specifies an alternative collation for XPath within the element’s scope, using a URI identifying it.
Several space-separated collation URIs are allowed because recognition depends on implementation: the processor searches the list until it recognizes one. Put Unicode Codepoint Collation last, since it is always supported and prevents an error.
It also affects instructions xsl:key and xsl:for-each-group in scope, but not xsl:sort, which always uses the default collation.
Note: XPath 1.0 always compares codepoints; other comparison or sorting rules cannot be selected.
Further information:
RFC 4790 (March 2007) proposes global naming of collation URIs: ftp://ftp.rfc-editor.org/in-notes/rfc4790.txt
exclude-result-prefixes
Value | tokens | "#default" | "#all" |
Usage | Optional standard attribute |
Introduced in | XSLT 2.0 |
Attribute exclude-result-prefixes prevents copying namespace prefixes and their namespaces into the result. List prefixes as tokens separated by whitespace (whitespace). All namespace URIs bound to them are excluded (excluded).
The default namespace, declared by pseudo-attribute xmlns without a prefix, can also be added using #default in the exclusion list.
Shorthand #all excludes all namespaces from copying without exception. Other prefixes in the list are then ignored.
The list applies to the element bearing the attribute and its entire descendant stylesheet branch.
Applies only to the current module:
When attribute exclude-result-prefixes is on element xsl:stylesheet, it does not extend to imported or included external modules.
extension-element-prefixes
Value | tokens | "#default" |
Usage | Optional standard attribute |
Introduced in | XSLT 2.0 |
Stylesheet elements can be marked as extension instructions by binding them to a special namespace declared at the root. Listing it in attribute extension-element-prefixes marks it as an extension namespace and prevents its copying into the result.
This applies to the element bearing the attribute and all descendants. It is cumulative: repeating it on a subbranch adds declarations instead of replacing them. The default namespace of the declaration xmlns can be included; since it has no prefix, use #default.
Applies only to the current module:
When attribute extension-element-prefixes is on root element xsl:stylesheet, like exclude-result-prefixes, it applies only to this module, not imported or included external modules.
use-when
Value | xpath-expression |
Usage | Optional standard attribute |
Introduced in | XSLT 2.0 |
The new attribute use-when is also allowed on every XSLT element. It takes an XPath expression evaluated as a boolean, deciding whether to process the element and its content (use-when="true") or ignore them (use-when="false").
Conditions such as processor version or availability of functions and schema types can include or exclude instructions, template blocks or whole modules. This is particularly useful when the processor can compile and store a stylesheet beforehand (Saxon supports this).
Evaluation occurs while reading the stylesheet (static analysis phase), before static-error checking or loading a source document. Only static context is available. For example, XPath expressions in use-when cannot reference variables, which are not instantiated even if in scope, test imported schema types, whose import comes later, test stylesheet functions (processor extension functions are allowed), or check external text and XML resources, loaded later.
The most obvious available information is the supported XSLT version, obtained with function system-property(). This block is read only with XSLT 2.0 or later; otherwise it is ignored:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="beispiel" use-when="number(system-property('xsl:version')) >= 2.0">
... <!-- nur für XSLT 2.0-Prozessoren und neuer -->
</xsl:template>
...
Beware of old processors in forwards-compatible mode
An XSLT 1.0 processor interpreting XSLT 2.0 in forwards-compatible mode ignores unfamiliar attributes use-when and would execute the block above. Use the familiar attribute priority of xsl:template to help.
Here the current processor respects attribute use-when, while the older processor identifies its intended template at runtime at the latest using attribute priority:
<xsl:template match="beispiel" priority="5" use-when="number(system-property('xsl:version')) >= 2.0">
<!-- Ein XSLT 2.0-Prozessor liest dieses Template ein. Ein XSLT 1.0-Prozessor erkennt niedrigere Priorität. -->
</xsl:template>
<xsl:template match="beispiel" priority="10" use-when="number(system-property('xsl:version')) = 1.0">
<!-- Ein XSLT 2.0-Prozessor ignoriert dieses Template. Ein XSLT 1.0-Prozessor erkennt die höhere Priorität. -->
</xsl:template>
Note: With a new processor, attribute priority causes no conflict because the second template is never read and does not exist at runtime. The recognized template’s priority is irrelevant here.
version
Value | "1.0" | "2.0" ... etc. |
Usage | Optional standard attribute |
Introduced in | General standard attribute since XSLT 2.0 |
Attribute version tells the XSLT processor the specification version applying to its declaration or instruction. On root element xsl:stylesheet, it sets the whole stylesheet’s version.
Attribute version can now declare versions for individual instructions and contents. XSLT 1.0 processors naturally ignore these attributes.
Attribute version controls backward- and forwards-compatible behavior. In an XSLT 2.0 stylesheet, attribute version below "2.0", that is "1.0", forces XSLT 1.0 backward-compatible processing. Above "2.0", even an XSLT 2.0 processor uses forwards-compatible rules.
Note: Modules need not share a version. A processor can switch behavior per module or even per instruction.
xpath-default-namespace
Value | URI string |
Usage | Optional standard attribute |
Introduced in | XSLT 2.0 |
This declares a default namespace within XPath expressions for unprefixed elements, affecting XPath pattern interpretation.
The value of xpath-default-namespace is the namespace URI. This addresses XPath expressions targeting unprefixed elements when a default namespace for xmlns also exists.
Explanation:
A NameTest on an unprefixed element assumes a null namespace and tests it (the expanded name then equals the local name). A default namespace declared with xmlns="http://defaultnamensraum" applies to unprefixed elements in document context but not XPath, so the sought element would not be recognized.
The namespace defined by xpath-default-namespace covers its element and descendant branch, but can be overridden in subbranches. It does not apply to attribute or namespace axes.
In XPath, it affects only element names checked by NameTests, not other QNames such as functions, variables or names passed to XSLT function key().
Here the default namespace of xpath-default-namespace applies to the select attribute’s pattern in xsl:for-each:
<xsl:template match="buch" xpath-default-namespace="http://beispiel.org/buch">
<xsl:for-each select="kapitel">
<!-- verarbeitet <kapitel> im genannten Namensraum -->
</xsl:for-each>
</xsl:template>
Use on literal result elements
All these attributes also work on literal result elements, but must bear prefix xsl to identify the XSLT namespace, written as xsl:use-when, xsl:default-collation, xsl:exclude-result-prefixes, xsl:extension-element-prefixes and xsl:xpath-default-namespace.
Note on the following XSLT element descriptions
Because attribute version and these standard attributes apply to every XSLT element from version 2.0, the following alphabetical reference does not list them separately for every element.
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