XSLT defines 37 elements, which break down into 3 overlapping categories:
Two root elements:
xsl:stylesheet xsl:transform
12 top-level elements. These elements may appear as immediate children of the root and are the following:
xsl:attribute-set xsl:decimal-format xsl:import xsl:include xsl:key xsl:namespace-alias xsl:output xsl:param xsl:preserve-space xsl:strip-space xsl:template xsl:variable
23 instruction elements. These elements appear in the content of elements that contain templates. Here we don't mean the xsl:template element. We mean the content of that and several other elements, such as xsl:for-each and xsl:message, which are composed of literal result elements, character data, and XSLT instructions that are processed to produce part of the result tree. These elements are as follows:
xsl:apply-imports xsl:apply-templates xsl:attribute xsl:call-template xsl:choose xsl:comment xsl:copy xsl:copy-of xsl:element xsl:fallback xsl:for-each xsl:if xsl:message xsl:number xsl:otherwise xsl:param xsl:processing-instruction xsl:sort xsl:text xsl:value-of xsl:variable xsl:with-param xsl:when
Most XSLT processors also provide various nonstandard extension elements and allow you to write your own extension elements in languages such as Java and JavaScript.
Elements in this section are arranged alphabetically from xsl:apply-imports to xsl:with-param. Each element begins with a synopsis in the following form:
<xsl:elementName attribute1 = "allowed attribute values" attribute2 = "allowed attribute values" > <!-- Content model --> </xsl:elementName>
Most attribute values are one of the following types:
Some attributes that contain strings--whether those strings are literals, expressions, names, or something else--can be given as attribute value templates. This is an XPath expression enclosed in curly braces, which is evaluated to provide the final value of the attribute. When this is the case, it is indicated in the description of each attribute.
Potentially nonempty elements have content models given in a comment in the form they might appear in an ELEMENT declaration. If an element can contain a template, we use the word "template" to stand in for all the possible elements that may appear.
xsl:apply-imports |
<xsl:apply-imports /> |
xsl:apply-templates |
<xsl:apply-templates select = "node-set-expression" mode = "QualifiedName"> <! -- (xsl:sort | xsl:with-param)* -- > </xsl:apply-templates> |
The xsl:apply-templates element may have xsl:sort child elements to specify the order in which the selected nodes will be processed. Without any xsl:sort children, the default is to process nodes in document order.
The xsl:apply-templates element may have xsl:with-param child elements to pass parameter values to the matched templates.
xsl:attribute |
<xsl:attribute name = "QualifiedName" namespace = "URI"> <! -- template for the attribute value -- > </xsl:attribute> |
The contents of this element are a template whose instantiation only produces text nodes. The value of the attribute added to the result tree is determined by instantiating the template.
xsl:attribute-set |
<xsl:attribute-set name = "QualifiedName" use-attribute-sets = "QualifiedNames"> <! -- xsl:attribute* -- > </xsl:attribute-set> |
This element contains zero or more xsl:attribute elements. Each such element adds one attribute to the set.
xsl:call-template |
<xsl:call-template name = "QualifiedName"> <! -- xsl:with-param* -- > </xsl:call-template> |
This element contains zero or more xsl:with-param elements that pass parameters to the named template.
xsl:choose |
<xsl:choose> <! -- (xsl:when+, xsl:otherwise?) -- > </xsl:choose> |
This element contains one or more xsl:when elements, each of which has a test condition. The contents are output for the first xsl:when child whose test condition is true.
The xsl:choose element may have an optional xsl:otherwise element whose contents are output only if none of the test conditions in any of the xsl:when elements is true.
If no xsl:otherwise element exists and none of the test conditions in any of the xsl:when child elements is true, then this element will not produce output.
xsl:comment |
<xsl:comment> <! -- template -- > </xsl:comment> |
The content of xsl:comment is a template that will be instantiated to form the text of the comment inserted into the result tree. The result of instantiating this template must only be text nodes that do not contain the double hyphen (--) (since comments cannot contain the double hyphen).
xsl:copy |
<xsl:copy use-attribute-sets = "QualifiedName1 QualifiedName2..."> <! -- template -- > </xsl:copy> |
If the current node is an element node, attributes can be added via xsl:attribute children. If the current node is the root node or an element node (a node that can have children), then xsl:copy may contain a template that specifies the content of the element inserted into the result tree. All xsl:attribute elements must precede the output template.
xsl:copy-of |
<xsl:copy-of select = "expression" /> |
xsl:decimal-format |
xsl:element |
<xsl:element name = "QualifiedName" namespace = "URI" use-attribute-sets = "QualifiedName1 QualifiedName2..."> <! -- template -- > </xsl:element> |
The contents of this element are a template. Once instantiated, this template forms the content of the element inserted into the result tree.
xsl:fallback |
<xsl:fallback> <! -- template -- > </xsl:fallback> |
The contents of this element are a template that is instantiated and output if and only if the XSLT processor does not recognize the xsl:fallback element's parent element.
xsl:for-each |
<xsl:for-each select = "node-set-expression"> <! -- (xsl:sort*, template) -- > </xsl:for-each> |
Normally, the selected nodes are processed in the order in which they appear in the document. However, nodes can be sorted using xsl:sort child elements. The first such element is the primary sort key; the second is the secondary sort key; and so on.
The xsl:for-each element must also contain a template that is instantiated once for each member of the node-set returned by the node-set expression in the select attribute.
xsl:if |
<xsl:if test = "boolean-expression"> <! -- template -- > </xsl:if> |
A template is instantiated if the test attribute evaluates to true.
xsl:import |
<xsl:import href = "URI" /> |
All xsl:import elements must be immediate children of the root xsl:stylesheet element. Furthermore, they must appear before all other top-level elements.
An imported stylesheet may itself import another stylesheet. A stylesheet may not import a stylesheet that was already imported, directly or indirectly. That is, it's an error if A imports B, which imports A, thus creating a circular reference.
xsl:include |
<xsl:include href = "URI" /> |
An included stylesheet may include another stylesheet. A stylesheet may not include a stylesheet that was already included, directly or indirectly; it is an error if A includes B, which includes A.
xsl:key |
<xsl:key name = "QualifiedName" match = "pattern" use = "expression" /> |
xsl:message |
<xsl:message terminate = "yes" | "no"> <! -- template -- > </xsl:message> |
An xsl:message element's content is a template instantiated to create an XML fragment. The result is then delivered to the XSLT processor as the message.
WARNING: The XSLT specification does not define XML fragment, and various XSLT processors interpret it differently. It may be a result tree fragment or an XML fragment, as defined by the now moribund XML Fragment Interchange working draft. It may be something else. Clarification from the W3C is necessary.
xsl:namespace-alias |
<xsl:namespace-alias stylesheet-prefix = "prefix" result-prefix = "prefix" /> |
xsl:number |
You can also change the starting point; for instance, setting the format token to 5 would create the sequence 5, 6, 7, 8, 9, . . .
xsl:otherwise |
<xsl:otherwise> <! -- template -- > </xsl:otherwise> |
The contents are a template that is instantiated if and only if none of the xsl:choose element's xsl:when sibling elements is true.
xsl:output |
xsl:param |
<xsl:param name = "QualifiedName" select = "expression"> <! -- template -- > </xsl:param> |
An xsl:param element's content is a template that is instantiated to produce a result-tree fragment. This result-tree fragment then becomes the parameter's value. A nonempty xsl:param element must not have a select attribute.
xsl:preserve-space |
<xsl:preserve-space elements="QualifiedName_1 QualifiedName_2..." /> |
xsl:processing-instruction |
<xsl:processing-instruction name = "target"> <! -- template -- > </xsl:processing-instruction> |
The xsl:processing-instruction element's contents are a template that is instantiated to produce the processing-instruction data. This template may include XSLT instructions, provided that the result of instantiating this template is text that does not contain the two-character string ?>.
xsl:sort |
<xsl:sort select = "string-expression" data-type = "text" | "number" | "PrefixedName" lang = "langcode" order = "ascending" | "descending" case-order = "upper-first" | "lower-first" /> |
xsl:strip-space |
<xsl:strip-space elements="QualifiedName_1 QualifiedName_2..." /> |
This element does not trim leading or trailing whitespace or otherwise normalize whitespace in elements that contain even a single nonwhitespace character.
xsl:stylesheet |
Any xsl:import elements, followed by any other top-level elements in any order.
xsl:template |
<xsl:template match = "pattern" priority = "number" name = "QualifiedName" mode = "QualifiedName"> <! -- (xsl:param*, template) -- > </xsl:template> |
Template rules with match patterns composed of just an element or attribute name (e.g., person or @profession) have priority 0.
Template rules with match patterns composed of just a processing-instruction('target') node test have priority 0.
Template rules with match patterns in the form prefix:* have priority -0.25.
Template rules with match patterns that just have a wildcard node test (*, @*, comment( ), node( ), text(), and processing-instruction( )) have priority -0.5. (This means that built-in template rules have priority -0.5. However, they are also imported before all other template rules, and thus never override any explicit template rule, regardless of priority.)
Template rules with any other patterns (person[name='Feynman'], people/person/@profession, person/text( ), etc.) have priority 0.5.
It is an error if two or more template rules match a node and have the same priority. However, in this case most XSLT processors choose the last template rule occurring in the stylesheet rather than signaling the error.
The template that should be instantiated when this element is matched or called by name.
xsl:text |
<xsl:text disable-output-escaping = "yes" | "no"> <! -- #PCDATA -- > </xsl:text> |
xsl:transform |
xsl:value-of |
<xsl:value-of select = "expression" disable-output-escaping = "yes" | "no" /> |
You can also take values of things that aren't nodes. The value of a node-set is the value of the first node in the set. The value of a string expression is the string. The value of a number expression is the string form of the number. The value of a Boolean expression is the string true if the Boolean is true or the string false if the Boolean is false.
xsl:variable |
<xsl:variable name = "QualifiedName" select = "expression"> <! -- template -- > </xsl:variable> |
TIP: The word variable is a little misleading. Once the value of an xsl:variable is set, it cannot be changed. An xsl:variable is more like a named constant than a traditional variable.
A template that is instantiated to produce the variable's value as a result-tree fragment. If an xsl:variable is not an empty element, it must not have a select attribute. If xsl:variable is empty and does not have a select attribute, then its value is the empty string.
xsl:when |
<xsl:when test = "boolean-expression"> <! -- template -- > </xsl:when> |
The template to be instantiated and inserted into the result tree if the test attribute is true.
xsl:with-param |
<xsl:with-param name = "QualifiedName" select = "expression"> <! -- template -- > </xsl:with-param> |
A template that is instantiated and passed as the parameter's value. If xsl:with-param is not an empty element, it must not have a select attribute. If xsl:with-param is empty and does not have a select attribute, then its value is the empty string.
Copyright © 2002 O'Reilly & Associates. All rights reserved.