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 mode accepts value #current to continue processing in the current mode.

  • Exploit sequence support using a comma-separated list, such as <xsl:apply-templates select="title, heading, para"/>, to process title elements first, then heading elements, and finally para elements. In 1.0, this required three separate apply-template instructions.

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 attribute select was unsupported. It is now supported and should be used for simple attributes, though sequence-constructor syntax remains available.

  • Attributes type and validation were added for schema-aware processors. type specifies built-in or user-defined types defined by W3C Schema. validation specifies 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-param that the called template does not define causes an error.

xsl:comment

  • Like xsl:attribute, this now supports attribute select in 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 is yes, consistent with 1.0 behavior.

  • Attribute type was added to specify built-in or user-defined W3C Schema types.

  • Attribute validation controls result validation or preservation of existing type annotations.

xsl:element

  • Attributes type and validation were added for schema-aware processors. type specifies built-in or user-defined W3C Schema types. validation controls 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>)&#xa;</xsl:text>
</xsl:for-each>

xsl:key

  • Attributes match and use can 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 use can 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 terminate can now be an attribute value template, simplifying global changes to termination behavior.

  • A select attribute 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 select permits 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

xsl:param

xsl:processing-instruction

  • A select attribute is now supported.

xsl:strip-space

  • Attribute elements accepts name tests of form *:Name to strip whitespace from all Name elements regardless of namespace. For whitespace handling, see recipe Handling whitespace.

xsl:stylesheet

  • A new default-validation attribute sets default validation for new element and attribute nodes when the creating instruction lacks a validation attribute.

  • A new xpath-default-namespace attribute sets the namespace for unprefixed element names in XPath expressions.

xsl:template

  • Multiple modes are supported through #all or a list in attribute mode.

  • An as attribute specifies the result type.

  • The match attribute enables type-based filtering.

  • The match attribute can reference global variables or parameters.

xsl:value-of

  • A new separator attribute separates sequences (see recipe Exporting XML to delimited data).

  • Sequence-constructor syntax is now supported alongside attribute select.

xsl:variable

  • An as attribute specifies the variable type.

xsl:with-param

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:product returns the processor name (for example, Saxon).

  • Property xsl:product-version returns the processor version (for example, 8.1).

  • Property xsl:is-schema-aware returns yes or no to indicate schema awareness.

  • Property xsl:supports-serialization returns yes or no to indicate serialization support.

  • Property xsl:supports-backwards-compatibility returns yes or no to 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.

  

Back to top

 

 

 

back to top