XML technologies / XPath / XPath 2.0 – Language reference / XPath expressions: operators and keywords / Sequence-type operators
(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 4.)
Operators: instance of, cast as, castable as, treat as
XPath 2.0 provides four operator keywords for checking sequence types, declaring a static type, converting types and testing beforehand whether conversion is possible.
Type checking
Operator: instance of
Type checking with Boolean operator instance of returns true if the type of the expression supplied as the first operand matches the type named as the second operand. If the checked type does not match the required type, the operator returns false.
The type name in the second operand can optionally include a cardinality indicator *, + or ? to describe a sequence’s composition.
The following example tests an integer against type xs:integer. In this case, the operator returns true:
5 instance of xs:integer
The test also returns true when the tested type is a supertype of the item’s actual type rather than the same type. The actual type xs:integer is derived by restriction (restriction) from the tested type xs:decimal, so the following test again returns true:
5 instance of xs:decimal
Instead of a concrete value, the first operand can be any XPath expression. In the following case it is the symbol '.' for the context item. This example expression returns true if the context item is an element node:
. instance of element()
Looking back at XPath 1.0
This functionality was introduced in XPath 2.0. No type checking comparable to instance of exists in XPath 1.0.
Type conversion
Operator: cast as
When a value occasionally needs to be converted to a specified data type, XPath 2.0 provides operator cast as.
The expression has two operands: an input value of any type and a target type. It creates a new value corresponding to the input, but of the target data type.
Not every input value can be converted to a value of any other type.
Instead of operator cast as, constructor functions can be used. With slightly simpler syntax, their results are fully equivalent to conversion with cast as. Each type defined in XML Schema or XPath has such a constructor function, which also accepts an input value and creates a copy with the corresponding target type.
"2007-01-01" cast as xs:date
therefore means the same as
xs:date("2007-01-01")
The following two statements also have identical effects:
($floatvalue * 0.2E-5) cast as xs:decimal
and
xs:decimal($floatvalue * 0.2E-5)
Looking back at XPath 1.0
XPath 1.0 has no type conversion in the sense of type casting. Usually, expressions perform implicit conversion when needed. Otherwise, conversion functions string(), boolean() and number() can be used.
Testing convertibility to a target type
Operator: castable as
If conversion to a required target type is impossible, XPath 2.0 stops processing with a type error. To avoid this, convertibility can be checked beforehand.
Test operator castable as serves this purpose. Like operator cast as, it accepts an input value and a target type. It returns Boolean value true if conversion would succeed, or false if it would fail.
Testing with castable as can therefore prevent runtime errors from failed conversions:
if($x castable as mein:typ)
then $x cast as mein:typ
else if($x castable as dein:typ)
then $x cast as dein:typ
else $x cast as xs:string
By guarding the actual cast as conversion with a condition expressed using castable as, conversion to the specific types is performed only when it can succeed. The alternative string conversion in the else branch is usually possible.
Looking back at XPath 1.0
XPath 1.0 needs no advance check of possible type conversion, so no corresponding test is provided.
Static type assertion
Operator: treat as
When an XPath expression is evaluated in an environment supporting static type checking, its result type can be specified using operator treat as.
The expression’s dynamic type, determined during actual evaluation, is not changed by treat as. Rather, this is an advance assertion (assertion) that the expression should have a particular type:
$isbn-nr treat as element(*, mein:isbn)
If evaluation reveals that the expression does not have the declared type, this can be treated as a runtime error.
Looking back at XPath 1.0
XPath 1.0 does not provide static type checking.
Copyright © Galileo Press, Bonn 2008
You may print the online version for your private use.
Otherwise, this chapter from “XSLT 2.0 & XPath 2.0 ― Das umfassende Handbuch” 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.
Galileo Press, Rheinwerkallee 4, 53227 Bonn