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

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

The syntax has already been covered under
Value Comparisons
on page 581: these operators are defined by the same production rule that defines the value comparison operators (the
eq
family) and the general comparison operators (
=
and friends).

For all three operators, each operand must be either a single node or an empty sequence. If either operand is an empty sequence, the result is an empty sequence (which will be treated as
false
if it is used in a boolean test such as a predicate). If either operand is a sequence containing more than one item, or an item other than a node, then a type error is reported.

The
is
Operator

The
is
operator tests whether both operands evaluate to the same node. The nodes must be identical; it's not enough to have the same name or the same value, they must actually be the same node.

Terminology here can get messy. If two nodes are identical, then they are one node, not two. Better perhaps to say that two values are references to the same node (but that's not the language of the W3 C specs). Identity causes other problems: it doesn't fit well into a language that in most other respects is purely functional. For example, if you write a function
f()
in XSLT or XQuery that creates and returns a new element node, then the expression
f() is f()
returns false, because each time
f()
is called, it creates a node with distinct identity. This breaks the rule that applies to all other XPath function calls, namely that calling the same function repeatedly with the same arguments and the same evaluation context always returns the same result.

Here's an example of how the
is
operator can be used. Sometimes you have a sequence of elements such as:


and you need to select all the


elements that follow a particular


element, up to the next


element. (I have shown all the elements as empty because we're not interested in their content for this example.) Let's suppose that the variable
$H
identifies the


element where you want to start. The expression
$H/following-sibling::p
selects all the


elements after the start element, but it doesn't stop when it reaches the next


. You want to select only the


elements whose immediately preceding


element is
$H
. Here is the expression to do this:

Other books

The Deputy's New Family by Jenna Mindel
Black Market Baby by Tabra Jordan
A Harvest of Hope by Lauraine Snelling
Atlantis Rising by Barron, T.A.
This Other Eden by Ben Elton
The Aylesford Skull by James P. Blaylock