XML technologies / XSLT / XSLT Cookbook / Exploiting the potential of XSLT 2.0 / Understanding new capabilities of existing XSLT 1.0 features
(Excerpt from “XSLT Kochbuch” by Sal Mangano)
Problem
In XSLT 2.0, many small improvements are difficult to master quickly.
Solution
Many 2.0 capabilities extend existing 1.0 instructions and functions. They are less obvious than entirely new instructions or functions. This section surveys these extensions.
xsl:apply-templates
Attribute
modeaccepts value#currentto continue processing in the current mode.Exploit sequence support using a comma-separated list, such as
<xsl:apply-templates select="title, heading, para"/>, to processtitleelements first, thenheadingelements, and finallyparaelements. In 1.0, this required three separateapply-templateinstructions.
Note:
In both 1.0 and 2.0, you can write: <xsl:apply-templates select="title | heading | para"/>, but this is unordered: children are processed in document order, not the order of the selection.
<xsl:apply-templates select="title | heading | para"/>xsl:attribute
Many XSLT 1.0 developers were frustrated at being unable to write
<xsl:attribute name="foo" select="10"/>because attributeselectwas unsupported. It is now supported and should be used for simple attributes, though sequence-constructor syntax remains available.Attributes
typeandvalidationwere added for schema-aware processors.typespecifies built-in or user-defined types defined by W3C Schema.validationspecifies how to validate the attribute.
xsl:call-template
The call can return any sequence, such as integers, rather than a node set.
Passing a parameter through
xsl:with-paramthat the called template does not define causes an error.
xsl:comment
Like
xsl:attribute, this now supports attributeselectin addition to sequence-constructor syntax.
xsl:copy and xsl:copy-of
New attribute
copy-namespaces="yes | no"controls copying of an element’s namespace nodes. Its default isyes, consistent with 1.0 behavior.Attribute
typewas added to specify built-in or user-defined W3C Schema types.Attribute
validationcontrols result validation or preservation of existing type annotations.
xsl:element
Attributes
typeandvalidationwere added for schema-aware processors.typespecifies built-in or user-defined W3C Schema types.validationcontrols element validation.
xsl:for-each
Can now process arbitrary sequences as well as node sequences:
<xsl:for-each select="(1, 2, 3, 4, 5)">
<xsl:value-of select="."/><xsl:text>)
</xsl:text>
</xsl:for-each>xsl:key
Attributes
matchandusecan now reference global variables provided there is no circular dependency between variable and key:
<xsl:variable name="state" select=" 'active' "/>
<xsl:key name="state-key" match="employee[@state=$state]" use="@type"/>Attribute
usecan be replaced by a sequence constructor’s value:
<!--Den Wert eines Schlüssels mit Hilfe ausgeklügelter Verarbeitung definieren -->
<xsl:key name="sick-key" select="employee">
<xsl:apply-templates select="record[@type='sick-day']" mode="sick-key"/>
</xsl:key>A collation can define when two key values compare equal. Available collations depend on the implementation.
xsl:message
Attribute
terminatecan now be an attribute value template, simplifying global changes to termination behavior.A
selectattribute is now supported alongside the sequence constructor:
<xsl:param name="terminate" select=" 'no' "/>
<xsl:template match="employee">
<xsl:if test="not(@type)">
<xsl:message terminate="{$terminate}" select=" 'Missing type attribute for employee' "/>
<xsl:if>
</xsl:template>
xsl:number
A new attribute
selectpermits numbering nodes other than the context node.Formatting now supports words such as “one”, “two”, “three” according to the selected language:
<!-- Dies gibt 'Ten' aus -->
<xsl:number value="10" format="Ww"/>
<!-- Dies gibt 'ten' aus -->
<xsl:number value="10" format="w"/>
<!-- Dies gibt 'TEN' aus -->
<xsl:number value="10" format="W"/>xsl:output
This facility can be named for use with new instruction
xsl:result-document. See recipe Processing string literals containing quotation marks.New method XHTML is supported.
New attribute
escape-uri-attributescontrols escaping of URI attributes in HTML and XHTML output.New attribute
include-content-typecontrols adding a<meta>element to identify output content type and encoding.New attribute
normalize-unicodecontrols normalization of Unicode characters. For details, see Character Model for the World Wide Web 1.0.New attribute
undeclare-namespacescontrols whether namespaces in XML 1.1 should be undeclared when out of scope. A namespace is undeclared usingxmlns:pre="", where pre is any prefix.New attribute
use-character-mapsspecifies a list of character-map names (see recipe Solving difficult serialization problems with character maps).
xsl:param
An
asattribute specifies the parameter type.A
requiredattribute specifies whether the parameter is required or optional.A
tunnelattribute controls tunneling for this parameter (see recipe Emulating object-oriented reuse and design patterns).
xsl:processing-instruction
A
selectattribute is now supported.
xsl:strip-space
Attribute
elementsaccepts name tests of form*:Nameto strip whitespace from all Name elements regardless of namespace. For whitespace handling, see recipe Handling whitespace.
xsl:stylesheet
A new
default-validationattribute sets default validation for new element and attribute nodes when the creating instruction lacks avalidationattribute.A new
xpath-default-namespaceattribute sets the namespace for unprefixed element names in XPath expressions.
xsl:template
Multiple modes are supported through
#allor a list in attributemode.An
asattribute specifies the result type.The
matchattribute enables type-based filtering.The
matchattribute can reference global variables or parameters.
xsl:value-of
A new
separatorattribute separates sequences (see recipe Exporting XML to delimited data).Sequence-constructor syntax is now supported alongside attribute
select.
xsl:variable
An
asattribute specifies the variable type.
xsl:with-param
An
asattribute specifies the parameter type.A
tunnelattribute controls tunneling for the parameter (see recipe Emulating object-oriented reuse and design patterns).Now usable with
xsl:apply-importsand new instructionxsl:next-match.
current( )
The function can return general items, such as a string, as well as nodes.
Can now occur in a filter pattern to reference the matched item.
<!-- Filtert Knoten mit Abkömmlingen, die Attribute besitzen, deren Wert dem lokalen Namen des erfassten Elements entspricht -->
<xsl:template match="*[descendant::*/@* = local-name(current( ))]">document( )
The first argument may now be any sequence of URIs.
Note:
New XPath function doc( ) is a simpler alternative to XSLT function document( ).
function-available( )
Now accepts a second argument giving the tested function’s arity (argument count).
key( )
An optional third argument specifies the document to search.
Note:
This avoids xsl:for-each instructions used solely to switch documents so key( ) can operate relative to that document:
<!-- Code wie dieser ist nicht mehr notwendig -->
<xsl:for-each select="doc('anderes.xml')">
<xsl:if test="key('irgendein-schlüssel', $val)">
<!-- ...-->
</xsl:if>
</xsl:for-each>
<!-- Schreiben Sie stattdessen -->
<xsl:if test="key('irgendein-schlüssel', $val, doc('anderes.xml'))">
<!-- ...-->
</xsl:if>system-property( )
Property
xsl:productreturns the processor name (for example,Saxon).Property
xsl:product-versionreturns the processor version (for example,8.1).Property
xsl:is-schema-awarereturnsyesornoto indicate schema awareness.Property
xsl:supports-serializationreturnsyesornoto indicate serialization support.Property
xsl:supports-backwards-compatibilityreturnsyesornoto indicate support for backwards-compatible mode (for example,version="1.0").
Discussion
Type support and consistency largely drive these improvements. Consistency mainly means adding a select attribute where only a sequence constructor was supported, or vice versa.
Note:
XSLT’s designers repeatedly introduce two names for constructs that, in my opinion, need only one. Consider attributes as and type. They are never used together, so why not simply use type? Similarly, one could eliminate xsl:with-param in favor of xsl:param when 1.0 is specified.
Copyright © 2006 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from “XSLT Kochbuch” is subject to the same conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O'Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de