XSL Transformations (XSLT) Version 1.1 (original) (raw)

1 Introduction

This specification defines the syntax and semantics of the XSLT language. A transformation in the XSLT language is expressed as a well-formed XML document [XML] conforming to the Namespaces in XML Recommendation [XML Names], which may include both elements that are defined by XSLT and elements that are not defined by XSLT. [Definition: XSLT-defined elements are distinguished by belonging to a specific XML namespace (see 2.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.

A transformation expressed in XSLT describes rules for transforming a source tree into a result tree. The transformation is achieved by associating patterns with templates. A pattern is matched against elements in the source tree. A template is instantiated to create part of the result tree. The result tree is separate from the source tree. The structure of the result tree can be completely different from the structure of the source tree. In constructing the result tree, elements from the source tree can be filtered and reordered, and arbitrary structure can be added.

A transformation expressed in XSLT is called a stylesheet. This is because, in the case when XSLT is transforming into the XSL formatting vocabulary, the transformation functions as a stylesheet.

This document does not specify how an XSLT stylesheet is associated with an XML document. It is recommended that XSL processors support the mechanism described in [XML Stylesheet]. When this or any other mechanism yields a sequence of more than one XSLT stylesheet to be applied simultaneously to a XML document, then the effect should be the same as applying a single stylesheet that imports each member of the sequence in order (see 2.6.2 Stylesheet Import).

A stylesheet contains a set of template rules. A template rule has two parts: a pattern which is matched against nodes in the source tree and a template which can be instantiated to form part of the result tree. This allows a stylesheet to be applicable to a wide class of documents that have similar source tree structures.

A template is instantiated for a particular source element to create part of the result tree. A template can contain elements that specify literal result element structure. A template can also contain elements from the XSLT namespace that are instructions for creating result tree fragments. When a template is instantiated, each instruction is executed and replaced by the result tree fragment that it creates. Instructions can select and process descendant source elements. Processing a descendant element creates a result tree fragment by finding the applicable template rule and instantiating its template. Note that elements are only processed when they have been selected by the execution of an instruction. The result tree is constructed by finding the template rule for the root node and instantiating its template.

In the process of finding the applicable template rule, more than one template rule may have a pattern that matches a given element. However, only one template rule will be applied. The method for deciding which template rule to apply is described in 5.5 Conflict Resolution for Template Rules.

A single template by itself has considerable power: it can create structures of arbitrary complexity; it can pull string values out of arbitrary locations in the source tree; it can generate structures that are repeated according to the occurrence of elements in the source tree. For simple transformations where the structure of the result tree is independent of the structure of the source tree, a stylesheet can often consist of only a single template, which functions as a template for the complete result tree. Transformations on XML documents that represent data are often of this kind (seeE.2 Data Example). XSLT allows a simplified syntax for such stylesheets (see 2.3 Literal Result Element as Stylesheet).

When a template is instantiated, it is always instantiated with respect to a [Definition: current node] and a [Definition: current node list]. The current node is always a member of the current node list. Many operations in XSLT are relative to the current node. Only a few instructions change the current node list or the current node (see 5 Template Rules and 8 Repetition); during the instantiation of one of these instructions, the current node list changes to a new list of nodes and each member of this new list becomes the current node in turn; after the instantiation of the instruction is complete, the current node and current node list revert to what they were before the instruction was instantiated.

XSLT makes use of the expression language defined by [XPath] for selecting elements for processing, for conditional processing and for generating text.

XSLT provides two "hooks" for extending the language, one hook for extending the set of instruction elements used in templates and one hook for extending the set of functions used in XPath expressions. These hooks are both based on XML namespaces. See 14 Extensions. This version of XSLT defines a mechanism for providing implementations of extension functions (see 14.4 Defining Extension Functions), but does not define a mechanism for providing implementations of extension elements.

Note:

The XSL WG intends to define such a mechanism for extension elements in a future version of this specification or in a separate specification.

The element syntax summary notation used to describe the syntax of XSLT-defined elements is described in 18 Notation.

The MIME media types text/xml andapplication/xml [RFC2376] should be used for XSLT stylesheets. It is possible that a media type will be registered specifically for XSLT stylesheets; if and when it is, that media type may also be used.

2 Stylesheet Structure

2.1 XSLT Namespace

The XSLT namespace has the URI http://www.w3.org/1999/XSL/Transform.

Note:

The 1999 in the URI indicates the year in which the URI was allocated by the W3C. It does not indicate the version of XSLT being used, which is specified by attributes (see 2.2 Stylesheet Element and 2.3 Literal Result Element as Stylesheet).

XSLT processors must use the XML namespaces mechanism [XML Names] to recognize elements and attributes from this namespace. Elements from the XSLT namespace are recognized only in the stylesheet and not in the source document. The complete list of XSLT-defined elements is specified in B Element Syntax Summary. Vendors must not extend the XSLT namespace with additional elements or attributes. Instead, any extension must be in a separate namespace. Any namespace that is used for additional instruction elements must be identified by means of the extension element mechanism specified in 14.1 Using Extension Elements.

This specification uses a prefix of xsl: for referring to elements in the XSLT namespace. However, XSLT stylesheets are free to use any prefix, provided that there is a namespace declaration that binds the prefix to the URI of the XSLT namespace.

An element from the XSLT namespace may have any attribute not from the XSLT namespace, provided that the expanded-name of the attribute has a non-null namespace URI. The presence of such attributes must not change the behavior of XSLT elements and functions defined in this document. Thus, an XSLT processor is always free to ignore such attributes, and must ignore such attributes without giving an error if it does not recognize the namespace URI. Such attributes can provide, for example, unique identifiers, optimization hints, or documentation.

It is an error for an element from the XSLT namespace to have attributes with expanded-names that have null namespace URIs (i.e. attributes with unprefixed names) other than attributes defined for the element in this document.

Note:

The conventions used for the names of XSLT elements, attributes and functions are that names are all lower-case, use hyphens to separate words, and use abbreviations only if they already appear in the syntax of a related language such as XML or HTML.

2.2 Stylesheet Element

A stylesheet is represented by an xsl:stylesheetelement in an XML document. xsl:transform is allowed as a synonym for xsl:stylesheet.

An xsl:stylesheet element must have aversion attribute, indicating the version of XSLT that the stylesheet requires. For this version of XSLT, the value should be 1.1. When the value is not equal to1.0 or 1.1, forwards-compatible processing mode is enabled (see 2.5 Forwards-Compatible Processing).

The xsl:stylesheet element may contain the following types of elements:

[Definition: An element occurring as a child of an xsl:stylesheet element is called atop-level element.]

This example shows the structure of a stylesheet. Ellipses (...) indicate where attribute values or content have been omitted. Although this example shows one of each type of allowed element, stylesheets may contain zero or more of each of these elements.

<xsl:stylesheet version="1.1" xmlns:xsl="" title="undefined" rel="noopener noreferrer">http://www.w3.org/1999/XSL/Transform"> <xsl:import href="..."/>

<xsl:include href="..."/>

<xsl:strip-space elements="..."/>

<xsl:preserve-space elements="..."/>

<xsl:output method="..."/>

<xsl:key name="..." match="..." use="..."/>

<xsl:decimal-format name="..."/>

<xsl:namespace-alias stylesheet-prefix="..." result-prefix="..."/>

<xsl:attribute-set name="..."> ...

<xsl:variable name="...">...

<xsl:param name="...">...

<xsl:script implements-prefix="..." language="..."> ...

<xsl:template match="..."> ...

<xsl:template name="..."> ...

The order in which the children of the xsl:stylesheetelement occur is not significant except for xsl:importelements and for error recovery. Users are free to order the elements as they prefer, and stylesheet creation tools need not provide control over the order in which the elements occur.

In addition, the xsl:stylesheet element may contain any element not from the XSLT namespace, provided that the expanded-name of the element has a non-null namespace URI. The presence of such top-level elements must not change the behavior of XSLT elements and functions defined in this document; for example, it would not be permitted for such a top-level element to specify thatxsl:apply-templates was to use different rules to resolve conflicts. Thus, an XSLT processor is always free to ignore such top-level elements, and must ignore a top-level element without giving an error if it does not recognize the namespace URI. Such elements can provide, for example,

2.3 Literal Result Element as Stylesheet

A simplified syntax is allowed for stylesheets that consist of only a single template for the root node. The stylesheet may consist of just a literal result element (see 7.1.1 Literal Result Elements). Such a stylesheet is equivalent to a stylesheet with an xsl:stylesheet element containing a template rule containing the literal result element; the template rule has a match pattern of /. For example

Expense Report Summary

Total Amount:

has the same meaning as

<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="" title="undefined" rel="noopener noreferrer">http://www.w3.org/TR/xhtml1"> <xsl:template match="/">

Expense Report Summary

Total Amount:

A literal result element that is the document element of a stylesheet must have an xsl:version attribute, which indicates the version of XSLT that the stylesheet requires. For this version of XSLT, the value should be 1.1; the value must be a Number. Other literal result elements may also have an xsl:versionattribute. When the xsl:version attribute is not equal to1.0 or 1.1, forwards-compatible processing mode is enabled (see 2.5 Forwards-Compatible Processing).

The allowed content of a literal result element when used as a stylesheet is no different from when it occurs within a stylesheet. Thus, a literal result element used as a stylesheet cannot contain top-level elements.

In some situations, the only way that a system can recognize that an XML document needs to be processed by an XSLT processor as an XSLT stylesheet is by examining the XML document itself. Using the simplified syntax makes this harder.

Note:

For example, another XML language (AXL) might also use anaxl:version on the document element to indicate that an XML document was an AXL document that required processing by an AXL processor; if a document had both an axl:versionattribute and an xsl:version attribute, it would be unclear whether the document should be processed by an XSLT processor or an AXL processor.

Therefore, the simplified syntax should not be used for XSLT stylesheets that may be used in such a situation. This situation can, for example, arise when an XSLT stylesheet is transmitted as a message with a MIME media type of text/xml orapplication/xml to a recipient that will use the MIME media type to determine how the message is processed.

2.4 Qualified Names

The name of an internal XSLT object, specifically a named template (see 6 Named Templates), a mode (see 5.7 Modes), an attribute set (see 7.1.4 Named Attribute Sets), a key (see 12.2 Keys), a decimal-format (see 12.3 Number Formatting), a variable or a parameter (see 11 Variables and Parameters) is specified as a QName. If it has a prefix, then the prefix is expanded into a URI reference using the namespace declarations in effect on the attribute in which the name occurs. Theexpanded-nameconsisting of the local part of the name and the possibly null URI reference is used as the name of the object. The default namespace is_not_ used for unprefixed names.

2.5 Forwards-Compatible Processing

An element enables forwards-compatible mode for itself, its attributes, its descendants and their attributes if either it is anxsl:stylesheet element whose versionattribute is not equal to 1.0 or1.1, or it is a literal result element that has an xsl:version attribute whose value is not equal to1.0 or1.1. A literal result element that has an xsl:version attribute whose value is equal to1.0 or 1.1disables forwards-compatible mode for itself, its attributes, its descendants and their attributes.

If an element is processed in forwards-compatible mode, then:

Thus, any XSLT 1.1 processor must be able to process the following stylesheet without error, although the stylesheet includes elements from the XSLT namespace that are not defined in this specification:

<xsl:stylesheet version="17.0" xmlns:xsl="" title="undefined" rel="noopener noreferrer">http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> xsl:choose <xsl:when test="system-property('xsl:version') >= 17.0"> xsl:exciting-new-17.0-feature/ xsl:otherwise XSLT 17.0 required

Sorry, this stylesheet requires XSLT 17.0.

Note:

If a stylesheet depends crucially on a top-level element introduced by a version of XSLT after 1.1, then the stylesheet can use an xsl:message element withterminate="yes" (see 13 Messages) to ensure that XSLT processors implementing earlier versions of XSLT will not silently ignore the top-level element. For example,

<xsl:stylesheet version="18.0" xmlns:xsl="" title="undefined" rel="noopener noreferrer">http://www.w3.org/1999/XSL/Transform">

xsl:important-new-17.0-declaration/

<xsl:template match="/"> xsl:choose <xsl:when test="system-property('xsl:version') < 17.0"> <xsl:message terminate="yes"> xsl:textSorry, this stylesheet requires XSLT 17.0. xsl:otherwise ... ...

If an expression occurs in an attribute that is processed in forwards-compatible mode, then an XSLT processor must recover from errors in the expression as follows:

2.6 Combining Stylesheets

XSLT provides two mechanisms to combine stylesheets:

an inclusion mechanism that allows stylesheets to be combined without changing the semantics of the stylesheets being combined, and
an import mechanism that allows stylesheets to override each other.

2.6.1 Stylesheet Inclusion

An XSLT stylesheet may include another XSLT stylesheet using anxsl:include element. The xsl:include element has an href attribute whose value is a URI reference identifying the stylesheet to be included. A relative URI is resolved relative to the base URI of the xsl:include element (see3.2 Base URI).

The xsl:include element is only allowed as a top-level element.

The inclusion works at the XML tree level. The resource located by the href attribute value is parsed as an XML document, and the children of the xsl:stylesheet element in this document replace the xsl:include element in the including document. The fact that template rules or definitions are included does not affect the way they are processed.

The included stylesheet may use the simplified syntax described in2.3 Literal Result Element as Stylesheet. The included stylesheet is treated the same as the equivalent xsl:stylesheetelement.

It is an error if a stylesheet directly or indirectly includes itself.

Note:

Including a stylesheet multiple times can cause errors because of duplicate definitions. Such multiple inclusions are less obvious when they are indirect. For example, if stylesheetB includes stylesheet A, stylesheet Cincludes stylesheet A, and stylesheet D includes both stylesheet B and stylesheet C, thenA will be included indirectly by D twice. If all of B, C and D are used as independent stylesheets, then the error can be avoided by separating everything in B other than the inclusion of Ainto a separate stylesheet B' and changing B to contain just inclusions of B' and A, similarly for C, and then changing D to includeA, B', C'.

2.6.2 Stylesheet Import

An XSLT stylesheet may import another XSLT stylesheet using anxsl:import element. Importing a stylesheet is the same as including it (see 2.6.1 Stylesheet Inclusion) except that definitions and template rules in the importing stylesheet take precedence over template rules and definitions in the imported stylesheet; this is described in more detail below. The xsl:import element has an href attribute whose value is a URI reference identifying the stylesheet to be imported. A relative URI is resolved relative to the base URI of the xsl:import element (see3.2 Base URI).

The xsl:import element is only allowed as a top-level element. Thexsl:import element children must precede all other element children of an xsl:stylesheet element, including any xsl:include element children. Whenxsl:include is used to include a stylesheet, anyxsl:import elements in the included document are moved up in the including document to after any existingxsl:import elements in the including document.

For example,

<xsl:stylesheet version="1.1" xmlns:xsl="" title="undefined" rel="noopener noreferrer">http://www.w3.org/1999/XSL/Transform"> <xsl:import href="article.xsl"/> <xsl:import href="bigfont.xsl"/> <xsl:attribute-set name="note-style"> <xsl:attribute name="font-style">italic

[Definition: Thexsl:stylesheet elements encountered during processing of a stylesheet that contains xsl:import elements are treated as forming an import tree. In the import tree, each xsl:stylesheet element has one import child for eachxsl:import element that it contains. Anyxsl:include elements are resolved before constructing the import tree.] [Definition: An xsl:stylesheet element in the import tree is defined to have lower import precedence than anotherxsl:stylesheet element in the import tree if it would be visited before that xsl:stylesheet element in a post-order traversal of the import tree (i.e. a traversal of the import tree in which an xsl:stylesheet element is visited after its import children).] Each definition and template rule has import precedence determined by thexsl:stylesheet element that contains it.

For example, suppose

Then the order of import precedence (lowest first) isD, B, E, C,A.

Note:

Since xsl:import elements are required to occur before any definitions or template rules, an implementation that processes imported stylesheets at the point at which it encounters thexsl:import element will encounter definitions and template rules in increasing order of import precedence.

In general, a definition or template rule with higher import precedence takes precedence over a definition or template rule with lower import precedence. This is defined in detail for each kind of definition and for template rules.

It is an error if a stylesheet directly or indirectly imports itself. Apart from this, the case where a stylesheet with a particular URI is imported in multiple places is not treated specially. Theimport tree will have a separate xsl:stylesheet for each place that it is imported.

Note:

If xsl:apply-imports is used (see 5.6 Overriding Template Rules), the behavior may be different from the behavior if the stylesheet had been imported only at the place with the highest import precedence.

2.7 Embedding Stylesheets

Normally an XSLT stylesheet is a complete XML document with thexsl:stylesheet element as the document element. However, an XSLT stylesheet may also be embedded in another resource. Two forms of embedding are possible:

the XSLT stylesheet may be textually embedded in a non-XML resource, or
the xsl:stylesheet element may occur in an XML document other than as the document element.

To facilitate the second form of embedding, thexsl:stylesheet element is allowed to have an ID attribute that specifies a unique identifier.

Note:

In order for such an attribute to be used with the XPath id function, it must actually be declared in the DTD as being an ID.

The following example shows how the xml-stylesheetprocessing instruction [XML Stylesheet] can be used to allow a document to contain its own stylesheet. The URI reference uses a relative URI with a fragment identifier to locate thexsl:stylesheet element:

...

Note:

A stylesheet that is embedded in the document to which it is to be applied or that may be included or imported into a stylesheet that is so embedded typically needs to contain a template rule that specifies thatxsl:stylesheet elements are to be ignored.

3 Data Model

The data model used by XSLT is the same as that used by XPath with the additions described in this section. XSLT operates on source, result and stylesheet documents using the same data model. Any two XML documents that have the same tree will be treated the same by XSLT.

Processing instructions and comments in the stylesheet are ignored: the stylesheet is treated as if neither processing instruction nodes nor comment nodes were included in the tree that represents the stylesheet.

3.1 Root Node Children

The normal restrictions on the children of the root node are relaxed for the result tree. The result tree may have any sequence of nodes as children that would be possible for an element node. In particular, it may have text node children, and any number of element node children. When written out using the XML output method (see16 Output), it is possible that a result tree will not be a well-formed XML document; however, it will always be a well-formed external general parsed entity.

When the source tree is created by parsing a well-formed XML document, the root node of the source tree will automatically satisfy the normal restrictions of having no text node children and exactly one element child. When the source tree is created in some other way, for example by using the DOM, the usual restrictions are relaxed for the source tree as for the result tree.

3.2 Base URI

Every node also has an associated URI called its base URI, which is used for resolving attribute values that represent relative URIs into absolute URIs. The base URI is determined as follows:

3.3 Unparsed Entities

The root node has a mapping that gives the URI for each unparsed entity declared in the document's DTD. The URI is generated from the system identifier and public identifier specified in the entity declaration. The XSLT processor may use the public identifier to generate a URI for the entity instead of the URI specified in the system identifier. If the XSLT processor does not use the public identifier to generate the URI, it must use the system identifier; if the system identifier is a relative URI, it must be resolved into an absolute URI using the URI of the resource containing the entity declaration as the base URI [RFC2396].

3.4 Whitespace Stripping

After the tree for a source document or stylesheet document has been constructed, but before it is otherwise processed by XSLT, some text nodes are stripped. A text node is never stripped unless it contains only whitespace characters. Stripping the text node removes the text node from the tree. The stripping process takes as input a set of element names for which whitespace must be preserved. The stripping process is applied to both stylesheets and source documents, but the set of whitespace-preserving element names is determined differently for stylesheets and for source documents.

A text node is preserved if any of the following apply:

Otherwise, the text node is stripped.

The xml:space attributes are not stripped from the tree.

Note:

This implies that if an xml:space attribute is specified on a literal result element, it will be included in the result.

For stylesheets, the set of whitespace-preserving element names consists of just xsl:text.

For source documents, the set of whitespace-preserving element names is specified byxsl:strip-space and xsl:preserve-space top-level elements. Whether an element name is included in the set of whitespace-preserving names is determined by the best match amongst xsl:strip-space orxsl:preserve-space elements: it is included if and only if there is no match or the best match is anxsl:preserve-space element. Thexsl:strip-space and xsl:preserve-spaceelements each have an elements attribute whose value is a whitespace-separated list of NameTests; an element name matches anxsl:strip-space or xsl:preserve-spaceelement if it matches one of the NameTests. An element matches a NameTest if and only if the NameTest would be true for the element as an XPath node test. When more than one xsl:strip-space andxsl:preserve-space element matches, the best matching element is determined by the best matching NameTest. This is determined in the same way as with template rules:

It is an error if this leaves more than one match. An XSLT processor may signal the error; if it does not signal the error, it must recover by choosing, from amongst the matches that are left, the one that occurs last in the stylesheet.

3.5 Namespace Fixup

In a tree constructed by parsing an XML document, the following constraints relating to namespace nodes will be satisfied:

However, when a tree is being constructed as the result of an XSLT transformation, these constraints may not be satisfied. In particular, since xsl:element and xsl:attributeinstructions do not create namespace nodes, they will often cause these constraints not to be satisfied. [Definition: The process ofnamespace fixup transforms a result tree by adding namespace nodes so that it satisfies these constraints.] What namespace nodes are added and where they are added by namespace fixup is implementation-dependent, provided that the resulting tree satisfies the constraints and provided that all namespaces nodes in the resulting tree are allowable, where a namespace node is allowable if any of the following conditions applies:

Namespace fixup must not result in an element having multiple namespace nodes with the same expanded-name.

Namespace fixup is performed in two situations:

| Editorial note | | | ---------------------------------------------------- | | | What about stylesheet parameters that are node-sets? | |

| Editorial note | | | --------------------------------------------------- | | | What about extension functions returning node-sets? | |

4 Expressions

XSLT uses the expression language defined by XPath [XPath]. Expressions are used in XSLT for a variety of purposes including:

selecting nodes for processing;
specifying conditions for different ways of processing a node;
generating text to be inserted in the result tree.

[Definition: An**expression** must match the XPath production Expr.]

Expressions occur as the value of certain attributes on XSLT-defined elements and within curly braces in attribute value templates.

In XSLT, an outermost expression (i.e. an expression that is not part of another expression) gets its context as follows:

5 Template Rules

5.1 Processing Model

A list of source nodes is processed to create a result tree fragment. The result tree is constructed by processing a list containing just the root node. A list of source nodes is processed by appending the result tree structure created by processing each of the members of the list in order. A node is processed by finding all the template rules with patterns that match the node, and choosing the best amongst them; the chosen rule's template is then instantiated with the node as the current node and with the list of source nodes as the current node list. A template typically contains instructions that select an additional list of source nodes for processing. The process of matching, instantiation and selection is continued recursively until no new source nodes are selected for processing.

Implementations are free to process the source document in any way that produces the same result as if it were processed using this processing model.

5.2 Patterns

[Definition: Template rules identify the nodes to which they apply by using a **pattern**. As well as being used in template rules, patterns are used for numbering (see7.7 Numbering) and for declaring keys (see 12.2 Keys). A pattern specifies a set of conditions on a node. A node that satisfies the conditions matches the pattern; a node that does not satisfy the conditions does not match the pattern. The syntax for patterns is a subset of the syntax for expressions. In particular, location paths that meet certain restrictions can be used as patterns. An expression that is also a pattern always evaluates to an object of type node-set. A node matches a pattern if the node is a member of the result of evaluating the pattern as an expression with respect to some possible context; the possible contexts are those whose context node is the node being matched or one of its ancestors.]

Here are some examples of patterns:

A pattern must match the grammar for Pattern. A Pattern is a set of location path patterns separated by |. A location path pattern is a location path whose steps all use only thechild or attribute axes. Although patterns must not use the descendant-or-self axis, patterns may use the // operator as well as the /operator. Location path patterns can also start with an id or key function call with a literal argument. Predicates in a pattern can use arbitrary expressions just like predicates in a location path.

Patterns
[1] Pattern ::= LocationPathPattern
| Pattern ' ' LocationPathPattern
[2] LocationPathPattern ::= '/' RelativePathPattern?
| IdKeyPattern (('/' '//') RelativePathPattern)?
| '//'? RelativePathPattern
[3] IdKeyPattern ::= 'id' '(' Literal ')'
| 'key' '(' Literal ',' Literal ')'
[4] RelativePathPattern ::= StepPattern
| RelativePathPattern '/' StepPattern
| RelativePathPattern '//' StepPattern
[5] StepPattern ::= ChildOrAttributeAxisSpecifier NodeTest Predicate*
[6] ChildOrAttributeAxisSpecifier ::= AbbreviatedAxisSpecifier
| ('child' 'attribute') '::'

A pattern is defined to match a node if and only if there is possible context such that when the pattern is evaluated as an expression with that context, the node is a member of the resulting node-set. When a node is being matched, the possible contexts have a context node that is the node being matched or any ancestor of that node, and a context node list containing just the context node.

For example, p matches any p element, because for any p if the expression p is evaluated with the parent of the p element as context the resulting node-set will contain that p element as one of its members.

Note:

This matches even a p element that is the document element, since the document root is the parent of the document element.

Although the semantics of patterns are specified indirectly in terms of expression evaluation, it is easy to understand the meaning of a pattern directly without thinking in terms of expression evaluation. In a pattern, | indicates alternatives; a pattern with one or more | separated alternatives matches if any one of the alternative matches. A pattern that consists of a sequence of StepPatterns separated by/ or // is matched from right to left. The pattern only matches if the rightmost StepPattern matches and a suitable element matches the rest of the pattern; if the separator is /then only the parent is a suitable element; if the separator is//, then any ancestor is a suitable element. A StepPattern that uses the child axis matches if the NodeTest is true for the node and the node is not an attribute node. A StepPattern that uses the attribute axis matches if the NodeTest is true for the node and the node is an attribute node. When []is present, then the first PredicateExpr in a StepPattern is evaluated with the node being matched as the context node and the siblings of the context node that match the NodeTest as the context node list, unless the node being matched is an attribute node, in which case the context node list is all the attributes that have the same parent as the attribute being matched and that match the NameTest.

For example

appendix//ulist/item[position()=1]

matches a node if and only if all of the following are true:

5.3 Defining Template Rules

A template rule is specified with the xsl:templateelement. The match attribute is a Pattern that identifies the source node or nodes to which the rule applies. The match attribute is required unless the xsl:template element has aname attribute (see 6 Named Templates). It is an error for the value of the match attribute to contain a VariableReference. The content of the xsl:template element is the template that is instantiated when the template rule is applied.

For example, an XML document might contain:

This is an important point.

The following template rule matches emph elements and produces a fo:inline-sequence formatting object with afont-weight property of bold.

<xsl:template match="emph"> <fo:inline-sequence font-weight="bold"> xsl:apply-templates/

Note:

Examples in this document use the fo: prefix for the namespace http://www.w3.org/1999/XSL/Format, which is the namespace of the formatting objects defined in [XSL].

As described next, the xsl:apply-templates element recursively processes the children of the source element.

5.4 Applying Template Rules

This example creates a block for a chapter element and then processes its immediate children.

<xsl:template match="chapter"> fo:block xsl:apply-templates/

In the absence of a select attribute, thexsl:apply-templates instruction processes all of the children of the current node, including text nodes. However, text nodes that have been stripped as specified in 3.4 Whitespace Strippingwill not be processed. If stripping of whitespace nodes has not been enabled for an element, then all whitespace in the content of the element will be processed as text, and thus whitespace between child elements will count in determining the position of a child element as returned by the position function.

A select attribute can be used to process nodes selected by an expression instead of processing all children. The value of the select attribute is an expression. The expression must evaluate to a node-set. The selected set of nodes is processed in document order, unless a sorting specification is present (see10 Sorting). The following example processes all of theauthor children of the author-group:

<xsl:template match="author-group"> fo:inline-sequence <xsl:apply-templates select="author"/>

The following example processes all of the given-names of the authors that are children ofauthor-group:

<xsl:template match="author-group"> fo:inline-sequence <xsl:apply-templates select="author/given-name"/>

This example processes all of the heading descendant elements of the book element.

<xsl:template match="book"> fo:block <xsl:apply-templates select=".//heading"/>

It is also possible to process elements that are not descendants of the current node. This example assumes that a departmentelement has group children and employeedescendants. It finds an employee's department and then processes the group children of the department.

<xsl:template match="employee"> fo:block Employee <xsl:apply-templates select="name"/> belongs to group <xsl:apply-templates select="ancestor::department/group"/>

Multiple xsl:apply-templates elements can be used within a single template to do simple reordering. The following example creates two HTML tables. The first table is filled with domestic sales while the second table is filled with foreign sales.

<xsl:template match="product">

<xsl:apply-templates select="sales/domestic"/>
<xsl:apply-templates select="sales/foreign"/>

Note:

It is possible for there to be two matching descendants where one is a descendant of the other. This case is not treated specially: both descendants will be processed as usual. For example, given a source document

the rule

<xsl:template match="doc"> <xsl:apply-templates select=".//div"/>

will process both the outer div and inner divelements.

Note:

Typically, xsl:apply-templates is used to process only nodes that are descendants of the current node. Such use of xsl:apply-templates cannot result in non-terminating processing loops. However, when xsl:apply-templates is used to process elements that are not descendants of the current node, the possibility arises of non-terminating loops. For example,

<xsl:template match="foo"> <xsl:apply-templates select="."/>

Implementations may be able to detect such loops in some cases, but the possibility exists that a stylesheet may enter a non-terminating loop that an implementation is unable to detect. This may present a denial of service security risk.

5.5 Conflict Resolution for Template Rules

It is possible for a source node to match more than one template rule. The template rule to be used is determined as follows:

  1. First, all matching template rules that have lower import precedence than the matching template rule or rules with the highest import precedence are eliminated from consideration.
  2. Next, all matching template rules that have lower priority than the matching template rule or rules with the highest priority are eliminated from consideration. The priority of a template rule is specified by the priority attribute on the template rule. The value of this must be a real number (positive or negative), matching the production Numberwith an optional leading minus sign (-). [Definition: The default priority is computed as follows:]
    • If the pattern contains multiple alternatives separated by|, then it is treated equivalently to a set of template rules, one for each alternative.
    • If the pattern has the form of a QName preceded by a ChildOrAttributeAxisSpecifieror has the formprocessing-instruction(Literal) preceded by a ChildOrAttributeAxisSpecifier, then the priority is 0.
    • If the pattern has the form NCName:* preceded by aChildOrAttributeAxisSpecifier, then the priority is -0.25.
    • Otherwise, if the pattern consists of just a NodeTest preceded by a ChildOrAttributeAxisSpecifier, then the priority is -0.5.
    • Otherwise, the priority is 0.5.
      Thus, the most common kind of pattern (a pattern that tests for a node with a particular type and a particular expanded-name) has priority 0. The next less specific kind of pattern (a pattern that tests for a node with a particular type and an expanded-name with a particular namespace URI) has priority -0.25. Patterns less specific than this (patterns that just tests for nodes with particular types) have priority -0.5. Patterns more specific than the most common kind of pattern have priority 0.5.

It is an error if this leaves more than one matching template rule. An XSLT processor may signal the error; if it does not signal the error, it must recover by choosing, from amongst the matching template rules that are left, the one that occurs last in the stylesheet.

5.6 Overriding Template Rules

A template rule that is being used to override a template rule in an imported stylesheet (see 5.5 Conflict Resolution for Template Rules) can use thexsl:apply-imports element to invoke the overridden template rule.

[Definition: At any point in the processing of a stylesheet, there is acurrent template rule. Whenever a template rule is chosen by matching a pattern, the template rule becomes the current template rule for the instantiation of the rule's template. When anxsl:for-each element is instantiated, the current template rule becomes null for the instantiation of the content of thexsl:for-each element.]

xsl:apply-imports processes the current node using only template rules that were imported into the stylesheet element containing the current template rule; the node is processed in the current template rule's mode. An xsl:apply-imports element may usexsl:with-param child elements to pass parameters to the matching template rule's template (see 11.6 Passing Parameters to Templates). It is an error ifxsl:apply-imports is instantiated when the current template rule is null.

For example, suppose the stylesheet doc.xsl contains a template rule for example elements:

<xsl:template match="example">

xsl:apply-templates/

Another stylesheet could import doc.xsl and modify the treatment of example elements as follows:

<xsl:import href="doc.xsl"/>

<xsl:template match="example">

xsl:apply-imports/

The combined effect would be to transform an exampleinto an element of the form:

...

5.7 Modes

Modes allow an element to be processed multiple times, each time producing a different result.

Both xsl:template and xsl:apply-templateshave an optional mode attribute. The value of themode attribute is a QName, which is expanded as described in 2.4 Qualified Names. If xsl:template does not have a match attribute, it must not have a modeattribute. If an xsl:apply-templates element has amode attribute, then it applies only to those template rules from xsl:template elements that have amode attribute with the same value; if anxsl:apply-templates element does not have amode attribute, then it applies only to those template rules from xsl:template elements that do not have amode attribute.

5.8 Built-in Template Rules

There is a built-in template rule to allow recursive processing to continue in the absence of a successful pattern match by an explicit template rule in the stylesheet. This template rule applies to both element nodes and the root node. The following shows the equivalent of the built-in template rule:

<xsl:template match="*|/"> xsl:apply-templates/

There is also a built-in template rule for each mode, which allows recursive processing to continue in the same mode in the absence of a successful pattern match by an explicit template rule in the stylesheet. This template rule applies to both element nodes and the root node. The following shows the equivalent of the built-in template rule for mode m.

<xsl:template match="*|/" mode="m"> <xsl:apply-templates mode="m"/>

There is also a built-in template rule for text and attribute nodes that copies text through:

<xsl:template match="text()|@*"> <xsl:value-of select="."/>

The built-in template rule for processing instructions and comments is to do nothing.

<xsl:template match="processing-instruction()|comment()"/>

The built-in template rule for namespace nodes is also to do nothing. There is no pattern that can match a namespace node; so, the built-in template rule is the only template rule that is applied for namespace nodes.

The built-in template rules are treated as if they were imported implicitly before the stylesheet and so have lower import precedence than all other template rules. Thus, the author can override a built-in template rule by including an explicit template rule.

6 Named Templates

Templates can be invoked by name. An xsl:templateelement with a name attribute specifies a named template. The value of the name attribute is a QName, which is expanded as described in 2.4 Qualified Names. If an xsl:template element has a name attribute, it may, but need not, also have amatch attribute. An xsl:call-templateelement invokes a template by name; it has a requiredname attribute that identifies the template to be invoked. Unlike xsl:apply-templates,xsl:call-template does not change the current node or the current node list.

The match, mode and priority attributes on anxsl:template element do not affect whether the template is invoked by an xsl:call-template element. Similarly, the name attribute on an xsl:templateelement does not affect whether the template is invoked by anxsl:apply-templates element.

It is an error if a stylesheet contains more than one template with the same name and same import precedence.

7 Creating the Result Tree

This section describes instructions that directly create nodes in the result tree.

7.1 Creating Elements and Attributes

7.1.1 Literal Result Elements

In a template, an element in the stylesheet that does not belong to the XSLT namespace and that is not an extension element (see 14.1 Using Extension Elements) is instantiated to create an element node with the same expanded-name. The content of the element is a template, which is instantiated to give the content of the created element node. The created element node will have the attribute nodes that were present on the element node in the stylesheet tree, other than attributes with names in the XSLT namespace.

The created element node will also have a copy of the namespace nodes that were present on the element node in the stylesheet tree with the exception of any namespace node whose string-value is the XSLT namespace URI (http://www.w3.org/1999/XSL/Transform), a namespace URI declared as an extension namespace (see 14.1 Using Extension Elements), or a namespace URI designated as an excluded namespace. A namespace URI is designated as an excluded namespace by using an exclude-result-prefixes attribute on an xsl:stylesheet element or anxsl:exclude-result-prefixes attribute on a literal result element. The value of both these attributes is a whitespace-separated list of namespace prefixes. The namespace bound to each of the prefixes is designated as an excluded namespace. It is an error if there is no namespace bound to the prefix on the element bearing theexclude-result-prefixes orxsl:exclude-result-prefixes attribute. The default namespace (as declared by xmlns) may be designated as an excluded namespace by including #default in the list of namespace prefixes. The designation of a namespace as an excluded namespace is effective within the subtree of the stylesheet rooted at the element bearing the exclude-result-prefixes orxsl:exclude-result-prefixes attribute; a subtree rooted at an xsl:stylesheet element does not include any stylesheets imported or included by children of that xsl:stylesheet element.

Note:

When a stylesheet uses a namespace declaration only for the purposes of addressing the source tree, specifying the prefix in theexclude-result-prefixes attribute will avoid superfluous namespace declarations in the result tree.

The value of an attribute of a literal result element is interpreted as an attribute value template: it can contain expressions contained in curly braces ({}).

| Editorial note | | | ------------------------------------------------- | | | Add note warning against using AVTs withxml:base. | |

[Definition: A namespace URI in the stylesheet tree that is being used to specify a namespace URI in the result tree is called a literal namespace URI.] This applies to:

[Definition: A stylesheet can use thexsl:namespace-alias element to declare that one namespace URI is an alias for another namespace URI.] When a literal namespace URI has been declared to be an alias for another namespace URI, then the namespace URI in the result tree will be the namespace URI that the literal namespace URI is an alias for, instead of the literal namespace URI itself. The xsl:namespace-aliaselement declares that the namespace URI bound to the prefix specified by the stylesheet-prefix attribute is an alias for the namespace URI bound to the prefix specified by theresult-prefix attribute. Thus, thestylesheet-prefix attribute specifies the namespace URI that will appear in the stylesheet, and theresult-prefix attribute specifies the corresponding namespace URI that will appear in the result tree. The default namespace (as declared by xmlns) may be specified by using #default instead of a prefix. If a namespace URI is declared to be an alias for multiple different namespace URIs, then the declaration with the highest import precedence is used. It is an error if there is more than one such declaration. An XSLT processor may signal the error; if it does not signal the error, it must recover by choosing, from amongst the declarations with the highest import precedence, the one that occurs last in the stylesheet.

When literal result elements are being used to create element, attribute, or namespace nodes that use the XSLT namespace URI, the stylesheet must use an alias. For example, the stylesheet

<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:axsl="" title="undefined" rel="noopener noreferrer">http://www.w3.org/1999/XSL/TransformAlias">

<xsl:namespace-alias stylesheet-prefix="axsl" result-prefix="xsl"/>

<xsl:template match="/"> axsl:stylesheet xsl:apply-templates/

<xsl:template match="block"> <axsl:template match="{.}"> fo:blockaxsl:apply-templates/

will generate an XSLT stylesheet from a document of the form:

p h1 h2 h3 h4

Note:

It may be necessary also to use aliases for namespaces other than the XSLT namespace URI. For example, literal result elements belonging to a namespace dealing with digital signatures might cause XSLT stylesheets to be mishandled by general-purpose security software; using an alias for the namespace would avoid the possibility of such mishandling.

7.1.2 Creating Elements with xsl:element

The xsl:element element allows an element to be created with a computed name. The expanded-name of the element to be created is specified by a required nameattribute and an optional namespace attribute. The content of the xsl:element element is a template for the attributes and children of the created element.

The name attribute is interpreted as an attribute value template. It is an error if the string that results from instantiating the attribute value template is not a QName. An XSLT processor may signal the error; if it does not signal the error, then it must recover by making the result of instantiating the xsl:elementelement be the sequence of nodes created by instantiating the content of the xsl:element element, excluding any initial attribute nodes. If the namespace attribute is not present then the QName is expanded into an expanded-name using the namespace declarations in effect for the xsl:element element, including any default namespace declaration.

If the namespace attribute is present, then it also is interpreted as an attribute value template. The string that results from instantiating the attribute value template should be a URI reference. It is not an error if the string is not a syntactically legal URI reference. If the string is empty, then the expanded-name of the element has a null namespace URI. Otherwise, the string is used as the namespace URI of the expanded-name of the element to be created. The local part of theQName specified by thename attribute is used as the local part of the expanded-name of the element to be created.

XSLT processors may make use of the prefix of the QName specified in thename attribute when selecting the prefix used for outputting the created element as XML; however, they are not required to do so.

7.1.3 Creating Attributes with xsl:attribute

The xsl:attribute element can be used to add attributes to result elements whether created by literal result elements in the stylesheet or by instructions such asxsl:element. The expanded-name of the attribute to be created is specified by a required nameattribute and an optional namespace attribute. Instantiating an xsl:attribute element adds an attribute node to the containing result element node. The content of thexsl:attribute element is a template for the value of the created attribute.

The name attribute is interpreted as an attribute value template. It is an error if the string that results from instantiating the attribute value template is not a QName or is the stringxmlns. An XSLT processor may signal the error; if it does not signal the error, it must recover by not adding the attribute to the result tree. If the namespace attribute is not present, then the QName is expanded into an expanded-name using the namespace declarations in effect for the xsl:attribute element, _not_including any default namespace declaration.

If the namespace attribute is present, then it also is interpreted as an attribute value template. The string that results from instantiating it should be a URI reference. It is not an error if the string is not a syntactically legal URI reference. If the string is empty, then the expanded-name of the attribute has a null namespace URI. Otherwise, the string is used as the namespace URI of the expanded-name of the attribute to be created. The local part of the QName specified by thename attribute is used as the local part of the expanded-name of the attribute to be created.

XSLT processors may make use of the prefix of the QName specified in thename attribute when selecting the prefix used for outputting the created attribute as XML; however, they are not required to do so and, if the prefix is xmlns, they must not do so. Thus, although it is not an error to do:

<xsl:attribute name="xmlns:xsl" namespace="whatever">http://www.w3.org/1999/XSL/Transform

it will not result in a namespace declaration being output.

Adding an attribute to an element replaces any existing attribute of that element with the same expanded-name.

The following are all errors:

Note:

When an xsl:attribute contains a text node with a newline, then the XML output must contain a character reference. For example,

<xsl:attribute name="a">x y

will result in the output

(or with any equivalent character reference). The XML output cannot be

This is because XML 1.0 requires newline characters in attribute values to be normalized into spaces but requires character references to newline characters not to be normalized. The attribute values in the data model represent the attribute value after normalization. If a newline occurring in an attribute value in the tree were output as a newline character rather than as character reference, then the attribute value in the tree created by reparsing the XML would contain a space not a newline, which would mean that the tree had not been output correctly.

7.1.4 Named Attribute Sets

The xsl:attribute-set element defines a named set of attributes. The name attribute specifies the name of the attribute set. The value of the name attribute is a QName, which is expanded as described in 2.4 Qualified Names. The content of the xsl:attribute-setelement consists of zero or more xsl:attribute elements that specify the attributes in the set.

Attribute sets are used by specifying ause-attribute-sets attribute on xsl:element,xsl:copy (see 7.5 Copying) orxsl:attribute-set elements. The value of theuse-attribute-sets attribute is a whitespace-separated list of names of attribute sets. Each name is specified as a QName, which is expanded as described in 2.4 Qualified Names. Specifying ause-attribute-sets attribute is equivalent to addingxsl:attribute elements for each of the attributes in each of the named attribute sets to the beginning of the content of the element with the use-attribute-sets attribute, in the same order in which the names of the attribute sets are specified in the use-attribute-sets attribute. It is an error if use of use-attribute-sets attributes onxsl:attribute-set elements causes an attribute set to directly or indirectly use itself.

Attribute sets can also be used by specifying anxsl:use-attribute-sets attribute on a literal result element. The value of the xsl:use-attribute-setsattribute is a whitespace-separated list of names of attribute sets. The xsl:use-attribute-sets attribute has the same effect as the use-attribute-sets attribute onxsl:element with the additional rule that attributes specified on the literal result element itself are treated as if they were specified by xsl:attribute elements before any actual xsl:attribute elements but after anyxsl:attribute elements implied by thexsl:use-attribute-sets attribute. Thus, for a literal result element, attributes from attribute sets named in anxsl:use-attribute-sets attribute will be added first, in the order listed in the attribute; next, attributes specified on the literal result element will be added; finally, any attributes specified by xsl:attribute elements will be added. Since adding an attribute to an element replaces any existing attribute of that element with the same name, this means that attributes specified in attribute sets can be overridden by attributes specified on the literal result element itself.

The template within each xsl:attribute element in anxsl:attribute-set element is instantiated each time the attribute set is used; it is instantiated using the same current node and current node list as is used for instantiating the element bearing the use-attribute-sets orxsl:use-attribute-sets attribute. However, it is the position in the stylesheet of the xsl:attribute element rather than of the element bearing the use-attribute-setsor xsl:use-attribute-sets attribute that determines which variable bindings are visible (see 11 Variables and Parameters); thus, only variables and parameters declared by top-level xsl:variable andxsl:param elements are visible.

The following example creates a named attribute settitle-style and uses it in a template rule.

<xsl:template match="chapter/heading"> <fo:block quadding="start" xsl:use-attribute-sets="title-style"> xsl:apply-templates/

<xsl:attribute-set name="title-style"> <xsl:attribute name="font-size">12pt <xsl:attribute name="font-weight">bold

Multiple definitions of an attribute set with the same expanded-name are merged. An attribute from a definition that has higher import precedencetakes precedence over an attribute from a definition that has lowerimport precedence. It is an error if there are two attribute sets that have the same expanded-name and equal import precedence and that both contain the same attribute, unless there is a definition of the attribute set with higher import precedence that also contains the attribute. An XSLT processor may signal the error; if it does not signal the error, it must recover by choosing from amongst the definitions that specify the attribute that have the highest import precedence the one that was specified last in the stylesheet. Where the attributes in an attribute set were specified is relevant only in merging the attributes into the attribute set; it makes no difference when the attribute set is used. For each attribute set name occurring in a use-attribute-sets attribute on an xsl:attribute-set element, all definitions of an attribute set with that name must be merged before theuse-attribute-sets attribute is replaced by the equivalent sequence of xsl:attribute child elements. Anyuse-attribute-sets attribute on anxsl:attribute-set element must be replaced by the equivalent sequence of xsl:attribute child elements_before_ that xsl:attribute-set element is merged with other xsl:attribute-set elements with the same expanded-name. When xsl:attribute-set elements with the same expanded-name are merged, any xsl:attributechild elements added to replace a use-attribute-setsattribute are treated exactly as if they had originally been specified in the stylesheet as child elements.

7.2 Creating Text

A template can also contain text nodes. Each text node in a template remaining after whitespace has been stripped as specified in3.4 Whitespace Stripping will create a text node with the same string-value in the result tree. Adjacent text nodes in the result tree are automatically merged.

Note that text is processed at the tree level. Thus, markup of&lt; in a template will be represented in the stylesheet tree by a text node that includes the character<. This will create a text node in the result tree that contains a < character, which will be represented by the markup &lt; (or an equivalent character reference) when the result tree is externalized as an XML document (unless output escaping is disabled as described in 16.4 Disabling Output Escaping).

Literal data characters may also be wrapped in anxsl:text element. This wrapping may change what whitespace characters are stripped (see 3.4 Whitespace Stripping) but does not affect how the characters are handled by the XSLT processor thereafter.

Note:

The xml:lang and xml:spaceattributes are not treated specially by XSLT. In particular,

7.3 Creating Processing Instructions

The xsl:processing-instruction element is instantiated to create a processing instruction node. The content of thexsl:processing-instruction element is a template for the string-value of the processing instruction node. Thexsl:processing-instruction element has a requiredname attribute that specifies the name of the processing instruction node. The value of the name attribute is interpreted as an attribute value template.

For example, this

<xsl:processing-instruction name="xml-stylesheet">href="book.css" type="text/css"

would create the processing instruction

It is an error if the string that results from instantiating thename attribute is not both an NCName and a PITarget. An XSLT processor may signal the error; if it does not signal the error, it must recover by not adding the processing instruction to the result tree.

Note:

This means that xsl:processing-instructioncannot be used to output an XML declaration. Thexsl:output element should be used instead (see 16 Output).

It is an error if instantiating the content ofxsl:processing-instruction creates nodes other than text nodes. An XSLT processor may signal the error; if it does not signal the error, it must recover by ignoring the offending nodes together with their content.

It is an error if the result of instantiating the content of thexsl:processing-instruction contains the string?>. An XSLT processor may signal the error; if it does not signal the error, it must recover by inserting a space after any occurrence of ? that is followed by a >.

7.4 Creating Comments

The xsl:comment element is instantiated to create a comment node in the result tree. The content of thexsl:comment element is a template for the string-value of the comment node.

For example, this

xsl:commentThis file is automatically generated. Do not edit!

would create the comment

It is an error if instantiating the content ofxsl:comment creates nodes other than text nodes. An XSLT processor may signal the error; if it does not signal the error, it must recover by ignoring the offending nodes together with their content.

It is an error if the result of instantiating the content of thexsl:comment contains the string -- or ends with -. An XSLT processor may signal the error; if it does not signal the error, it must recover by inserting a space after any occurrence of - that is followed by another- or that ends the comment.

7.5 Copying

The xsl:copy element provides an easy way of copying the current node. Instantiating the xsl:copy element creates a copy of the current node. The namespace nodes of the current node are automatically copied as well, but the attributes and children of the node are not automatically copied. The content of thexsl:copy element is a template for the attributes and children of the created node; the content is instantiated only for nodes of types that can have attributes or children (i.e. root nodes and element nodes).

The xsl:copy element may have ause-attribute-sets attribute (see 7.1.4 Named Attribute Sets). This is used only when copying element nodes.

The root node is treated specially because the root node of the result tree is created implicitly. When the current node is the root node, xsl:copy will not create a root node, but will just use the content template.

For example, the identity transformation can be written usingxsl:copy as follows:

<xsl:template match="@*|node()"> xsl:copy <xsl:apply-templates select="@*|node()"/>

When the current node is an attribute, then if it would be an error to use xsl:attribute to create an attribute with the same name as the current node, then it is also an error to usexsl:copy (see 7.1.3 Creating Attributes with xsl:attribute).

When the current node is a namespace node, then copying it adds a namespace node to the containing node in the result tree. It is an error if this containing node is not an element; implementations may either signal the error or ignore the namespace node. It is an error to add a namespace node to an element after children have been added to it or after attributes have been added to it; implementations may either signal an error or ignore the namespace node. It is an error to add a namespace node to an element if the element already has a namespace node with the same name, unless both namespace nodes have the same string-value, in which case the duplicate is ignored. It is an error to add a namespace node to an element if the namespace node has a null name and the element has a null namespace URI.

The following example shows how xml:lang attributes can be easily copied through from source to result. If a stylesheet defines the following named template:

<xsl:template name="apply-templates-copy-lang"> <xsl:for-each select="@xml:lang"> xsl:copy/ xsl:apply-templates/

then it can simply do

<xsl:call-template name="apply-templates-copy-lang"/>

instead of

when it wants to copy the xml:lang attribute.

7.6 Computing Generated Text

Within a template, the xsl:value-of element can be used to compute generated text, for example by extracting text from the source tree or by inserting the value of a variable. Thexsl:value-of element does this with an expression that is specified as the value of the select attribute. Expressions can also be used inside attribute values of literal result elements by enclosing the expression in curly braces ({}).

7.6.1 Generating Text with xsl:value-of

The xsl:value-of element is instantiated to create a text node in the result tree. The required selectattribute is an expression; this expression is evaluated and the resulting object is converted to a string as if by a call to the string function. The string specifies the string-value of the created text node. If the string is empty, no text node will be created. The created text node will be merged with any adjacent text nodes.

The xsl:copy-of element can be used to copy a node-set over to the result tree without converting it to a string. See 11.3 Using Values of Variables and Parameters with xsl:copy-of.

For example, the following creates an HTML paragraph from aperson element with given-name andfamily-name attributes. The paragraph will contain the value of the given-name attribute of the current node followed by a space and the value of the family-name attribute of the current node.

<xsl:template match="person">

<xsl:value-of select="@given-name"/> xsl:text <xsl:value-of select="@family-name"/>

For another example, the following creates an HTML paragraph from aperson element with given-name andfamily-name children elements. The paragraph will contain the string-value of the first given-name child element of the current node followed by a space and the string-value of the first family-name child element of the current node.

<xsl:template match="person">

<xsl:value-of select="given-name"/> xsl:text <xsl:value-of select="family-name"/>

The following precedes each procedure element with a paragraph containing the security level of the procedure. It assumes that the security level that applies to a procedure is determined by asecurity attribute on the procedure element or on an ancestor element of the procedure. It also assumes that if more than one such element has a security attribute then the security level is determined by the element that is closest to the procedure.

<xsl:template match="procedure"> fo:block <xsl:value-of select="ancestor-or-self::*[@security][1]/@security"/> xsl:apply-templates/

7.6.2 Attribute Value Templates

[Definition: In an attribute value that is interpreted as an**attribute value template**, such as an attribute of a literal result element, an expression can be used by surrounding the expression with curly braces ({})]. The attribute value template is instantiated by replacing the expression together with surrounding curly braces by the result of evaluating the expression and converting the resulting object to a string as if by a call to the string function. Curly braces are not recognized in an attribute value in an XSLT stylesheet unless the attribute is specifically stated to be one that is interpreted as an attribute value template; in an element syntax summary, the value of such attributes is surrounded by curly braces.

Note:

Not all attributes are interpreted as attribute value templates. Attributes whose value is an expression or pattern, attributes of top-level elements and attributes that refer to named XSLT objects are not interpreted as attribute value templates. In addition, xmlns attributes are not interpreted as attribute value templates; it would not be conformant with the XML Namespaces Recommendation to do this. The xsl:output element is an exception: although it is a top-level element, its attributes are all interpreted as attribute value templates for consistency withxsl:document.

The following example creates an img result element from a photograph element in the source; the value of thesrc attribute of the img element is computed from the value of the image-dir variable and the string-value of the href child of thephotograph element; the value of the widthattribute of the img element is computed from the value of the width attribute of the size child of the photograph element:

<xsl:variable name="image-dir">/images

<xsl:template match="photograph">

With this source

headquarters.jpg

the result would be

When an attribute value template is instantiated, a double left or right curly brace outside an expression will be replaced by a single curly brace. It is an error if a right curly brace occurs in an attribute value template outside an expression without being followed by a second right curly brace. A right curly brace inside a Literal in an expression is not recognized as terminating the expression.

Curly braces are not recognized recursively inside expressions. For example:

is not allowed. Instead, use simply:

7.7 Numbering

The xsl:number element is used to insert a formatted number into the result tree. The number to be inserted may be specified by an expression. The value attribute contains an expression. The expression is evaluated and the resulting object is converted to a number as if by a call to the number function. It is an error if the number is NaN, infinite or less than 0.5; an XSLT processor may signal the error; if it does not signal the error, it must recover by converting the number to a string as if by a call to the string function and inserting the resulting string into the result tree. Otherwise, the number is rounded to an integer and then converted to a string using the attributes specified in 7.7.1 Number to String Conversion Attributes; in this context, the value of each of these attributes is interpreted as an attribute value template. After conversion, the resulting string is inserted in the result tree. For example, the following example numbers a sorted list:

<xsl:template match="items"> <xsl:for-each select="item"> <xsl:sort select="."/>

<xsl:number value="position()" format="1. "/> <xsl:value-of select="."/>

If no value attribute is specified, then thexsl:number element inserts a number based on the position of the current node in the source tree. The following attributes control how the current node is to be numbered:

In addition, the attributes specified in 7.7.1 Number to String Conversion Attributesare used for number to string conversion, as in the case when thevalue attribute is specified.

The xsl:number element first constructs a list of positive integers using the level, count andfrom attributes:

The list of numbers is then converted into a string using the attributes specified in 7.7.1 Number to String Conversion Attributes; in this context, the value of each of these attributes is interpreted as an attribute value template. After conversion, the resulting string is inserted in the result tree.

The following would number the items in an ordered list:

<xsl:template match="ol/item"> fo:block xsl:number/xsl:text. xsl:apply-templates/ xsl:template

The following two rules would number title elements. This is intended for a document that contains a sequence of chapters followed by a sequence of appendices, where both chapters and appendices contain sections, which in turn contain subsections. Chapters are numbered 1, 2, 3; appendices are numbered A, B, C; sections in chapters are numbered 1.1, 1.2, 1.3; sections in appendices are numbered A.1, A.2, A.3.

<xsl:template match="title"> fo:block <xsl:number level="multiple" count="chapter|section|subsection" format="1.1 "/> xsl:apply-templates/

<xsl:template match="appendix//title" priority="1"> fo:block <xsl:number level="multiple" count="appendix|section|subsection" format="A.1 "/> xsl:apply-templates/

The following example numbers notes sequentially within a chapter:

<xsl:template match="note"> fo:block <xsl:number level="any" from="chapter" format="(1) "/> xsl:apply-templates/

The following example would number H4 elements in HTML with a three-part label:

<xsl:template match="H4"> fo:block <xsl:number level="any" from="H1" count="H2"/> xsl:text. <xsl:number level="any" from="H2" count="H3"/> xsl:text. <xsl:number level="any" from="H3" count="H4"/> xsl:text xsl:apply-templates/

7.7.1 Number to String Conversion Attributes

The following attributes are used to control conversion of a list of numbers into a string. The numbers are integers greater than 0. The attributes are all optional.

The main attribute is format. The default value for the format attribute is 1. Theformat attribute is split into a sequence of tokens where each token is a maximal sequence of alphanumeric characters or a maximal sequence of non-alphanumeric characters. Alphanumeric means any character that has a Unicode category of Nd, Nl, No, Lu, Ll, Lt, Lm or Lo. The alphanumeric tokens (format tokens) specify the format to be used for each number in the list. If the first token is a non-alphanumeric token, then the constructed string will start with that token; if the last token is non-alphanumeric token, then the constructed string will end with that token. Non-alphanumeric tokens that occur between two format tokens are separator tokens that are used to join numbers in the list. The nth format token will be used to format the nth number in the list. If there are more numbers than format tokens, then the last format token will be used to format remaining numbers. If there are no format tokens, then a format token of 1 is used to format all numbers. The format token specifies the string to be used to represent the number 1. Each number after the first will be separated from the preceding number by the separator token preceding the format token used to format that number, or, if there are no separator tokens, then by . (a period character).

Format tokens are a superset of the allowed values for thetype attribute for the OL element in HTML 4.0 and are interpreted as follows:

When numbering with an alphabetic sequence, the langattribute specifies which language's alphabet is to be used; it has the same range of values as xml:lang [XML]; if no lang value is specified, the language should be determined from the system environment. Implementers should document for which languages they support numbering.

Note:

Implementers should not make any assumptions about how numbering works in particular languages and should properly research the languages that they wish to support. The numbering conventions of many languages are very different from English.

The letter-value attribute disambiguates between numbering sequences that use letters. In many languages there are two commonly used numbering sequences that use letters. One numbering sequence assigns numeric values to letters in alphabetic sequence, and the other assigns numeric values to each letter in some other manner traditional in that language. In English, these would correspond to the numbering sequences specified by the format tokens aand i. In some languages, the first member of each sequence is the same, and so the format token alone would be ambiguous. A value of alphabetic specifies the alphabetic sequence; a value of traditional specifies the other sequence. If the letter-value attribute is not specified, then it is implementation-dependent how any ambiguity is resolved.

Note:

It is possible for two conforming XSLT processors not to convert a number to exactly the same string. Some XSLT processors may not support some languages. Furthermore, there may be variations possible in the way conversions are performed for any particular language that are not specifiable by the attributes on xsl:number. Future versions of XSLT may provide additional attributes to provide control over these variations. Implementations may also use implementation-specific namespaced attributes onxsl:number for this.

The grouping-separator attribute gives the separator used as a grouping (e.g. thousands) separator in decimal numbering sequences, and the optional grouping-size specifies the size (normally 3) of the grouping. For example,grouping-separator="," and grouping-size="3"would produce numbers of the form 1,000,000. If only one of the grouping-separator and grouping-sizeattributes is specified, then it is ignored.

Here are some examples of conversion specifications:

8 Repetition

When the result has a known regular structure, it is useful to be able to specify directly the template for selected nodes. Thexsl:for-each instruction contains a template, which is instantiated for each node selected by the expression specified by theselect attribute. The select attribute is required. The expression must evaluate to a node-set. The template is instantiated with the selected node as the current node, and with a list of all of the selected nodes as the current node list. The nodes are processed in document order, unless a sorting specification is present (see 10 Sorting).

For example, given an XML document with this structure

... ... ... ... ... ...

the following would create an HTML document containing a table with a row for each customer element

<xsl:template match="/"> Customers

<xsl:for-each select="customers/customer"> <xsl:for-each select="order">
<xsl:apply-templates select="name"/> xsl:apply-templates/

9 Conditional Processing

There are two instructions in XSLT that support conditional processing in a template: xsl:if andxsl:choose. The xsl:if instruction provides simple if-then conditionality; the xsl:choose instruction supports selection of one choice when there are several possibilities.

9.1 Conditional Processing with xsl:if

The xsl:if element has a test attribute, which specifies an expression. The content is a template. The expression is evaluated and the resulting object is converted to a boolean as if by a call to the boolean function. If the result is true, then the content template is instantiated; otherwise, nothing is created. In the following example, the names in a group of names are formatted as a comma separated list:

<xsl:template match="namelist/name"> xsl:apply-templates/ <xsl:if test="not(position()=last())">,

The following colors every other table row yellow:

<xsl:template match="item"> <xsl:if test="position() mod 2 = 0"> <xsl:attribute name="bgcolor">yellow xsl:apply-templates/

9.2 Conditional Processing with xsl:choose

The xsl:choose element selects one among a number of possible alternatives. It consists of a sequence ofxsl:when elements followed by an optionalxsl:otherwise element. Each xsl:whenelement has a single attribute, test, which specifies anexpression. The content of thexsl:when and xsl:otherwise elements is a template. When an xsl:choose element is processed, each of the xsl:when elements is tested in turn, by evaluating the expression and converting the resulting object to a boolean as if by a call to the boolean function. The content of the first, and only the first, xsl:when element whose test is true is instantiated. If no xsl:when is true, the content of the xsl:otherwise element is instantiated. If no xsl:when element is true, and noxsl:otherwise element is present, nothing is created.

The following example enumerates items in an ordered list using arabic numerals, letters, or roman numerals depending on the depth to which the ordered lists are nested.

<xsl:template match="orderedlist/listitem"> <fo:list-item indent-start='2pi'> fo:list-item-label <xsl:variable name="level" select="count(ancestor::orderedlist) mod 3"/> xsl:choose <xsl:when test='$level=1'> <xsl:number format="i"/> <xsl:when test='$level=2'> <xsl:number format="a"/> xsl:otherwise <xsl:number format="1"/> xsl:text. fo:list-item-body xsl:apply-templates/

10 Sorting

Sorting is specified by adding xsl:sort elements as children of an xsl:apply-templates orxsl:for-each element. The first xsl:sortchild specifies the primary sort key, the second xsl:sortchild specifies the secondary sort key and so on. When anxsl:apply-templates or xsl:for-each element has one or more xsl:sort children, then instead of processing the selected nodes in document order, it sorts the nodes according to the specified sort keys and then processes them in sorted order. When used in xsl:for-each, xsl:sortelements must occur first. When a template is instantiated byxsl:apply-templates and xsl:for-each, thecurrent node listconsists of the complete list of nodes being processed in sorted order.

xsl:sort has a select attribute whose value is an expression. For each node to be processed, the expression is evaluated with that node as the current node and with the complete list of nodes being processed in document order as the current node list. The resulting object is converted to a string as if by a call to the string function; this string is used as the sort key for that node. The default value of theselect attribute is ., which will cause the string-value of the current node to be used as the sort key.

This string serves as a sort key for the node. The following optional attributes on xsl:sort control how the list of sort keys are sorted; the values of all of these attributes are interpreted as attribute value templates.

Note:

It is possible for two conforming XSLT processors not to sort exactly the same. Some XSLT processors may not support some languages. Furthermore, there may be variations possible in the sorting of any particular language that are not specified by the attributes on xsl:sort, for example, whether Hiragana or Katakana is sorted first in Japanese. Future versions of XSLT may provide additional attributes to provide control over these variations. Implementations may also use implementation-specific namespaced attributes on xsl:sort for this.

Note:

It is recommended that implementers consult [UNICODE TR10] for information on internationalized sorting.

The sort must be stable: in the sorted list of nodes, any sub list that has sort keys that all compare equal must be in document order.

For example, suppose an employee database has the form

James Clark ...

Then a list of employees sorted by name could be generated using:

<xsl:template match="employees">

<xsl:template match="employee">

  • <xsl:value-of select="name/given"/> xsl:text <xsl:value-of select="name/family"/>
  • 11 Variables and Parameters

    A variable is a name that may be bound to a value. The value to which a variable is bound (the value of the variable) can be an object of any of the types that can be returned by expressions. There are two elements that can be used to bind variables:xsl:variable and xsl:param. The difference is that the value specified on the xsl:param variable is only a default value for the binding; when the template or stylesheet within which the xsl:param element occurs is invoked, parameters may be passed that are used in place of the default values.

    Both xsl:variable and xsl:param have a required name attribute, which specifies the name of the variable. The value of the name attribute is a QName, which is expanded as described in 2.4 Qualified Names.

    For any use of these variable-binding elements, there is a region of the stylesheet tree within which the binding is visible. The set of variable bindings in scope for an expression consists of those bindings that are visible at the point in the stylesheet where the expression occurs.

    11.1 Result Tree Fragments

    Note:

    The result tree fragment data-type has been removed in XSLT 1.1.

    11.2 Values of Variables and Parameters

    A variable-binding element can specify the value of the variable in three alternative ways.

    Note:

    When a variable is used to select nodes by position, be careful not to do:

    <xsl:variable name="n">2 ... <xsl:value-of select="item[$n]"/>

    This will output the value of the first item element, because the variable n will be bound to a node-set, not a number. Instead, do either

    <xsl:variable name="n" select="2"/> ... <xsl:value-of select="item[$n]"/>

    or

    <xsl:variable name="n">2 ... <xsl:value-of select="item[position()=$n]"/>

    Note:

    One convenient way to specify the empty node-set as the default value of a parameter is:

    <xsl:param name="x" select="/.."/>

    11.3 Using Values of Variables and Parameters withxsl:copy-of

    The xsl:copy-of element can be used to insert a node-set into the result tree, without first converting it to a string as xsl:value-of does (see7.6.1 Generating Text with xsl:value-of). The required selectattribute contains an expression. When the result of evaluating the expression is a node-set, all the nodes in the set are copied in document order into the result tree; copying an element node copies the attribute nodes, namespace nodes and children of the element node as well as the element node itself; a root node is copied by copying its children. For copying an attribute or namespace node, the same rules apply as with xsl:copy (see 7.5 Copying). When the result is not a node-set, the result is converted to a string and then inserted into the result tree, as with xsl:value-of.

    11.4 Top-level Variables and Parameters

    Both xsl:variable and xsl:param are allowed as top-level elements. A top-level variable-binding element declares a global variable that is visible everywhere (except where it is shadowed by another binding). A top-level xsl:param element declares a parameter to the stylesheet; XSLT does not define the mechanism by which parameters are passed to the stylesheet. If a stylesheet contains more than one binding for a variable of a particular name, then the binding with the highest import precedence is used. It is an error if a stylesheet contains more than one binding of a top-level variable with the same name and same import precedence. At the top-level, the expression or template specifying the variable value is evaluated with the same context as that used to process the root node of the source document: the current node is the root node of the source document and the current node list is a list containing just the root node of the source document. If the template or expression specifying the value of a global variable x references a global variable y, then the value for y must be computed before the value of x. It is an error if it is impossible to do this for all global variable definitions; in other words, it is an error if the definitions are circular.

    This example declares a global variable para-font-size, which it references in an attribute value template.

    <xsl:variable name="para-font-size">12pt

    <xsl:template match="para"> <fo:block font-size="{$para-font-size}"> xsl:apply-templates/

    11.5 Variables and Parameters within Templates

    As well as being allowed at the top-level, bothxsl:variable and xsl:param are also allowed in templates. xsl:variable is allowed anywhere within a template that an instruction is allowed. In this case, the binding is visible for all following siblings and their descendants. Note that the binding is not visible for the xsl:variableelement itself. xsl:param is allowed as a child at the beginning of an xsl:template element. In this context, the binding is visible for all following siblings and their descendants. Note that the binding is not visible for thexsl:param element itself.

    [Definition: A bindingshadows another binding if the binding occurs at a point where the other binding is visible, and the bindings have the same name.] It is an error if a binding established by anxsl:variable or xsl:param element within a template shadows another binding established by an xsl:variable or xsl:paramelement also within the template. It is not an error if a binding established by an xsl:variable or xsl:paramelement in a template shadowsanother binding established by an xsl:variable orxsl:param top-levelelement. In this case, the top-level binding will not be visible in the region of the stylesheet where it is shadowed by the other binding. Thus, the following is an error:

    <xsl:template name="foo"> <xsl:param name="x" select="1"/> <xsl:variable name="x" select="2"/>

    However, the following is allowed:

    <xsl:param name="x" select="1"/> <xsl:template name="foo"> <xsl:variable name="x" select="2"/>

    Note:

    The nearest equivalent in Java to an xsl:variableelement in a template is a final local variable declaration with an initializer. For example,

    <xsl:variable name="x" select="'value'"/>

    has similar semantics to

    final Object x = "value";

    XSLT does not provide an equivalent to the Java assignment operator

    because this would make it harder to create an implementation that processes a document other than in a batch-like way, starting at the beginning and continuing through to the end.

    11.6 Passing Parameters to Templates

    Parameters are passed to templates using thexsl:with-param element. The required nameattribute specifies the name of the parameter (the variable the value of whose binding is to be replaced). The value of thename attribute is a QName, which is expanded as described in 2.4 Qualified Names. xsl:with-param is allowed withinxsl:call-template,xsl:apply-templates andxsl:apply-imports. It is an error if a single xsl:call-template,xsl:apply-templates or xsl:apply-importselement contains more than one xsl:with-param element with the same name. The value of the parameter is specified in the same way as for xsl:variable andxsl:param. The current node and current node list used for computing the value specified by xsl:with-paramelement is the same as that used for thexsl:apply-templates,xsl:apply-imports, orxsl:call-templateelement within which it occurs. It is not an error to pass a parameter x to a template that does not have anxsl:param element for x; the parameter is simply ignored.

    This example defines a named template for anumbered-block with an argument to control the format of the number.

    <xsl:template name="numbered-block"> <xsl:param name="format">1. fo:block <xsl:number format="{$format}"/> xsl:apply-templates/

    <xsl:template match="ol//ol/li"> <xsl:call-template name="numbered-block"> <xsl:with-param name="format">a.

    12 Additional Functions

    This section describes XSLT-specific additions to the core XPath function library. Some of these additional functions also make use of information specified by top-levelelements in the stylesheet; this section also describes these elements.

    12.1 Multiple Source Documents

    node-set document(object, node-set?)

    The document function allows access to XML documents other than the main source document.

    When the document function has exactly one argument and the argument is a node-set, then the result is the union, for each node in the argument node-set, of the result of calling thedocument function with the first argument being the string-valueof the node, and the second argument being a node-set with the node as its only member. When the document function has two arguments and the first argument is a node-set, then the result is the union, for each node in the argument node-set, of the result of calling the document function with the first argument being the string-value of the node, and with the second argument being the second argument passed to thedocument function.

    When the first argument to the documentfunction is not a node-set, the first argument is converted to a string as if by a call to the string function. This string is treated as a URI reference; the resource identified by the URI is retrieved. The data resulting from the retrieval action is parsed as an XML document and a tree is constructed in accordance with the data model (see 3 Data Model). If there is an error retrieving the resource, then the XSLT processor may signal an error; if it does not signal an error, it must recover by returning an empty node-set. One possible kind of retrieval error is that the XSLT processor does not support the URI scheme used by the URI. An XSLT processor is not required to support any particular URI schemes. The documentation for an XSLT processor should specify which URI schemes the XSLT processor supports.

    If the URI reference does not contain a fragment identifier, then a node-set containing just the root node of the document is returned. If the URI reference does contain a fragment identifier, the function returns a node-set containing the nodes in the tree identified by the fragment identifier of the URI reference. The semantics of the fragment identifier are dependent on the media type of the result of retrieving the URI. If there is an error in processing the fragment identifier, the XSLT processor may signal the error; if it does not signal the error, it must recover by returning an empty node-set. Possible errors include:

    The data resulting from the retrieval action is parsed as an XML document regardless of the media type of the retrieval result; if the top-level media type is text, then it is parsed in the same way as if the media type were text/xml; otherwise, it is parsed in the same way as if the media type wereapplication/xml.

    Note:

    Since there is no top-level xml media type, data with a media type other than text/xml orapplication/xml may in fact be XML.

    The URI reference may be relative. The base URI (see 3.2 Base URI) of the node in the second argument node-set that is first in document order is used as the base URI for resolving the relative URI into an absolute URI. It is an error if the second argument node-set is empty and the URI reference is relative; the XSLT processor may signal the error; if it does not signal an error, it must recover by returning an empty node-set. If the second argument is omitted, then it defaults to the node in the stylesheet that contains the expression that includes the call to the documentfunction. Note that a zero-length URI reference is a reference to the document relative to which the URI reference is being resolved; thusdocument("") refers to the root node of the stylesheet; the tree representation of the stylesheet is exactly the same as if the XML document containing the stylesheet was the initial source document.

    Two documents are treated as the same document if they are identified by the same URI. The URI used for the comparison is the absolute URI into which any relative URI was resolved and does not include any fragment identifier. One root node is treated as the same node as another root node if the two nodes are from the same document. Thus, the following expression will always be true:

    generate-id(document("foo.xml"))=generate-id(document("foo.xml"))

    The document function gives rise to the possibility that a node-set may contain nodes from more than one document. With such a node-set, the relative document order of two nodes in the same document is the normal document order defined by XPath [XPath]. The relative document order of two nodes in different documents is determined by an implementation-dependent ordering of the documents containing the two nodes. There are no constraints on how the implementation orders documents other than that it must do so consistently: an implementation must always use the same order for the same set of documents.

    12.2 Keys

    Keys provide a way to work with documents that contain an implicit cross-reference structure. The ID, IDREFand IDREFS attribute types in XML provide a mechanism to allow XML documents to make their cross-reference explicit. XSLT supports this through the XPath id function. However, this mechanism has a number of limitations:

    Because of these limitations XML documents sometimes contain a cross-reference structure that is not explicitly declared by ID/IDREF/IDREFS attributes.

    A key is a triple containing:

    1. the node which has the key
    2. the name of the key (an expanded-name)
    3. the value of the key (a string)

    A stylesheet declares a set of keys for each document using thexsl:key element. When this set of keys contains a member with node x, name y and valuez, we say that node x has a key with namey and value z.

    Thus, a key is a kind of generalized ID, which is not subject to the same limitations as an XML ID:

    The xsl:key element is used to declare keys. Thename attribute specifies the name of the key. The value of the name attribute is a QName, which is expanded as described in 2.4 Qualified Names. The match attribute is a Pattern; an xsl:key element gives information about the keys of any node that matches the pattern specified in the match attribute. The use attribute is an expression specifying the values of the key; the expression is evaluated once for each node that matches the pattern. If the result is a node-set, then for each node in the node-set, the node that matches the pattern has a key of the specified name whose value is the string-value of the node in the node-set; otherwise, the result is converted to a string, and the node that matches the pattern has a key of the specified name with value equal to that string. Thus, a node x has a key with namey and value z if and only if there is anxsl:key element such that:

    Note also that there may be more than one xsl:keyelement that matches a given node; all of the matchingxsl:key elements are used, even if they do not have the same import precedence.

    It is an error for the value of either the useattribute or the match attribute to contain a VariableReference, or a call to thekey function.

    node-set key(string, object)

    The key function does for keys what the id function does for IDs. The first argument specifies the name of the key. The value of the argument must be aQName, which is expanded as described in 2.4 Qualified Names. When the second argument to thekey function is of type node-set, then the result is the union of the result of applying the keyfunction to the string-value of each of the nodes in the argument node-set. When the second argument to key is of any other type, the argument is converted to a string as if by a call to the string function; it returns a node-set containing the nodes in the same document as the context node that have a value for the named key equal to this string.

    For example, given a declaration

    <xsl:key name="idkey" match="div" use="@id"/>

    an expression key("idkey",@ref) will return the same node-set as id(@ref), assuming that the only ID attribute declared in the XML source document is:

    and that the ref attribute of the current node contains no whitespace.

    Suppose a document describing a function library uses aprototype element to define functions

    and a function element to refer to function names

    Then the stylesheet could generate hyperlinks between the references and definitions as follows:

    <xsl:key name="func" match="prototype" use="@name"/>

    <xsl:template match="function"> xsl:apply-templates/

    <xsl:template match="prototype">

    Function: ...

    The key always returns nodes that are in the same document as the current node; to retrieve a key from any other document, it is necessary first to change the current node. For example, suppose a document contains bibliographic references in the form <bibref>XSLT</bibref>, and there is a separate XML document bib.xml containing a bibliographic database with entries in the form:

    ...

    Then the stylesheet could use the following to transform thebibref elements:

    <xsl:key name="bib" match="entry" use="@name"/>

    <xsl:template match="bibref"> <xsl:variable name="name" select="."/> <xsl:for-each select="document('bib.xml')"> <xsl:apply-templates select="key('bib',$name)"/>

    12.3 Number Formatting

    string format-number(number, string, string?)

    The format-number function converts its first argument to a string using the format pattern string specified by the second argument and the decimal-format named by the third argument, or the default decimal-format, if there is no third argument. The format pattern string is in the syntax specified by the JDK 1.1 DecimalFormat class. The format pattern string is in a localized notation: the decimal-format determines what characters have a special meaning in the pattern (with the exception of the quote character, which is not localized). The format pattern must not contain the currency sign (#x00A4); support for this feature was added after the initial release of JDK 1.1. The decimal-format name must be a QName, which is expanded as described in 2.4 Qualified Names. It is an error if the stylesheet does not contain a declaration of the decimal-format with the specifiedexpanded-name.

    Note:

    Implementations are not required to use the JDK 1.1 implementation, nor are implementations required to be implemented in Java.

    Note:

    Stylesheets can use other facilities in XPath to control rounding.

    The xsl:decimal-format element declares a decimal-format, which controls the interpretation of a format pattern used by the format-number function. If there is a name attribute, then the element declares a named decimal-format; otherwise, it declares the default decimal-format. The value of the name attribute is a QName, which is expanded as described in 2.4 Qualified Names. It is an error to declare either the default decimal-format or a decimal-format with a given name more than once (even with different import precedence), unless it is declared every time with the same value for all attributes (taking into account any default values). If a stylesheet does not contain a declaration of the default decimal format, a declaration equivalent to an xsl:decimal-format element with no attributes is implied.

    The other attributes on xsl:decimal-format correspond to the methods on the JDK 1.1 DecimalFormatSymbols class. For eachget/set method pair there is an attribute defined for the xsl:decimal-format element.

    The following attributes both control the interpretation of characters in the format pattern and specify characters that may appear in the result of formatting the number:

    The following attributes control the interpretation of characters in the format pattern:

    The following attributes specify characters or strings that may appear in the result of formatting the number:

    12.4 Miscellaneous Additional Functions

    node-set current()

    The current function returns a node-set that has the current node as its only member. For an outermost expression (an expression not occurring within another expression), the current node is always the same as the context node. Thus,

    <xsl:value-of select="current()"/>

    means the same as

    <xsl:value-of select="."/>

    However, within square brackets the current node is usually different from the context node. For example,

    <xsl:apply-templates select="//glossary/item[@name=current()/@ref]"/>

    will process all item elements that have aglossary parent element and that have a nameattribute with value equal to the value of the current node'sref attribute. This is different from

    <xsl:apply-templates select="//glossary/item[@name=./@ref]"/>

    which means the same as

    <xsl:apply-templates select="//glossary/item[@name=@ref]"/>

    and so would process all item elements that have aglossary parent element and that have a nameattribute and a ref attribute with the same value.

    It is an error to use the current function in a pattern.

    string unparsed-entity-uri(string)

    The unparsed-entity-uri returns the URI of the unparsed entity with the specified name in the same document as the context node (see 3.3 Unparsed Entities). It returns the empty string if there is no such entity.

    string generate-id(node-set?)

    The generate-id function returns a string that uniquely identifies the node in the argument node-set that is first in document order. The unique identifier must consist of ASCII alphanumeric characters and must start with an alphabetic character. Thus, the string is syntactically an XML name. An implementation is free to generate an identifier in any convenient way provided that it always generates the same identifier for the same node and that different identifiers are always generated from different nodes. An implementation is under no obligation to generate the same identifiers each time a document is transformed. There is no guarantee that a generated unique identifier will be distinct from any unique IDs specified in the source document. If the argument node-set is empty, the empty string is returned. If the argument is omitted, it defaults to the context node.

    object system-property(string)

    The argument must evaluate to a string that is a QName. The QName is expanded into a name using the namespace declarations in scope for the expression. Thesystem-property function returns an object representing the value of the system property identified by the name. If there is no such system property, the empty string should be returned.

    Implementations must provide the following system properties, which are all in the XSLT namespace:

    xsl:version, a number giving the version of XSLT implemented by the processor; for XSLT processors implementing the version of XSLT specified by this document, this is the number 1.0
    xsl:vendor, a string identifying the vendor of the XSLT processor
    xsl:vendor-url, a string containing a URL identifying the vendor of the XSLT processor; typically this is the host page (home page) of the vendor's Web site.

    Vendors must not define additional system properties in the XSLT namespace.

    13 Messages

    The xsl:message instruction sends a message in a way that is dependent on the XSLT processor. The content of thexsl:message instruction is a template. Thexsl:message is instantiated by instantiating the content to create an XML fragment. This XML fragment is the content of the message.

    Note:

    An XSLT processor might implement xsl:message by popping up an alert box or by writing to a log file.

    If the terminate attribute has the valueyes, then the XSLT processor should terminate processing after sending the message. The default value is no.

    One convenient way to do localization is to put the localized information (message text, etc.) in an XML document, which becomes an additional input file to the stylesheet. For example, suppose messages for a language L are stored in an XML file resources/L.xml in the form:

    A problem was detected. An error was detected.

    Then a stylesheet could use the following approach to localize messages:

    <xsl:param name="lang" select="'en'"/> <xsl:variable name="messages" select="document(concat('resources/', $lang, '.xml'))/messages"/>

    <xsl:template name="localized-message"> <xsl:param name="name"/> xsl:message <xsl:value-of select="$messages/message[@name=$name]"/>

    <xsl:template name="problem"> <xsl:call-template name="localized-message"/> <xsl:with-param name="name">problem

    14 Extensions

    XSLT allows two kinds of extension, extension elements and extension functions.

    This version of XSLT does not provide a mechanism for defining implementations of extension elements. It does provide a mechanism for defining extension functions; however, XSLT does not require that XSLT processors support any particular languages for implementing extensions. Therefore, an XSLT stylesheet that must be portable between XSLT implementations cannot rely on particular extensions being available. XSLT provides mechanisms that allow an XSLT stylesheet to determine whether the XSLT processor by which it is being processed has implementations of particular extensions available, and to specify what should happen if those extensions are not available. If an XSLT stylesheet is careful to make use of these mechanisms, it is possible for it to take advantage of extensions and still work with any XSLT implementation.

    14.1 Using Extension Elements

    [Definition: The element extension mechanism allows namespaces to be designated asextension namespaces. When a namespace is designated as an extension namespace and an element with a name from that namespace occurs in a template, then the element is treated as an instruction rather than as a literal result element.] The namespace determines the semantics of the instruction.

    Note:

    Since an element that is a child of anxsl:stylesheet element is not occurring in a template, non-XSLT top-level elements are not extension elements as defined here, and nothing in this section applies to them.

    A namespace is designated as an extension namespace by using anextension-element-prefixes attribute on anxsl:stylesheet element or anxsl:extension-element-prefixes attribute on a literal result element or extension element. The value of both these attributes is a whitespace-separated list of namespace prefixes. The namespace bound to each of the prefixes is designated as an extension namespace. It is an error if there is no namespace bound to the prefix on the element bearing the extension-element-prefixes orxsl:extension-element-prefixes attribute. The default namespace (as declared by xmlns) may be designated as an extension namespace by including #default in the list of namespace prefixes. The designation of a namespace as an extension namespace is effective within the subtree of the stylesheet rooted at the element bearing the extension-element-prefixes orxsl:extension-element-prefixes attribute; a subtree rooted at an xsl:stylesheet element does not include any stylesheets imported or included by children of that xsl:stylesheet element.

    If the XSLT processor does not have an implementation of a particular extension element available, then theelement-available function must return false for the name of the element. When such an extension element is instantiated, then the XSLT processor must perform fallback for the element as specified in 15 Fallback. An XSLT processor must not signal an error merely because a template contains an extension element for which no implementation is available.

    If the XSLT processor has an implementation of a particular extension element available, then theelement-available function must return true for the name of the element.

    14.2 Using Extension Functions

    If a FunctionName in aFunctionCall expression is not an NCName (i.e. if it contains a colon), then it is treated as a call to an extension function. The FunctionNameis expanded to a name using the namespace declarations from the evaluation context.

    If the XSLT processor does not have an implementation of an extension function of a particular name available, then thefunction-available function must return false for that name. If such an extension function occurs in an expression and the extension function is actually called, the XSLT processor must signal an error. An XSLT processor must not signal an error merely because an expression contains an extension function for which no implementation is available.

    If the XSLT processor has an implementation of an extension function of a particular name available, then thefunction-available function must return true for that name. If such an extension is called, then the XSLT processor must call the implementation passing it the function call arguments; the result returned by the implementation is returned as the result of the function call.

    | Editorial note | | | -------------------------------------------------------- | | | Say something about automatic argument conversions here. | |

    Typically, implementations of extension functions represent strings as a sequence of code values (usually 16-bit integers or bytes). It is possible for an extension function to return a sequence of code values that cannot be mapped to a sequence of legal XML characters. The behavior of an XSLT processor in this case is implementation-dependent.

    Note:

    Requiring the XSLT processor to check the contents of every string returned by an extension function could be a significant performance burden.

    14.3 External Objects

    [Definition: Support for extension functions introduces an additional data-type into the expression language. This additional data type is called anexternal object. A variable may be bound to an object of type external object instead of one of the four XPath data-types (string, number, boolean, node-set). An external object represents an object that is not convertible to one of the four XPath data types which is created by an external programming language and returned by an extension function. Expressions can only return values of type external object by referencing variables of type external object or calling extension functions that return an external object.]

    An external object may only be passed as an argument to another extension function. It may not be copied to the result tree or converted (implicitly or explicitly) to any of the XPath data types. So, for example, if the myvar variable is bound an external object, then the following are not allowed:

    If an external object is passed to an extension function with anexpanded-namewhose namespace URI is different from the namespace URI of theexpanded-name of the extension function that returned that external object, the behavior is implementation-dependent.

    An extension function can be used to convert the argument to a string or return an XML fragment to be copied to the result tree if desired. For example, assuming the myext:print()extension function accepts an argument of a compatible data type and returns a string, the following is allowed:

    select="myext:print($myvar)"/>

    Issue (issue-null-external-object):

    Should the spec have the concept that an external object may be null, and provide a way for testing this, for example, by conversion toboolean?

    | Editorial note | | | ------------------------------------------------------------------------------------------------------ | | | Define the idea that an external object "wraps" an object created by an external programming language. | |

    14.4 Defining Extension Functions

    The top-level xsl:script element provides an implementation of extension functions in a particular namespace. The function implementations may be provided as a separate resource. Some language bindings also allow the function implementations to be provided directly in the content of the xsl:script.

    | Editorial note | | | ------------------------------------------------------------------------------------------------------------------ | | | Make sure that it is clear that it is allowed to call extension functions without using xsl:script to define them. | |

    The implements-prefix attribute specifies a namespace prefix. This prefix is expanded into a URI using the namespace declarations in scope on the xsl:script element. This URI is the namespace URI of the expanded-names of the functions of which an implementation is being provided. The implementation of the extension functions identifies the local names of the functions that is is implementing.

    The language attribute identifies the language used by the implementation being provided. It also identifies the way arguments are passed by the XSLT processor to the implementation of the extension function, the way results are returned from the implementation of the extension function, and the way the XSLT expression context is made available to the extension function (collectively known as the language binding). The value must be aQName. If the QName does not have a prefix, then it identifies a language binding specified in this document and must beecmascript, javascript or java. If the QName has a prefix, then the QName is expanded into anexpanded-name as described in 2.4 Qualified Names; the expanded-name identifies the language binding; the language binding in this case is not specified by this document. XSLT processors are not required to support any particular language binding. xsl:script elements withlanguage values not supported by the processor are ignored.

    When the function implementations are provided as a separate resource, the content of the xsl:script element must be empty and the src attribute is required. Thesrc attribute is a URI reference that identifies the resource containing the implementation of the extension functions. For example,

    When a function implementation is provided locally in the content of the xsl:script element, the srcattribute must not be specified. For example,

    <xsl:script implements-prefix="pref" language="lang"> // Function Declarations in Supported Language Syntax function f(x) { ... } function g(y,z) { ... }

    The archive attribute is a whitespace-separated list of URI references, each pointing to an archive to be loaded by the processor before executing the extension function. Thearchive attribute may be used on anxsl:script element regardless of whether thatxsl:script element has a src attribute.

    The function-available function can be used to detect the availability of an extension function provided by anxsl:script element. The precise details of what it means for an extension function to be available is specific to the language-binding.

    A stylesheet can contain any number of xsl:scriptelements. The order of these elements is not significant in the stylesheet, except for fallback behavior. In particular, calls to extension function can occur before xsl:script elements that provide implementations of those extension functions.

    If there are multiple xsl:script elements with the same namespace URI but different implementation languages, then each implementation language corresponds to an alternative implementation of the same extension functions. If an XSLT processor supports two or more of the languages in which an extension function implementation is provided, then it is free to choose any of its supported languages, provided that it always chooses the same implementation for the same extension function namespace URI. A processor is under no obligation to choose the same implementation each time a document is transformed.

    If there are multiple xsl:script elements with the same namespace URI and the same language but different import precedences, a processor must reconcile the separate elements using the import precedence. The manner in which the elements are reconciled is determined by the language binding. For example, some language bindings may simply use the xsl:script element with the highest import precedence; others may merge thexsl:script elements in some way. It is an error for twoxsl:script elements with the same import precedence to implement the same namespace URI for the same language. An XSLT processor may signal the error; if it does not signal the error, it must recover by treating the two xsl:script elements as if the xsl:script element that occurs later in the stylesheet had a higher import precedence than the otherxsl:script element.

    15 Fallback

    Normally, instantiating an xsl:fallback element does nothing. However, when an XSLT processor performs fallback for an instruction element, if the instruction element has one or morexsl:fallback children, then the content of each of thexsl:fallback children must be instantiated in sequence; otherwise, an error must be signaled. The content of anxsl:fallback element is a template.

    The following functions can be used with thexsl:choose and xsl:if instructions to explicitly control how a stylesheet should behave if particular elements or functions are not available.

    boolean element-available(string)

    The argument must evaluate to a string that is a QName. The QName is expanded into an expanded-name using the namespace declarations in scope for the expression. If there is a default namespace in scope, then it is used to expand an unprefixed QName. Theelement-available function returns true if and only if the expanded-name is the name of an instruction. If the expanded-name has a namespace URI equal to the XSLT namespace URI, then it refers to an element defined by XSLT. Otherwise, it refers to an extension element. If the expanded-name has a null namespace URI, the element-available function will return false.

    boolean function-available(string)

    The argument must evaluate to a string that is a QName. The QName is expanded into an expanded-name using the namespace declarations in scope for the expression. Thefunction-available function returns true if and only if the expanded-name is the name of a function in the function library. If the expanded-name has a non-null namespace URI, then it refers to an extension function; otherwise, it refers to a function defined by XPath or XSLT.

    16 Output

    An XSLT processor may output the result tree as a sequence of bytes, although it is not required to be able to do so (see 17 Conformance). The xsl:output element allows stylesheet authors to specify how they wish the result tree to be output. If an XSLT processor outputs the result tree, it should do so as specified by the xsl:output element; however, it is not required to do so. Before the result tree is output, namespace fixupis performed (see 3.5 Namespace Fixup).

    The xsl:output element is only allowed as a top-level element. All attributes on xsl:output are interpreted as attribute value templates. Expressions recognized in the attributes ofxsl:output are evaluated with the same context as used to process the root node of the source document: the current node is the root node of the source document and the current node list is a list containing just the root node of the source document.

    The method attribute on xsl:outputidentifies the overall method that should be used for outputting the result tree. The value must be a QName. If the QName does not have a prefix, then it identifies a method specified in this document and must be one ofxml, html or text. If the QName has a prefix, then the QName is expanded into an expanded-name as described in 2.4 Qualified Names; the expanded-name identifies the output method; the behavior in this case is not specified by this document.

    The default for the method attribute is chosen as follows. If

    then the default output method is html; otherwise, the default output method is xml. The default output method should be used if there are no xsl:output elements or if none of the xsl:output elements specifies a value for themethod attribute.

    The other attributes on xsl:output provide parameters for the output method. The following attributes are allowed:

    The detailed semantics of each attribute will be described separately for each output method for which it is applicable. If the semantics of an attribute are not described for an output method, then it is not applicable to that output method.

    A stylesheet may contain multiple xsl:output elements and may include or import stylesheets that also containxsl:output elements. All the xsl:outputelements occurring in a stylesheet are merged into a single effectivexsl:output element. For thecdata-section-elements attribute, the effective value is the union of the specified values. For other attributes, the effective value is the specified value with the highest import precedence. It is an error if there is more than one such value for an attribute. An XSLT processor may signal the error; if it does not signal the error, if should recover by using the value that occurs last in the stylesheet. The values of attributes are defaulted after thexsl:output elements have been merged; different output methods may have different default values for an attribute.

    16.1 XML Output Method

    The xml output method outputs the result tree as a well-formed XML external general parsed entity. If the root node of the result tree has a single element node child and no text node children, then the entity should also be a well-formed XML document entity. When the entity is referenced within a trivial XML document wrapper like this

    ]> &e;

    where entity-URI is a URI for the entity, then the wrapper document as a whole should be a well-formed XML document conforming to the XML Namespaces Recommendation [XML Names]. In addition, the output should be such that if a new tree was constructed by parsing the wrapper as an XML document as specified in 3 Data Model, and then removing the document element, making its children instead be children of the root node, then the new tree would be the same as the result tree, with the following possible exceptions:

    If the XSLT processor generated a document type declaration because of the doctype-system attribute or generated a standalone document declaration because of the standalone attribute, then the output must be a well-formed XML document, and the above requirements apply to the tree constructed by parsing that document directly, without any wrapper, using an XML processor that does not process the DTD.

    Note:

    Processing the DTD might cause additional attributes to appear in the tree because of attribute defaulting.

    The version attribute specifies the version of XML to be used for outputting the result tree. If the XSLT processor does not support this version of XML, it should use a version of XML that it does support. The version output in the XML declaration (if an XML declaration is output) should correspond to the version of XML that the processor used for outputting the result tree. The value of theversion attribute should match the VersionNum production of the XML Recommendation [XML]. The default value is1.0.

    The encoding attribute specifies the preferred encoding to use for outputting the result tree. XSLT processors are required to respect values of UTF-8 andUTF-16. For other values, if the XSLT processor does not support the specified encoding it may signal an error; if it does not signal an error it should use UTF-8 orUTF-16 instead. The XSLT processor must not use an encoding whose name does not match the EncName production of the XML Recommendation [XML]. If no encodingattribute is specified, then the XSLT processor should use eitherUTF-8 or UTF-16. It is possible that the result tree will contain a character that cannot be represented in the encoding that the XSLT processor is using for output. In this case, if the character occurs in a context where XML recognizes character references (i.e. in the value of an attribute node or text node), then the character should be output as a character reference; otherwise (for example if the character occurs in the name of an element) the XSLT processor should signal an error.

    If the indent attribute has the valueyes, then the xml output method may output whitespace in addition to the whitespace in the result tree (possibly based on whitespace stripped from either the source document or the stylesheet) in order to indent the result nicely; if theindent attribute has the value no, it should not output any additional whitespace. The default value isno. The xml output method should use an algorithm to output additional whitespace that ensures that the result if whitespace were to be stripped from the output using the process described in 3.4 Whitespace Stripping with the set of whitespace-preserving elements consisting of justxsl:text would be the same when additional whitespace is output as when additional whitespace is not output.

    Note:

    It is usually not safe to use indent="yes" with document types that include element types with mixed content.

    The cdata-section-elements attribute contains a whitespace-separated list of QNames. Each QName is expanded into an expanded-name using the namespace declarations in effect on thexsl:output element in which the QName occurs; if there is a default namespace, it is used for QNames that do not have a prefix. The expansion is performed before the merging of multiple xsl:output elements into a single effective xsl:output element. If the expanded-name of the parent of a text node is a member of the list, then the text node should be output as a CDATA section. For example,

    <xsl:output cdata-section-elements="example"/>

    would cause a literal result element written in the stylesheet as

    <foo>

    or as

    to be output as

    If the text node contains the sequence of characters]]>, then the currently open CDATA section should be closed following the ]] and a new CDATA section opened before the >. For example, a literal result element written in the stylesheet as

    ]]>

    would be output as

    If the text node contains a character that is not representable in the character encoding being used to output the result tree, then the currently open CDATA section should be closed before the character, the character should be output using a character reference or entity reference, and a new CDATA section should be opened for any further characters in the text node.

    CDATA sections should not be used except for text nodes that thecdata-section-elements attribute explicitly specifies should be output using CDATA sections.

    The xml output method should output an XML declaration unless the omit-xml-declaration attribute has the valueyes. The XML declaration should include both version information and an encoding declaration. If thestandalone attribute is specified, it should include a standalone document declaration with the same value as the value as the value of the standalone attribute. Otherwise, it should not include a standalone document declaration; this ensures that it is both an XML declaration (allowed at the beginning of a document entity) and a text declaration (allowed at the beginning of an external general parsed entity).

    If the doctype-system attribute is specified, thexml output method should output a document type declaration immediately before the first element. The name following<!DOCTYPE should be the name of the first element. Ifdoctype-public attribute is also specified, then thexml output method should output PUBLICfollowed by the public identifier and then the system identifier; otherwise, it should output SYSTEM followed by the system identifier. The internal subset should be empty. Thedoctype-public attribute should be ignored unless thedoctype-system attribute is specified.

    The media-type attribute is applicable for thexml output method. The default value for themedia-type attribute is text/xml.

    16.2 HTML Output Method

    The html output method outputs the result tree as HTML; for example,

    <xsl:stylesheet version="1.1" xmlns:xsl="" title="undefined" rel="noopener noreferrer">http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="html"/>

    <xsl:template match="/"> xsl:apply-templates/

    ...

    The version attribute indicates the version of the HTML. The default value is 4.0, which specifies that the result should be output as HTML conforming to the HTML 4.0 Recommendation [HTML].

    The html output method should not output an element differently from the xml output method unless the expanded-name of the element has a null namespace URI; an element whose expanded-name has a non-null namespace URI should be output as XML. If the expanded-name of the element has a null namespace URI, but the local part of the expanded-name is not recognized as the name of an HTML element, the element should output in the same way as a non-empty, inline element such as span.

    The html output method should not output an end-tag for empty elements. For HTML 4.0, the empty elements arearea, base, basefont,br, col, frame,hr, img, input,isindex, link, meta andparam. For example, an element written as<br/> or <br></br> in the stylesheet should be output as <br>.

    The html output method should recognize the names of HTML elements regardless of case. For example, elements namedbr, BR or Br should all be recognized as the HTML br element and output without an end-tag.

    The html output method should not perform escaping for the content of the script and styleelements. For example, a literal result element written in the stylesheet as

    or

    should be output as

    The html output method should not escape< characters occurring in attribute values.

    If the indent attribute has the valueyes, then the html output method may add or remove whitespace as it outputs the result tree, so long as it does not change how an HTML user agent would render the output. The default value is yes.

    The html output method should escape non-ASCII characters in URI attribute values using the method recommended inSection B.2.1 of the HTML 4.0 Recommendation.

    The html output method may output a character using a character entity reference, if one is defined for it in the version of HTML that the output method is using.

    The html output method should terminate processing instructions with > rather than?>.

    The html output method should output boolean attributes (that is attributes with only a single allowed value that is equal to the name of the attribute) in minimized form. For example, a start-tag written in the stylesheet as