XML technologies / XSLT / XSLT Cookbook / XPath / Handling the extended type system of XPath 2.0
(Excerpt from "XSLT Kochbuch" by Sal Mangano)
Problem
XPath 2.0’s stricter typing rules make you curse the W3C and long for Perl.
Solution
Most incompatibilities between XPath/XSLT 1.0 and 2.0 result from type errors, whether or not a schema exists. Many XPath-related problems when porting old XSLT 1.0 code to XSLT 2.0 can be avoided by using 1.0 compatibility mode.
<xsl:stylesheet version="1.0">
<!-- ... -->
</xsl:stylesheet>In my view, you will eventually stop using compatibility mode. XPath 2.0 provides several ways to handle type conversions. First, conversion functions can be called explicitly:
(: Konvertieren des ersten X-Kinds des Kontexts in eine Zahl. :)
number(X[1]) + 17
(: Konvertieren einer Zahl in $n in einen String. :)
concat("id-", string($n))
XPath 2.0 also provides type constructors to control explicitly how a string is interpreted:
(: Konstruktion eines Datums aus einem String. :)
xs:date("2005-06-01")
(: Konstruktion von Doubles aus Strings. :)
xs:double("1.1e8") + xs:double("23000")
Finally, XPath has the operators castable as, cast as and treat as. You will generally use the first two.
if ($x castable as xs:date) then $x cast as xs:date else xs:date("1970-01-01")
The operator treat as is not a conversion but an assertion promising the XPath processor that a value has a particular type at runtime. If it does not, a type error occurs. XPath 2.0 added treat as to let implementers perform static checking at compile time as well as dynamic checking, while allowing programmers to disable static type tests selectively. XSLT 2.0 implementations with static checking will probably be rare, so treat as can be ignored for now. It is more likely in advanced XQuery processors, where static checking enables optimizations.
Discussion
Running an XSLT 2.0 processor in 1.0 compatibility mode does not prevent use of new 2.0 functions. The mode merely activates certain XPath 1.0 conversion rules.
It permits nonnumeric types where numbers are expected, converting them automatically through atomization followed by
number().(: Im Kompatibilitätsmodus wird der folgende Ausdruck zu 18.1 ausgewertet, in 2.0 ist es jedoch ein Typfehler. :)"1.1" + "17"It permits nonstring types where strings are expected, converting them automatically through atomization followed by
string().(: Im Kompatibilitätsmodus wird der folgende Ausdruck zu 2 ausgewertet, in 2.0 ist es jedoch ein Typfehler. :)string-length(1 + 2 + 3 + 4 + 5)It automatically discards extra items from sequences of length two or more used where a single item is expected. This often occurs when a path expression’s result is passed to a function.
<poem>
<line>In einem Sägewerk an der Peene</line>
<line>da sägte ein Mann ganz alleene.</line>
<line>An der Wand stand gedruckt</line>
<line>"Meine Hand für`s Produkt."</line>
<line>Da lag se ooch schon mang de Späne!</line>
</poem>(: Im Kompatibilitätsmodus werden beide Ausdrücke zu 30 ausgewertet, in 2.0 liefert der erste Ausdruck jedoch einen Typfehler. :)string-length(/poem/line)string-length(/poem/line[1])
Copyright © 2006 O'Reilly Verlag GmbH & Co. KG
You may print the online version for private use.
Otherwise, this chapter from "XSLT Kochbuch" is subject to the same terms as the bound edition: the work and all its parts are protected by copyright. All rights reserved, including reproduction, translation, microfilming, storage and processing in electronic systems.
O'Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de