XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition (337 page)

BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition
7.01Mb size Format: txt, pdf, ePub

Unary plus has been added to the language largely so that any value accepted as the lexical value of a number by XML Schema is also accepted as a valid constant value in an XPath expression. XML Schema accepts
+1.0
as a legal representation of a number, so XPath 2.0 accepts it too.

The relative precedence of the union operator
|
and unary minus has changed since XPath 1.0. In 1.0, the expression
-@price|@cost
was interpreted as
-(@price|@cost)
, whereas it now means
(-@price)
|
(@cost)
. Usually this will give a type error on the grounds that the operands of
|
must be nodes, but in the case where there is no
@price
attribute and
@cost
has the value 12, the expression will return -12 in XPath 1.0 but + 12 in XPath 2.0.

Effect

The detailed rules for these operators are as follows. The rules are given here on the assumption that the special rules for XPath 1.0 backward compatibility are not in force; the changes that apply under backward-compatibility mode are described later.

1.
The operands are atomized, as described on page 220 in Chapter 5. This means that nodes are replaced by their typed values; for example, if one of the operands is the attribute node
@price
, then the typed value of this attribute is extracted.

2.
If, after atomization, either operand is an empty sequence, then the result of the operation is also an empty sequence. For example, if the context node has no
price
attribute, then the result of
@price * 0.8
is the empty sequence,
()
.

3.
If either operand after atomization is a sequence of more than one item, a type error is raised. For example, if you write the expression
price
*
0.8
and the context node has more than one child element called

, a type error ensues. The significance of it being a type error is that it may be reported either at compile time or at runtime, as discussed in the section on
Static and Dynamic Type Checking
in Chapter 5 (page 221).

4.
If either operand is an
xs:untypedAtomic
value, then it is converted (using the casting rules) to an
xs:double
. This situation will normally occur when the operand as written is a node in a schema-less document. For example, suppose the expression is
@price * 0.8
, and there is no schema, and the
price
attribute in the source document is written as
price = β€œ129.99”
. Then the attribute value will be converted to the
xs:double
value
1.2999e2
, and the result of the multiplication will also be an
xs:double
. Note the difference with numeric literals, described on page 530 in Chapter 7β€”in the case of a value contained in an untyped node, it is always converted to an
xs:double
when used as an operand of
*
, regardless of whether it is written in exponential notation or not.

Other books

Promise Me by Cora Brent
The Owl Service by Alan Garner
Cowboy of Her Heart by Honor James
Love Storm by Houston, Ruth
The Dimple Strikes Back by Lucy Woodhull
Entangled by Cat Clarke
Bear Claw Conspiracy by Andersen, Jessica