XML technologies / XSLT / XSLT Cookbook / Exploiting the potential of XSLT 2.0 / Avoiding problems when porting from 1.0 to 2.0

(Excerpt from “XSLT Kochbuch” by Sal Mangano)

Problem

Not every 1.0 stylesheet works transparently in 2.0.

Solution

Watch for several pitfalls when porting 1.0 stylesheets to 2.0. XSLT 1.0 compatibility mode resolves some of them: use version=1.0 on stylesheet element <xsl:stylesheet version="1.0">. But if you want to migrate old stylesheets to 2.0, there are other solutions to these incompatibilities, as explained below.

Caution!
XSLT 2.0 processors need not support backward compatibility, although most probably will. An unsupported mode produces an error.

Sequences are not transparently converted to atomic objects

Consider fragment <xsl:value-of select="substring-before(Name, ' ')"/>. What happens in a context containing more than one Name element? XSLT 1.0 uses only the first and ignores the rest. XSLT 2.0 is stricter and reports a type error because the first argument of substring-before can only be a sequence of zero or one strings.

To fix this, get into the habit of writing <xsl:value-of select="substring-before(Name[1], ' ')"/>. Alternatively, you may want these errors because they could reveal a bug in the stylesheet or its input. In some cases, another solution is to combine multiple nodes into one before passing the sequence to a function expecting a single node. For example, <xsl:value-of select= "substring-before(string-join(Name), ' ')"/> would not produce an error.

Types are not transparently converted to other types

XSLT 1.0 was relaxed about conversions. If a function expected a number but received a string, it tried to convert it, and vice versa. This also applied between Booleans and integers or strings. Compatibility mode retains that behavior, but values can also be converted explicitly:

<xsl:variable name="a" select=" '10' "/>
<xsl:value-of select="sum($a, 1)"/> <!-- Fehler -->
<xsl:value-of select="sum(number($a), 1)"/> <!-- OK -->

<xsl:value-of select="string-length(10)"/> <!-- Fehler -->
<xsl:value-of select="string-length(string(10))"/> <!-- OK -->
<xsl:value-of select="string-length(string(true( )))"/> <!-- OK, gleich 4 -->

<xsl:value-of select="1 + true( )"/> <!-- Fehler -->
<xsl:value-of select="1 + number(true( ))"/> <!-- OK, gleich 2 -->

Extra parameters are not ignored

In XSLT 1.0, calling a template with xsl:call-template and supplying parameters not declared by the template silently ignored those extra parameters. In 2.0 this is an error. Compatibility mode disables the error; otherwise remove the extra parameters or add defaults to the existing template.

Stricter semantic checking rejects questionable usage

Examples occur with instructions xsl:number and xsl:template. If attributes level and value are used together in xsl:number, attribute level is ignored in 1.0 but causes an error in 2.0. Similarly, with xsl:template, neither priority nor mode may be defined in 2.0 without a match attribute.

Discussion

Using backward compatibility to fix 1.0 stylesheet errors has consequences. Some behavior changes. In 1.0 compatibility mode:

  • instruction xsl:value-of outputs only the first item of a sequence, rather than all items separated by spaces.

  • an attribute value template, such as <foo values="{foo}"/>, outputs only the first item of a sequence, rather than all items separated by spaces.

  • using xsl:number outputs the first number in a sequence, rather than all numbers separated by spaces.

For these reasons, avoid relying on backward compatibility when developing new stylesheets for a 2.0-capable processor.

  

 

 

 

back to top