![]() |
![]() | ![]() | ![]() | XSL Transformations (XSLT) Version 2.0XSL Transformations (XSLT) Version 2.0W3C Recommendation 23 January 2007
Please refer to the errata for this document, which may include some normative corrections. See also translations. Copyright © 2007 W3C® (MIT, ERCIM, Keio), All Rights Reserved. W3C liability, trademark and document use rules apply. AbstractThis specification defines the syntax and semantics of XSLT 2.0, a language for transforming XML documents into other XML documents. XSLT 2.0 is a revised version of the XSLT 1.0 Recommendation [XSLT 1.0] published on 16 November 1999. 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 XDM data model, FO for Functions and Operators. Status of this DocumentThis 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 Recommendation builds on the success of [XSLT 1.0], which was published on 16 November 1999. Many new features have been added to the language (see J.2 New Functionality) while retaining a high level of backwards compatibility (see J.1 Incompatible Changes). The changes have been designed to meet the requirements for XSLT 2.0 described in [XSLT 2.0 Requirements]. The way in which each requirement has been addressed is outlined in I Checklist of Requirements. XSLT 2.0 depends on a number of other specifications that have progressed to Recommendation status at the same time: see [XPath 2.0], [Data Model], [Functions and Operators], and [XSLT and XQuery Serialization]. These subsidiary documents are also referenced in the specification of XQuery 1.0. This document has been produced by the XSL Working Group, which is part of the XML Activity. The document has been reviewed by W3C Members and other interested parties, and is endorsed by the Director. It is a stable document and may be used as reference material or cited as a normative reference from another document. W3C's role in making the Recommendation is to draw attention to the specification and to promote its widespread deployment. This enhances the functionality and interoperability of the Web. A small number of editorial corrections and clarifications have been made to the document since it was published as a Proposed Recommendation on 21 November 2006. These changes are listed at J.2.4 Changes since Proposed Recommendation. Please record any comments about this document in W3C's public Bugzilla system (instructions can be found at http://www.w3.org/XML/2005/04/qt-bugzilla). If access to that system is not feasible, you may send your comments to the W3C XSLT/XPath/XQuery public comments mailing list, public-qt-comments@w3.org. It is helpful to include the string [XSLT] in the subject line of your comment, whether made in Bugzilla or in email. Each Bugzilla entry and email message should contain only one comment. Archives of the comments and responses are available at http://lists.w3.org/Archives/Public/public-qt-comments/. General public discussion of XSLT takes place on the XSL-List forum. This document was produced by a group operating under the 5 February 2004 W3C Patent Policy. W3C maintains a public list of any patent disclosures made in connection with the deliverables of the group; that page also includes instructions for disclosing a patent. An individual who has actual knowledge of a patent which the individual believes contains Essential Claim(s) must disclose the information in accordance with section 6 of the W3C Patent Policy. Table of Contents1 Introduction AppendicesA References 1 Introduction1.1 What is XSLT?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 [Namespaces in XML 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 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 [Extensible Stylesheet Language (XSL)]), or into another presentation-oriented format such as HTML, XHTML, or SVG. However, XSLT is used for a wide range of transformation tasks, not exclusively for formatting and presentation applications. A transformation expressed in XSLT describes rules for transforming zero 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. In many cases, evaluating the sequence constructor will cause new nodes to be constructed, which can be used 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 1.2 What's New in XSLT 2.0?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 J 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. 2 Concepts2.1 TerminologyFor a full glossary of terms, see C Glossary. [Definition: The software responsible for transforming source trees into result trees using an XSLT stylesheet 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 ]. [Definition: The term result tree is used to refer to any tree constructed by instructions in the stylesheet. A result tree is either a final result tree or a temporary tree.] [Definition: A final result tree is a
result
tree that forms part of the final output of a
transformation. Once created, the contents of a final
result tree are not accessible within the stylesheet
itself.] The [Definition: The term source tree means any
tree provided as input to the transformation. This includes
the document containing the initial context node if
any, documents containing nodes supplied as the values of
stylesheet parameters,
documents obtained from the results of functions such as
[Definition: The term temporary tree means any tree that is neither a source tree nor a final result tree.] Temporary trees are used to hold intermediate results during the execution of the transformation. In this specification the phrases must, must not, should, should not, may, required, and recommended are to be interpreted as described in [RFC2119]. Where the phrase must, must not, or required 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 phrase must, must not, or required relates to a stylesheet, then the processor must enforce this constraint on stylesheets by reporting an error if the constraint is not satisfied. Where the phrase should, should not, or recommended relates to a stylesheet, then a processor may produce warning messages if the constraint is not satisfied, but must not treat this as an error. [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 must be described in documentation that accompanies any conformance claim.] [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 XDM specification [Data Model]. Particular attention is drawn to the following:
[Definition: The term core function means a function that is specified in [Functions and Operators] and that is in the standard function namespace.] 2.2 Notation[Definition: An XSLT element is an element in the XSLT namespace whose syntax and semantics are defined in this specification.] For a non-normative list of XSLT elements, see D Element Syntax Summary. In this document the specification of each XSLT element 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:
This example illustrates the notation used to describe XSLT elements.
This example defines a (non-existent) element
The content of an [ERR XTSE0010] 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.
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. [Definition: Some constructs defined in this specification are described as being deprecated. The use of this term implies that stylesheet authors should not use the construct, and that the construct may be removed in a later version of this specification.] All constructs that are deprecated in this specification are also (as it happens) optional features that implementations are not required to provide. 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. 2.3 Initiating a TransformationThis document does not specify any application programming interfaces or other interfaces for initiating a transformation. This section, however, describes the information that is supplied when a transformation is initiated. Except where otherwise indicated, the information is required. 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:
[ERR XTDE0040] 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 XTDE0045] It is a non-recoverable dynamic
error if the invocation of the stylesheet specifies an initial
mode (other than the
default mode) that does not match the expanded-QName in the
[ERR XTDE0047] It is a non-recoverable dynamic error if the invocation of the stylesheet specifies both an initial mode and an initial template. [ERR XTDE0050] It is a non-recoverable dynamic
error if the stylesheet that is invoked declares a
visible stylesheet parameter with
[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 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 XTDE0060] It is a non-recoverable dynamic
error if the initial template defines a
template parameter that
specifies 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 2.4 Executing a Transformation[Definition: A stylesheet contains a set of template rules (see 6 Template Rules). A template rule has three parts: a pattern that is matched against nodes, a (possibly empty) set of template parameters, and a sequence constructor that is evaluated to produce a sequence of items.] In many cases these items are newly constructed 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.7 Sequence Constructors. If the initial template has an
<xsl:template name="IMPLICIT">
<xsl:result-document href="">
<xsl:call-template name="T"/>
</xsl:result-document>
</xsl:template>
An implicit result tree is also created when the result
sequence is empty, provided that no Note: This means that there is always at least one result
tree. It also means that if the content of the initial
template is a single If the result of the initial template is non-empty,
and an explicit A sequence constructor is a sequence of sibling nodes in the stylesheet, each of which is either an XSLT instruction, a literal result element, a text node, or an extension instruction. [Definition: An instruction is either an XSLT instruction or an extension instruction.] [Definition: An XSLT instruction is an
XSLT
element whose syntax summary in this specification
contains the annotation Extension instructions are described in 18.2 Extension Instructions. The main categories of XSLT instruction are as follows:
Often, a sequence constructor will
include an 2.5 The Evaluation ContextThe results of some expressions and instructions in a stylesheet may depend on information provided contextually. This context information is divided into two categories: the static context, which is known during static analysis of the stylesheet, and the dynamic context, which is not known until the stylesheet is evaluated. Although information in the static context is known at analysis time, it is sometimes used during stylesheet evaluation. Some context information can be set by means of declarations within the stylesheet itself. For example, the namespace bindings used for any XPath expression are determined by the namespace declarations present in containing elements in the stylesheet. Other information may be supplied externally or implicitly: an example is the current date and time. The context information used in processing an XSLT
stylesheet includes as a subset all the context information
required when evaluating XPath expressions. The XPath 2.0
specification defines a static and dynamic context that the
host language (in this case, XSLT) may initialize, which
affects the results of XPath expressions used in that
context. XSLT augments the context with additional
information: this additional information is used firstly by
XSLT constructs outside the scope of XPath (for example,
the The static context for an expression or other construct in a stylesheet is determined by the place in which it appears lexically. The details vary for different components of the static context, but in general, elements within a stylesheet module affect the static context for their descendant elements within the same stylesheet module. The dynamic context is maintained as a stack. When an instruction or expression is evaluated, it may add dynamic context information to the stack; when evaluation is complete, the dynamic context reverts to its previous state. An expression that accesses information from the dynamic context always uses the value at the top of the stack. The most commonly used component of the dynamic context
is the context item. This is an implicit
variable whose value is the item (it may be a node or an
atomic value) currently being processed. The value of the
context item can be referenced within an XPath expression
using the expression Full details of the static and dynamic context are provided in 5.4 The Static and Dynamic Context. 2.6 Parsing and SerializationAn XSLT stylesheet describes a process that constructs a set of final result trees from a set of source trees. The stylesheet does not describe how a source tree is constructed. Some possible ways of constructing source trees are described in [Data Model]. 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 a 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 [DOM Level 2]). 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 XDM 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 final 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 2.7 ExtensibilityXSLT 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 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 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.2 User-defined Data Elements). 2.8 Stylesheets and XML SchemasAn 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 all the modules of 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 produce the
same results with a schema-aware XSLT processor
provided that the source documents are untyped (that
is, they are not validated against a schema). However, if
source documents are validated against a schema then the
results may be different from the case where they are not
validated. Some constructs that work on untyped data may
fail with typed data (for example, an attribute of type
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.13 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 The 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 a 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(schema-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 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 | |||