This document is also available in these non-normative formats: HTML without revision markings and HTML with revision markings.
Copyright © 2003 W3C® (MIT, ERCIM, Keio), All Rights Reserved. W3C liability, trademark, document use and software licensing rules apply.
This specification defines the syntax and semantics of XSLT 2.0, which is a language for transforming XML documents into other XML documents.
XSLT 2.0 is designed to be used in conjunction with XPath 2.0, which is defined in [XPath 2.0]. XSLT shares the same data model as XPath 2.0, which is defined in [Data Model], and it uses the library of functions and operators defined in [Functions and Operators].
XSLT 2.0 also includes optional facilities to serialize the results of a transformation, by means of an interface to the serialization component described in [XSLT and XQuery Serialization].
This document contains hyperlinks to specific sections or definitions within other documents in this family of specifications. These links are indicated visually by a superscript identifying the target specification: for example XP for XPath, DM for the Data Model, FO for Functions and Operators.
This section describes the status of this document at the time of its publication. Other documents may supersede this document. A list of current W3C publications and the latest revision of this technical report can be found in the W3C technical reports index at http://www.w3.org/TR/.
This specification is a Last Call Working Draft of XSLT 2.0. This is a signal that:
the XSL Working Group believes that it has satisfied its relevant technical requirements;
the Working Group believes that it has satisfied significant dependencies with other groups;
other groups are invited to review the document to confirm that these dependencies have been satisfied;
the Working Group is planning to advance the specification to become a Candidate Recommendation.
The final date for comments on this draft is 15 February 2004.
Comments should be sent to public-qt-comments@w3.org. Because
the same mailing list is also used for comments on XPath 2.0 and XQuery
1.0, it is helpful to include the string [XSLT2.0] in the
subject line, together with an originator's reference number that can be
used to track progress in dealing with the comment. If possible, please
send each comment as a separate email. Archives of
the comments and responses are available.
The document is published in two versions: one that highlights changes since the previous published Working Draft, and one without change highlighting.
As predicted in the previous (May 2003) draft, there are relatively
few technical innovations in this draft, but a substantial amount of
editorial revision and clarification. The technical changes of note are
the ability of many XSLT instructions (for example, xsl:attribute and xsl:value-of) to use a
select attribute or a contained sequence
constructor interchangeably, and the introduction of tunnel parameters
which allow parameter values to be passed from a high-level template rule
to a low-level rule without being declared in all the intermediate
templates. Named sort keys and the sort function have been
replaced with a new xsl:perform-sort instruction.
There have been revisions to the date formatting functions, aligning them
with the xsl:number
instruction and transferring some of the functionality into xsl:number to make it more widely
applicable.
A detailed summary of the changes is included at K.2.4 Changes since the May 2003 draft
The Working Group has commenced, but has not yet completed, a review of the classification of all error conditions described in this draft. It is likely that this review will cause the classification of some errors to change, for example some errors currently classified as recoverable may change to being non-recoverable, or vice versa. Comments on the classification, or on the general approach to handling of dynamic errors, are welcomed.
The statements in this draft concerning dependencies on other specifications that are not yet Recommendations (notably XML 1.1 and XML Namespaces 1.1) must be regarded as provisional, pending final acceptance of those specifications.
Publication as a Working Draft does not imply endorsement by the W3C Membership. This is a draft document and may be updated, replaced or obsoleted by other documents at any time. It is inappropriate to cite this document as other than work in progress.
XSLT 2.0 is a revised version of the XSLT 1.0 Recommendation [XSLT 1.0] published on 16 November 1999. The changes made in this document are intended to meet the requirements for XSLT 2.0 described in [XSLT 2.0 Requirements] and to incorporate fixes for errors that have been detected in XSLT 1.0. A summary of the changes since XSLT 1.0 is included in K Changes from XSLT 1.0.
XSLT 2.0 is designed to be used together with XPath 2.0, which has been developed by the W3C XSL Working Group in collaboration with the XML Query Working Group. The current specification of XPath 2.0 can be found in [XPath 2.0].
Public discussion of XSL, including XSL Transformations, takes place on the XSL-List mailing list.
The English version of this specification is the only normative version. However, for translations of this document, see http://www.w3.org/Style/XSL/translations.html.
The development of XSLT is undertaken by the XSL Working Group which is now part of the W3C XML Activity.
Patent disclosures relevant to this specification may be found on the XSL Working Group's patent disclosure page at http://www.w3.org/Style/XSL/Disclosures.html.
1 Introduction
1.1 What is
XSLT?
1.2 What's new in
XSLT 2.0?
2 Concepts
2.1 Terminology
2.2 Notation
2.3 Initiating a
Transformation
2.4 Executing a Transformation
2.5 The Stylesheet Evaluation
Context
2.5.1 Maintaining Position: the Focus
2.5.2 Additional Context Variables
2.6 Parsing
and Serialization
2.7 Extensibility
2.8 Stylesheets and Schemas
2.9 Error Handling
3 Stylesheet Structure
3.1 XSLT
Namespace
3.2 Reserved
Namespaces
3.3 Extension
Attributes
3.4 XSLT Media
Type
3.5 Standard
Attributes
3.6 Stylesheet
Element
3.6.1 User-defined Data Elements
3.7 Simplified
Stylesheet Modules
3.8 Backwards-Compatible
Processing
3.9 Forwards-Compatible
Processing
3.10 Combining
Stylesheet Modules
3.10.1 Locating Stylesheet Modules
3.10.2 Stylesheet Inclusion
3.10.3 Stylesheet Import
3.11 Embedded Stylesheet
Modules
3.12 Built-in
Types
3.13 Importing Schema
Components
4 Data Model
4.1 XML
Versions
4.2 Stripping
Whitespace from the Stylesheet
4.3 Stripping Whitespace from a
Source Tree
4.4 Attributes Types
and DTD Validation
4.5 Disable Output
Escaping
5 Syntactic Constructs
5.1 Qualified Names
5.2 Unprefixed
QNames in Expressions and Patterns
5.3 Expressions
5.3.1 Initializing the Static Context
5.3.2 Initializing the Dynamic Context
5.4 Patterns
5.5 Attribute Value Templates
5.6 Sequence
Constructors
5.6.1 Constructing Complex Content
5.6.2 Constructing Simple Content
5.6.3 Namespace Fixup
6 Template Rules
6.1 Defining
Templates
6.2 Defining
Template Rules
6.3 Applying
Template Rules
6.4 Conflict Resolution for
Template Rules
6.5 Modes
6.6 Built-in Template
Rules
6.7 Overriding Template
Rules
7 Repetition
8 Conditional Processing
8.1 Conditional Processing with
xsl:if
8.2 Conditional Processing
with xsl:choose
9 Variables and Parameters
9.1 Variables
9.2 Parameters
9.3 Values of
Variables and Parameters
9.4 Temporary
Trees
9.5 Global Variables
and Parameters
9.6 Local Variables
and Parameters
9.7 Scope of
Variables
9.8 Circular
Definitions
10 Callable Components
10.1 Named
Templates
10.1.1 Passing Parameters to Templates
10.1.2 Tunnel Parameters
10.2 Named Attribute
Sets
10.3 Stylesheet
Functions
11 Creating Nodes and Sequences
11.1 Literal
Result Elements
11.1.1 Setting the Type Annotation for Literal
Result Elements
11.1.2 Attribute Nodes for Literal Result
Elements
11.1.3 Namespace Nodes for Literal Result Elements
11.1.4 Namespace Aliasing
11.2 Creating Element
Nodes using xsl:element
11.2.1 Setting the Type Annotation for a
Constructed Element Node
11.3 Creating
Attribute Nodes using xsl:attribute
11.3.1 Setting the Type Annotation for a
Constructed Attribute Node
11.4 Creating Text
Nodes
11.4.1 Literal Text Nodes
11.4.2 Creating Text Nodes using xsl:text
11.4.3 Generating Text with xsl:value-of
11.5 Creating Processing
Instructions
11.6 Creating
Namespace Nodes
11.7 Creating
Comments
11.8 Copying Nodes from a
Source Tree to a Result Tree
11.8.1 Shallow Copy
11.8.2 Deep Copy
11.9 Constructing Sequences
12 Numbering
12.1 Formatting a Supplied Number
12.2 Numbering based on Position in a
Document
12.3 Number to String
Conversion Attributes
13 Sorting
13.1 The xsl:sort
Element
13.1.1 The Sorting Process
13.1.2 Comparing Sort Key Values
13.1.3 Sorting using Collations
13.2 Creating
a Sorted Sequence
13.3 Processing a
Sequence in Sorted Order
14 Grouping
14.1 The Current
Group
14.2 The Current
Grouping Key
14.3 The
xsl:for-each-group Element
14.4 Examples of
Grouping
15 Regular Expressions
15.1 The
xsl:analyze-string instruction
15.2 Captured
Substrings
15.3 Examples of
Regular Expression Matching
16 Additional Functions
16.1 Multiple Source
Documents
16.2 Reading Text
Files
16.3 Keys
16.3.1 The xsl:key Declaration
16.3.2 The key Function
16.4 Number
Formatting
16.4.1 Defining a Decimal Format
16.4.2 Processing the Picture String
16.4.3 Analysing the Picture String
16.4.4 Formatting the Number
16.5 Formatting Dates and
Times
16.5.1 The Picture String
16.5.2 The language, calendar, and country
arguments
16.5.3 Examples of date and time formatting
16.6 Miscellaneous
Additional Functions
16.6.1 current
16.6.2 unparsed-entity-uri
16.6.3 unparsed-entity-public-id
16.6.4 generate-id
16.6.5 system-property
17 Messages
18 Extensibility and Fallback
18.1 Extension
Functions
18.1.1 Testing Availability of
Functions
18.1.2 Calling Extension Functions
18.1.3 External Objects
18.2 Extension
Instructions
18.2.1 Designating an Extension
Namespace
18.2.2 Testing Availability of
Instructions
18.2.3 Fallback
19 Result Trees
19.1 Creating
Result Trees
19.2 Validation
19.2.1 Validating Constructed Elements and
Attributes
19.2.2 Validating Document Nodes
20 Serialization
20.1 Character
Maps
20.2 Disabling
Output Escaping
21 Conformance
21.1 Basic XSLT
Processor
21.2 Schema-Aware XSLT Processor
21.3 Serialization Feature
21.4 Backwards Compatibility
Feature
A References
A.1 Normative
References
A.2 Other
References
B The XSLT Media Type
B.1 Registration of MIME media type
application/xslt+xml
B.2 Fragment
Identifiers
C Glossary (Non-Normative)
D Element Syntax Summary
(Non-Normative)
E Summary of Error Conditions
(Non-Normative)
F Checklist of
Implementation-Defined Features (Non-Normative)
G Schema for XSLT Stylesheets
(Non-Normative)
H Acknowledgements (Non-Normative)
I Checklist of Requirements (Non-Normative)
J Summary of Issues (Non-Normative)
J.1 Open Issues
J.2 Decided
Issues
J.3 Closed
Issues
K Changes from XSLT 1.0 (Non-Normative)
K.1 Incompatible
Changes
K.1.1 Backwards Compatibility
Behavior
K.1.2 Incompatibility in the Absence of a
Schema
K.1.3 Compatibility in the Presence of a
Schema
K.1.4 XPath 2.0 Backwards Compatibility
K.2 New
Functionality
K.2.1 Pervasive changes
K.2.2 Major Features
K.2.3 Minor Changes
K.2.4 Changes since the May 2003 draft
This specification defines the syntax and semantics of the XSLT 2.0 language.
[Definition: A transformation in the XSLT language is expressed in the form of a stylesheet, whose syntax is well-formed XML [XML 1.0] conforming to the Namespaces in XML Recommendation [XML Namespaces 1.0].]
A stylesheet generally includes elements that are defined by XSLT
as well as elements that are not defined by XSLT. XSLT-defined
elements are distinguished by use of the namespace
http://www.w3.org/1999/XSL/Transform (see 3.1 XSLT Namespace), which is referred
to in this specification as the XSLT namespace. Thus this specification is a
definition of the syntax and semantics of the XSLT namespace.
The term stylesheet reflects the fact that one of the important roles of XSLT is to add styling information to an XML source document, by transforming it into a document consisting of XSL formatting objects (see [XSL]), or into another presentation-oriented format such as HTML, XHTML, or SVG. However, XSLT is used for a wide range of XML-to-XML transformation tasks, not exclusively for formatting and presentation applications.
A transformation expressed in XSLT describes rules for transforming one or more source trees into one or more result trees. The structure of these trees is described in [Data Model]. The transformation is achieved by a set of template rules. A template rule associates a pattern, which matches nodes in the source document, with a sequence constructor, which can be evaluated to produce part of a result tree. The structure of the result trees can be completely different from the structure of the source trees. In constructing a result tree, nodes from the source trees can be filtered and reordered, and arbitrary structure can be added. This mechanism allows a stylesheet to be applicable to a wide class of documents that have similar source tree structures.
[Definition: A stylesheet may consist of several stylesheet
modules, contained in different XML documents. For a given
transformation, one of these functions as the principal stylesheet
module. The complete stylesheet is assembled by finding the stylesheet
modules referenced directly or indirectly from the principal
stylesheet module using xsl:include and xsl:import elements: see 3.10.2 Stylesheet Inclusion and 3.10.3 Stylesheet Import.]
XSLT 1.0 was published in November 1999, and version 2.0 represents a significant increase in the capability of the language. A detailed list of changes is included in K Changes from XSLT 1.0. XSLT 2.0 has been developed in parallel with XPath 2.0 (see [XPath 2.0]), so the changes to XPath must be considered alongside the changes to XSLT.
For a full glossary of terms, see C Glossary.
[Definition: The software responsible for transforming source trees into result trees is referred to as the processor. This is sometimes expanded to XSLT processor to avoid any confusion with other processors, for example an XML processor.]
[Definition: A specific product that performs the functions of an XSLT processor is referred to as an implementation ].
Note:
The precise meanings of the terms source tree and
result tree, as used in this specification, depend on the
context. In the context of the stylesheet as a whole, the source trees are
the trees provided as the initial input to the transformation,
together with any trees supplied as stylesheet parameters and any
trees accessed using the document, doc
FO or collection
FO functions; while the result trees are the
trees created by an explicit xsl:result-document
instruction as well as the implicit result tree created in the
absence of an xsl:result-document
instruction. In the context of an individual instruction in the
stylesheet, the
term source tree also includes any temporary tree that the instruction
is using for input, and the term result tree includes any
temporary
tree that the instruction is using for output.
In this specification the words must, must not, should, should not, may, required, and recommended are to be interpreted as described in [RFC2119]. Where the word must relates to the behavior of the XSLT processor, then an implementation is not conformant unless it behaves as specified, subject to the more detailed rules in 21 Conformance. Where the word must relates to a stylesheet, then the processor must enforce this constraint on stylesheets.
[Definition: In this specification, the term implementation-defined refers to a feature where the implementation is allowed some flexibility, and where the choices made by the implementation should be described in the vendor's documentation.]
[Definition: The term implementation-dependent refers to a feature where the behavior may vary from one implementation to another, and where the vendor is not expected to provide a full specification of the behavior.] (This might apply, for example, to limits on the size of source documents that can be transformed.)
In all cases where this specification leaves the behavior implementation-defined or implementation-dependent, the implementation has the option of providing mechanisms that allow the user to influence the behavior.
A paragraph labeled as a Note or described as an example is non-normative.
Many terms used in this document are defined in the XPath specification [XPath 2.0] or the Data Model specification [Data Model]. Particular attention is drawn to the following:
[Definition: The term atomization is defined in Section 2.3.2 AtomizationXP. It is a process that takes as input a sequence of nodes and atomic values, and returns a sequence of atomic values, in which the nodes are replaced by their typed values as defined in [Data Model].] For some nodes (for example, elements with element-only content), atomization generates a dynamic error.
[Definition: The
term typed value is defined in Section
5.6 typed-value AccessorDM. Every
node except an element defined in the schema with element-only
content has a typed value. For example, the typed value of an
attribute of type xs:IDREFS is a sequence of zero or
more xs:IDREF values.]
[Definition: The term string value is defined in Section 5.5 string-value AccessorDM. Every node has a string value. For example, the string value of an element is the concatenation of the string values of all its descendant text nodes.]
[Definition: The term XPath 1.0
compatibility mode is defined in Section 2.1.1
Static ContextXP. This is a setting
in the static context of an XPath expression; it has two values,
true and false. When the value is set
to true, the semantics of function calls and certain other
operations are adjusted to give a greater degree of backwards
compatibility between XPath 2.0 and XPath 1.0.]
In this document the specification of each XSLT-defined element type is preceded by a summary of its syntax in the form of a model for elements of that element type. A full list of all these specifications can be found in D Element Syntax Summary. The meaning of syntax summary notation is as follows:
An attribute that is required is shown with its name in bold. An attribute that may be omitted is shown with a question mark following its name.
The string that occurs in the place of an attribute value
specifies the allowed values of the attribute. If this is
surrounded by curly brackets ({...}), then the
attribute value is treated as an attribute value template, and
the string occurring within curly brackets specifies the allowed
values of the result of evaluating the attribute value template.
Alternative allowed values are separated by |. A
quoted string indicates a value equal to that specific string. An
unquoted, italicized name specifies a particular type of
value.
In all cases where this specification states that the value of an attribute must be one of a limited set of values, leading and trailing whitespace in the attribute value is ignored. In the case of an attribute value template, this applies to the effective value obtained when the attribute value template is expanded.
Unless the element is required to be
empty, the model element contains a comment specifying the
allowed content. The allowed content is specified in a similar
way to an element type declaration in XML;
sequence constructor means that any mixture
of text nodes, literal result elements,
extension instructions, and XSLT
elements from the instruction category is allowed;
other-declarations means that any mixture of XSLT
elements from the declaration category, other than xsl:import, is allowed,
together with user-defined data elements.
The element is prefaced by comments indicating if it belongs
to the instruction category or
declaration category or both. The category of an
element only affects whether it is allowed in the content of
elements that allow a sequence constructor or
other-declarations.
This example illustrates the notation used to describe XSLT elements.
<!-- Category: instruction
-->
<xsl:example-element
select = expression
debug? = { "yes" | "no" }>
<!-- Content: ((xsl:variable | xsl:param)*, xsl:sequence) -->
</xsl:example-element>
This example defines a (non-existent) element
xsl:example-element. The element is classified as an
instruction. It takes a mandatory select attribute,
whose value is an XPath expression, and an optional debug
attribute, whose value must be either
yes or no; the curly brackets indicate
that the value can be defined as an attribute value template,
allowing a value such as debug="{$debug}", where the
variable
debug is evaluated to yield "yes" or
"no" at run-time.
The content of an xsl:example-element instruction
is defined to be a sequence of zero or more xsl:variable and xsl:param elements, followed by
an xsl:sequence
element.
[ERR XT0010] A static error is signaled if an XSLT-defined element is used in a context where it is not permitted, if a required attribute is omitted, or if the content of the element does not correspond to the content that is allowed for the element.
Attributes are validated as follows. These rules apply to the value of the attribute after removing leading and trailing whitespace.
[ERR XT0020] It is a static error if an attribute (other than an attribute written using curly brackets in a position where an attribute value template is permitted) contains a value that is not one of the permitted values for that attribute.
[ERR XT0030] It is a non-recoverable dynamic error if the effective value of an attribute written using curly brackets, in a position where an attribute value template is permitted, is a value that is not one of the permitted values for that attribute.
Special rules apply if the construct appears in part of the stylesheet that is processed with forwards-compatible behavior: see 3.9 Forwards-Compatible Processing.
Note:
This working draft includes a non-normative XML Schema for XSLT stylesheet modules (see G Schema for XSLT Stylesheets). The syntax summaries described in this section are normative.
XSLT defines a set of standard functions which are additional to those defined in [Functions and Operators]. The signatures of these functions are described using the same notation as used in [Functions and Operators]. The names of these functions are all in the standard function namespace.
This document does not specify any application programming interfaces or other interfaces for initiating a transformation. This section, however, describes the information that must be supplied when a transformation is initiated.
Implementations may allow a transformation to run as two or more phases, for example parsing, compilation and execution. Such a distinction is outside the scope of this specification, which treats transformation as a single process controlled using a set of stylesheet modules, supplied in the form of XML documents.
The following information is supplied to execute a transformation:
The stylesheet module that is to act as
the principal stylesheet module
for the transformation. The complete stylesheet is assembled by
recursively expanding the xsl:import and xsl:include declarations in
the principal stylesheet module, as described in 3.10.2 Stylesheet Inclusion and 3.10.3 Stylesheet Import.
A set (possibly empty) of values for stylesheet parameters (see 9.5 Global Variables and Parameters). These values are available for use within expressions in the stylesheet.
[Definition: A node that acts as the initial
context node for the transformation. This node is accessible
within the stylesheet as the initial value of the XPath
expressions
. (dot) and self::node(), as described
in 2.5.1 Maintaining Position: the
Focus].
If no initial context node is supplied, then the context item, context position, and context size will initially be unset, and the evaluation of any expression that references these values will result in a dynamic error. (Note that the initial context size and context position will always be 1 (one) when an initial context node is supplied, and will be undefined if no initial context node is supplied).
Optionally, the name of a named template which is to be executed as the entry point to the transformation. This template must exist within the stylesheet. If no named template is supplied, then the transformation starts with the template rule that best matches the initial context node, according to the rules defined in 6.4 Conflict Resolution for Template Rules. Either a named template, or an initial context node, or both, must be supplied.
Optionally, an initial mode. If an initial mode is supplied, then in searching for the template rule that best matches the initial context node, the processor considers only those rules that apply to the initial mode. If no initial mode is supplied, the default mode is used.
[Definition: A base output URI, that is, a URI to be used as the base URI when resolving a relative URI allocated to a result tree. If the transformation generates multiple result trees, then typically each one will be allocated a URI relative to this base URI.]
[ERR XT0040] It is a non-recoverable dynamic error if the invocation of the stylesheet specifies a template name that does not match the expanded-QName of a named template defined in the stylesheet.
[ERR
XT0050] It is a non-recoverable dynamic error if
the stylesheet that is invoked declares a visible stylesheet
parameter with required="yes" and no value for this
parameter is supplied during the invocation of the stylesheet. A
stylesheet parameter is visible if it is not masked by another global
variable or parameter with the same name and higher import
precedence.
[Definition: The transformation is performed by evaluating
an initial template; if a named template is supplied when the
transformation is initiated, then this is the initial template;
otherwise, the initial template is the template rule selected according to the
rules of the xsl:apply-templates
instruction for processing the initial context node in the initial
mode.]
Parameters passed to the transformation by the client application are matched against stylesheet parameters (see 9.5 Global Variables and Parameters), not against the template parameters declared within the initial template. All template parameters within the initial template to be executed will take their default values.
[ERR
XT0060] It is a non-recoverable dynamic error if
the initial
template defines a template parameter that specifies
required="yes".
A stylesheet can
process further source documents in addition to those supplied when
the transformation is invoked. These additional documents can be
loaded using the functions document (see 16.1 Multiple Source Documents) or doc
FO or collection
FO (see [Functions and
Operators]), or they can be supplied as stylesheet
parameters (see 9.5 Global
Variables and Parameters), or as the result of an extension
function (see 18.1 Extension
Functions).
[Definition: A stylesheet contains a set of template rules (see 6 Template Rules). A template rule has two parts: a pattern that is matched against nodes, and a sequence constructor that is evaluated to produce a sequence of items. In most cases these items are nodes, which are then written to a result tree.]
A transformation as a whole is executed by evaluating the
sequence constructor of the initial template
as described in 5.6 Sequence
Constructors. If the result is a non-empty sequence, then
this sequence is used to construct an implicit result tree, following
the rules described in 5.6.1 Constructing Complex
Content: the effect is as if the sequence
constructor contained in the initial template were contained in an
xsl:result-document
element with no attributes.
[Definition: The elements appearing within a sequence constructor are referred to as instructions.]
The main categories of instruction elements are as follows:
instructions that create new nodes: xsl:element, xsl:attribute, xsl:processing-instruction,
xsl:comment, xsl:value-of, xsl:text, xsl:namespace;
an instruction that creates an arbitrary sequence: xsl:sequence;
instructions that cause conditional or repeated evaluation of
nested instructions: xsl:if, xsl:choose, xsl:for-each, xsl:for-each-group;
instructions that invoke templates: xsl:apply-templates,
xsl:apply-imports,
xsl:call-template,
xsl:next-match;
an instruction that declares variables: xsl:variable;
other specialized instructions: xsl:number, xsl:analyze-string,
xsl:message, xsl:result-document.
Often, a sequence constructor will include an
xsl:apply-templates
instruction, which selects a sequence of nodes to be processed. Each
of the selected nodes is processed by searching the stylesheet for a
matching template
rule and evaluating the sequence constructor of that template
rule. The resulting sequences of items are concatenated, in order, to
give the result of the xsl:apply-templates
instruction, as described in 6.3
Applying Template Rules; this sequence is often added to a
result tree. Since the sequence constructors of the selected
template rules
may themselves contain xsl:apply-templates
instructions, this results in a cycle of selecting nodes, identifying
template rules,
constructing sequences, and constructing result trees, that recurses
through the source tree.
During the evaluation of a stylesheet, certain information is maintained about the current state of processing. This information is referred to collectively as the evaluation context. The variables that make up the evaluation context are described in this section.
The evaluation context is structured as a stack. When an instruction is evaluated, it inherits the state of the evaluation context from its calling instruction. An instruction may make modifications to the state of the evaluation context, but on return to its caller, the evaluation context is always in the same state as it was on entry to the instruction. The scope of variables in the evaluation context is dynamic; they are passed implicitly from a calling template to a called template, except where otherwise specified.
The variables making up the evaluation context are not available when a stylesheet function is called from an XPath expression. On entry to a stylesheet function, a new empty evaluation context is established. Some variables in an empty evaluation context are said to be undefined, in which case any reference to this variable causes a dynamic error. Other variables are initialized to a defined value, such as an empty sequence.
For convenience, the evaluation context is described in two parts: the focus, which represents the place in the source document that is currently being processed, and a collection of additional context variables.
[Definition: When a sequence constructor is evaluated, the processor keeps track of which nodes are being processed by means of a set of implicit variables referred to collectively as the focus.] More specifically, the focus consists of the following three values:
[Definition: The context item is the item
currently being processed. An item (see [Data Model]) is either an atomic value (such
as an integer, date, or string), or a node. The context item is
initially set to the initial context node supplied
when the transformation is invoked (see 2.3 Initiating a Transformation). It
changes whenever instructions such as xsl:apply-templates
and xsl:for-each
are used to process a sequence of items; each item in such a
sequence becomes the context item while that item is being
processed.] The context item is
returned by the XPath expression . (dot).
[Definition: The context position is the
position of the context item within the sequence of items
currently being processed. It changes whenever the context item
changes. When an instruction such as xsl:apply-templates
or xsl:for-each is
used to process a sequence of items, the first item in the
sequence is processed with a context position of 1, the second
item with a context position of 2, and so on.] The context position is returned by the
XPath expression position().
[Definition: The context size is the number of
items in the sequence of items currently being processed. It
changes whenever instructions such as xsl:apply-templates
and xsl:for-each
are used to process a sequence of items; during the processing
of each one of those items, the context size is set to the
count of the number of items in the sequence (or equivalently,
the position of the last item in the sequence).] The context size is returned by the XPath
expression
last().
[Definition: If
the context
item is a node (as distinct from an atomic value such as an
integer), then it is also referred to as the context node.
The context node is not an independent variable, it changes
whenever the context item changes. When the context item is an
atomic value, there is no context node.] The context node is returned by the XPath
expression
self::node(), and it is used as the starting node for
all relative path expressions.
The current
function can be used within any XPath expression to select the item that was
supplied as the context item to the XPath expression by the XSLT
processor; unlike . (dot) this is unaffected by
changes to the context item that occur within the XPath expression.
The current function
is described in 16.6.1
current.
On completion of an instruction that changes the focus (such as xsl:apply-templates or
xsl:for-each), the
focus reverts to its previous value.
When a stylesheet function is called, the focus within the body of the function is initially undefined. The focus is also undefined on initial entry to the stylesheet if no initial context node supplied.
[ERR XT0070] When the focus is undefined, evaluation of any expression that references the context item, context position, or context size results in a non-recoverable dynamic error.
The description above gives an outline of the way the focus works. Detailed rules for the effect of each instruction are given separately with the description of that instruction. In the absence of specific rules, an instruction uses the same focus as its parent instruction.
Sometimes the focus is based on a single node.
[Definition: A singleton focus based on a node N has the context item (and therefore the context node) set to N, and the context position and context size both set to 1 (one).]
In addition to the values that make up the focus, an XSLT processor maintains a number of other internal variables that reflect aspects of the evaluation context. These variables are fully described in the sections of the specification that maintain and use these variables. They are:
The current template, which is the
template
rule most recently invoked by an xsl:apply-templates,
xsl:apply-imports,
or xsl:next-match
instruction: see 6.7 Overriding
Template Rules;
The current mode, which is the mode in which the current template rule was invoked: see 6.5 Modes;
The current group and current
grouping key, which provide information about the
collection of items currently being processed by an xsl:for-each-group
instruction: see 14.1 The Current
Group and 14.2 The
Current Grouping Key;
The current captured
substrings: this is a sequence of strings, which
is maintained when a string is matched against a regular
expression using the xsl:analyze-string
instruction, and which is accessible using the regex-group function:
see 15.2 Captured
Substrings.
The output
state: this is a flag whose two possible values are
final output state and temporary output state. This
flag indicates whether instructions are currently writing to a
final result tree or to an internal data structure. The initial
setting is final output state, and it is
switched to temporary output state by
instructions such as xsl:variable. For more
details, see 19.1 Creating
Result Trees.
The following non-normative table summarizes the initial state of each of the variables in the evaluation context, and the instructions which cause the state of the variable to change.
| Variable | Initial Setting | Set by | Cleared by |
|---|---|---|---|
| focus | singleton focus based on the initial context node if supplied | xsl:apply-templates,
xsl:for-each,
xsl:for-each-group,
xsl:analyze-string |
calls on stylesheet functions |
| current template | the initial template | xsl:apply-templates,
xsl:apply-imports,
xsl:next-match |
xsl:for-each, xsl:for-each-group,
calls on stylesheet functions |
| current mode | the initial mode | xsl:apply-templates |
calls on stylesheet functions |
| current group | empty sequence | xsl:for-each-group |
calls on stylesheet functions |
| current grouping key | empty sequence | xsl:for-each-group |
calls on stylesheet functions |
| current captured substrings | empty sequence | xsl:matching-substring |
xsl:non-matching-substring;
calls on stylesheet functions |
| output state | final output state | Set to temporary output state by
instructions such as xsl:variable, xsl:attribute, etc.,
and by calls on stylesheet functions |
None |
An XSLT stylesheet describes a process that constructs a set of result trees from a set of source trees.
The stylesheet does not describe how a source tree is constructed. Frequently an implementation will operate in conjunction with an XML parser (or more strictly, in the terminology of [XML 1.0], an XML processor), to build the source tree from an input XML document. An implementation may also provide an application programming interface allowing the tree to be constructed directly, or allowing it to be supplied in the form of a DOM Document object (see [DOM2]). This is outside the scope of this specification. Users should be aware, however, that since the input to the transformation is a tree conforming to the XPath data model as described in [Data Model], constructs that might exist in the original XML document, or in the DOM, but which are not within the scope of the data model, cannot be processed by the stylesheet and cannot be guaranteed to remain unchanged in the transformation output. Such constructs include CDATA section boundaries, the use of entity references, and the DOCTYPE declaration and internal DTD subset.
[Definition: A frequent requirement is to output a result tree as an XML document (or in other formats such as HTML). This process is referred to as serialization.]
Like parsing, serialization is not part of the transformation
process, and it is not required that an
XSLT processor must be able to perform
serialization. However, for pragmatic reasons, this specification
describes declarations (the xsl:output element and the
xsl:character-map
declarations, see 20
Serialization) which allow a stylesheet to specify the desired
properties of a serialized output file. When serialization is
not being performed, either because the implementation does not
support the serialization option, or because the user is executing
the transformation in a way that does not invoke serialization, then
the content of the xsl:output and xsl:character-map
declarations has no effect. Under these circumstances the processor
may report any errors in an xsl:output or xsl:character-map
declaration, but is not required to do
so.
XSLT defines a number of features that allow the language to be extended by implementers, or, if implementers choose to provide the capability, by users. These features have been designed, so far as possible, so that they can be used without sacrificing interoperability. Extensions other than those explicitly defined in this specification are not permitted.
These features are all based on XML namespaces; namespaces are used to ensure that the extensions provided by one implementer do not clash with those of a different implementer.
The most common way of extending the language is by providing additional functions, which can be invoked from XPath expressions. These are known as extension functions, and are described in 18.1 Extension Functions.
It is also permissible to extend the language by providing new
XSLT instructions. These are referred to as extension
instructions, and are described in 18.2 Extension Instructions. A
stylesheet that uses extension instructions must declare that it is
doing so by using the [xsl:]extension-element-prefixes
attribute.
Extension instructions and extension functions defined according to these rules may be provided by the implementer of the XSLT processor, and the implementer may also provide facilities to allow users to create further extension instructions and extension functions.
This specification defines how extension instructions and extension functions are invoked, but the facilities for creating new extension instructions and extension functions are implementation-defined. For further details, see 18 Extensibility and Fallback.
The XSLT language can also be extended by the use of extension attributes (see 3.3 Extension Attributes), and by means of user-defined data elements (see 3.6.1 User-defined Data Elements).
An XSLT stylesheet can make use of information from a schema. An XSLT transformation can take place in the absence of a schema (and, indeed, in the absence of a DTD), but where the source document has undergone schema validity assessment, the XSLT processor has access to the type information associated with individual nodes, not merely to the untyped text.
Information from a schema can be used both statically (when the stylesheet is compiled), and dynamically (during evaluation of the stylesheet to transform a source document).
There are places within a stylesheet, and within XPath expressions and patterns in a stylesheet, where it is possible to refer to named type definitions in a schema, or to element and attribute declarations. For example, it is possible to declare the types expected for the parameters of a function. This is done using the SequenceType XP syntax defined in [XPath 2.0].
[Definition: Type definitions and element and attribute declarations are referred to collectively as schema components.]
[Definition: The schema components that may be referenced by name in a stylesheet are referred to as the in-scope schema components. This set is the same throughout a stylesheet.]
The conformance rules for XSLT 2.0, defined in 21 Conformance, distinguish between a
basic
XSLT processor and a schema-aware XSLT processor. As
the names suggest, a basic XSLT processor does not support the
features of XSLT that require access to schema information, either
statically or dynamically. A stylesheet that works with a basic XSLT
processor will work unchanged with a schema-aware XSLT
processor, unless the type information created as a result of schema
processing introduces type errors (for example, an attribute of type
xs:integer cannot be used as an argument of the concat
FO function), or unless the type information
changes the outcome of operations such as comparison and
sorting.
There is a standard set of type definitions that are always available as in-scope schema components in every stylesheet. These are defined in 3.12 Built-in Types. The set of built-in types varies between a basic XSLT processor and a schema-aware XSLT processor.
The remainder of this section describes facilities that are available only with a schema-aware XSLT processor.
Additional schema components (type definitions,
element declarations, and attribute declarations) may be added to the
in-scope schema components by
means of the xsl:import-schema
declaration in a stylesheet.
It is only necessary to import a schema explicitly if one or more of its schema components are referenced explicitly by name in the stylesheet; it is not necessary to import a schema merely because the stylesheet is used to process a source document that has been assessed against that schema. It is possible to make use of the information resulting from schema assessment (for example, the fact that a particular attribute holds a date) even if no schema has been imported by the stylesheet.
Further, importing a schema does not of itself say anything about the type of the source document that the stylesheet is expected to process. The imported type definitions can be used for temporary nodes or for nodes on the result tree just as much as for nodes in source documents. It is possible to make assertions about the type of an input document by means of tests within the stylesheet. For example:
<xsl:template match="document-node(element(my:invoice))" priority="2"> . . . </xsl:template> <xsl:template match="document-node()" priority="1"> <xsl:message terminate="yes">Source document is not an invoice</xsl:message> </xsl:template>
This example will cause the transformation to fail with an error
message unless the document element of the source document is valid
against the top-level element declaration my:invoice,
and has been annotated as such.
It is possible that a source document may contain nodes whose type
annotation is not one of the types imported by the stylesheet. This
creates a potential problem because in the case of an expression such
as data(.) instance of xs:integer the system needs to
know whether the type named in the type annotation of the context
node is derived by restriction from the type xs:integer.
This information is not explicitly available in the data model, as
defined in [Data Model]. The implementation
may choose one of several strategies for dealing with this
situation:
The processor may signal a non-recoverable dynamic error if a source document is found to contain a type annotation that is not known to the processor.
The processor may maintain additional metadata, beyond that
described in [Data Model], that allows
the source document to be processed as if all the necessary
schema information had been imported using xsl:import-schema. Such
metadata might be held in the data model itself, or it might be
held in a system catalog or repository.
The processor may be configured to use a fixed set of schemas, which are automatically used to validate all source documents before they can be supplied as input to a transformation. In this case it is impossible for a source document to have a type annotation that the processor is not aware of.
The processor may be configured to treat the input data model
as if no schema processing had been performed, that is,
effectively to strip all type annotations from elements and
attributes on input, marking them instead as having type
xdt:untypedAny and xdt:untypedAtomic
respectively.
Where a stylesheet author chooses to make assertions about the types of nodes or of variables and parameters, it is possible for an XSLT processor to perform static analysis of the stylesheet (that is, analysis in the absence of any source document). Such analysis may reveal errors that would otherwise not be discovered until the transformation is actually executed. An XSLT processor is not required to perform such static type-checking. Under some circumstances (see 2.9 Error Handling) type errors that are detected early may be reported as static errors. In addition an implementation may report any condition found during static analysis as a warning, provided that this does not prevent the stylesheet being evaluated as described by this specification.
A stylesheet can also control the type annotations of nodes that it constructs in a final result tree, or in temporary trees. This can be done in a number of ways.
It is possible to request explicit validation of a complete
result tree. Validation is either strict or lax, as described in
[XML Schema]. If validation of a result
tree fails (strictly speaking, if the outcome of the validity
assessment is invalid), then the transformation
fails, but in all other cases, the element and
attribute nodes of the tree will be annotated with the names of
the types to which these nodes conform. These annotations will be
discarded if the result tree is serialized as an XML document,
but they remain available when the result tree is passed to an
application (perhaps another stylesheet) for further processing.
It is also possible to validate individual element and
attribute nodes as they are constructed. This is done using the
type and validation attributes of the
xsl:element, xsl:attribute, xsl:copy, and xsl:copy-of instructions, or
the xsl:type and xsl:validation
attributes of a literal result element.
When elements, attributes, or document nodes are
copied, either explicitly using the xsl:copy or xsl:copy-of instructions, or
implicitly when nodes in a sequence are attached to a new parent
node, the options validation="strip" and
validation="preserve" are available, to control
whether existing type annotations are to be retained or not.
When nodes in a temporary tree are validated, type information is available for use by operations carried out on the temporary tree, in the same way as for a source document that has undergone schema assessment.
For details of how validation of element and attribute nodes works, see 19.2 Validation.
[Definition: An error that is detected by examining a stylesheet before execution starts (that is, before the source document and values of stylesheet parameters are available) is referred to as a static error.]
Errors classified in this specification as static errors must be signaled by all implementations: that is, the processor must indicate that the error is present. A static error must be signaled even if it occurs in a part of the stylesheet that is never evaluated. Static errors are never recoverable. After signaling a static error, a processor may continue for the purpose of signaling additional errors, but it must eventually terminate abnormally without producing any result tree.
There is an exception to this rule when the stylesheet specifies forwards-compatible behavior (see 3.9 Forwards-Compatible Processing).
Generally, errors in the structure of the stylesheet, or in the syntax of XPath expressions contained in the stylesheet, are classified as static errors. Where this specification states that an element in the stylesheet must or must not appear in a certain position, or that it must or must not have a particular attribute, or that an attribute must or must not have a value satisfying specified conditions, then any contravention of this rule is a static error unless otherwise specified.
[Definition: An error that is not detected until a source document is being transformed is referred to as a dynamic error.]
[Definition: Some dynamic errors are classed as recoverable errors. When a recoverable error occurs, this specification allows the processor either to signal the error (by reporting the error condition and terminating execution) or to take a defined recovery action and continue processing.] It is implementation-defined whether the error is signaled or the recovery action is taken.
[Definition: If an implementation chooses to recover from a recoverable dynamic error, it must take the optional recovery action defined for that error condition in this specification.]
When the implementation makes the choice between signaling a dynamic error or recovering, it is not restricted in how it makes the choice; for example, it may provide options that can be set by the user. When an implementation chooses to recover from a dynamic error, it may also take other action, such as logging a warning message.
[Definition: A dynamic error that is not recoverable is referred to as a non-recoverable dynamic error. When a non-recoverable dynamic error occurs, the processor must signal the error, and the transformation fails.]
Because different implementations may optimize execution of the stylesheet in different ways, the detection of dynamic errors is to some degree implementation-dependent. In cases where an implementation is able to produce the result tree without evaluating a particular construct, the implementation is never required to evaluate that construct solely in order to determine whether doing so causes a dynamic error. For example, if a variable is declared but never referenced, an implementation may choose whether or not to evaluate the variable declaration, which means that if evaluating the variable declaration causes a dynamic error, some implementations will signal this error and others will not.
There are some cases where this specification requires that a
construct must not be evaluated: for
example, the content of an xsl:if instruction must not be evaluated if the test condition is false.
This means that an implementation must not
signal any dynamic errors that would arise if the construct were
evaluated.
An implementation may signal a dynamic error before any source document is available, but only if it can determine that the error would be signaled for every possible source document and every possible set of parameter values. For example, some circularity errors fall into this category: see 9.8 Circular Definitions.
The XPath specification states, in effect, that an XPath processor may evaluate a constant subexpression during the analysis phase, and if any error occurs during that evaluation, it may report this as a static error. For XPath expressions used in an XSLT stylesheet, however, any such errors must not be reported as static errors in the stylesheet; instead, they must be held back until the evaluation phase, and signaled only if the XPath expression is actually evaluated.
An XPath processor may report statically that the expression
1 div 0 fails with a "divide by zero" error. But
suppose this XPath expression occurs in an XSLT construct such
as:
<xsl:choose>
<xsl:when test="system-property('xsl:version') = '1.0'">
<xsl:value-of select="1 div 0"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="xs:double('+INF')"/>
</xsl:otherwise>
</xsl:choose>
Then the XSLT processor must not report an error, because the relevant XPath construct appears in a context where it will never be executed by an XSLT 2.0 processor. (An XSLT 1.0 processor will execute this code successfully, returning positive infinity, because it uses double arithmetic rather than decimal arithmetic.)
[Definition: Certain errors are classified as type errors. A type error occurs when the value supplied as input to an operation is of the wrong type for that operation, for example when an integer is supplied to an operation that expects a node.] If a type error occurs in an instruction that is actually evaluated, then it must be signaled in the same way as a non-recoverable dynamic error. Alternatively, an implementation may signal a type error during the analysis phase in the same way as a static error, even if it occurs in part of the stylesheet that is never evaluated, provided it can establish that execution of a particular construct would never succeed.
It is implementation-defined whether type errors are signaled statically.
For example, the following construct contains a type error,
because 42 is not allowed as an operand of the
xsl:apply-templates
instruction. An implementation may
optionally signal this as a static error, even though the offending
instruction will never be evaluated, and the type error would
therefore never be signaled as a dynamic error.
<xsl:if test="false()"> <xsl:apply-templates select="42"/> </xsl:if>
On the other hand, in the following example it is not possible
to determine statically whether the operand of xsl:apply-templates
will have a suitable dynamic type. An implementation may produce a warning in such cases, but it
must not treat it as an error.
<xsl:template match="para"> <xsl:param name="p" as="item()"/> <xsl:apply-templates select="$p"/> </xsl:template>
If more than one error arises, an implementation is not required to signal any errors other than the first one that it detects. It is implementation-dependent which of the several errors is signaled. This applies both to static errors and to dynamic errors. An implementation is allowed to signal more than one error, but if any errors have been signaled, it must not finish as if the transformation were successful.
When a transformation signals one or more dynamic errors, the final state of any persistent resources updated by the transformation is implementation-dependent. Implementations are not required to restore such resources to their initial state. In particular, where a transformation produces multiple result documents, it is possible that one or more result documents may be written successfully before the transformation terminates, but the application cannot rely on this behavior.
Everything said above about error handling applies equally to errors in evaluating XSLT instructions, and errors in evaluating XPath expressions. Static errors and dynamic errors may occur in both cases.
[Definition: If a transformation has successfully produced a result tree, it is still possible that errors may occur in serializing the result tree. For example, it may be impossible to serialize the result tree using the encoding selected by the user. Such an error is referred to as a serialization error.] As with other aspects of serialization, the handling of serialization errors is implementation-defined: see 20 Serialization.
The error codes used to label error conditions in this specification (and summarized in E Summary of Error Conditions) are provided for ease of reference. Implementations may use these codes when signaling errors, but they are not required to do so. An implementation that uses these codes within an API should treat the codes as unprefixed QNames; additional codes defined by an implementation (or by an application) can then use QNames in an implementation-defined namespace without risk of collision.
[Definition: A stylesheet consists of one or more stylesheet modules, each one forming all or part of a well-formed XML document.]
Note:
A stylesheet module, as defined here, contains XML in its raw textual form. In discussing the semantics of a stylesheet module, this specification frequently makes reference to nodes in the data model (see [Data Model]) that is generated when the XML document containing the stylesheet module is parsed. These references should not be taken as implying that an implementation must always start with stylesheet modules as textual XML documents, nor that it must represent the stylesheet internally as an instance of the data model.
A stylesheet module is either a standard stylesheet module or a simplified stylesheet module:
[Definition: A standard stylesheet
module is an XML document, or part of an XML document, having
an xsl:stylesheet or
xsl:transform element
as its outermost element (see 3.6
Stylesheet Element).]
[Definition: A simplified stylesheet module is an XML document, or part of an XML document, whose outermost element is a literal result element to be copied to the result tree. This element is not itself in the XSLT namespace, but it