Boolean Operators
XPath provides several boolean operators. They’re all straightforward (in XPath 1.0, anyway), so we’ll just list them here.
Note
When you’re working with boolean expressions in XPath,
remember that the values 'true'
and 'false'
are just
strings. If you need to use the boolean values, use the
functions true()
and false()
. Simply using true
in an XPath expression means a node
whose name is true
, which is
almost certainly not what you want. To emphasize the point, we’ll
refer to the boolean values with their functions.
As we saw in the section on mathematical operators, converting
true()
and false()
to numbers
returns 1
and 0
, respectively. XPath defines rules for
converting different datatypes to boolean values. See Converting to boolean values” in Chapter 5
for all the details.
Comparing expressions
There are several operators from XPath 1.0 (and 2.0) that compare expressions. We’ll look at those here:
=
(equal)Given two expressions, returns
true()
if the two expressions evaluate to the same value, and returnsfalse()
otherwise.!=
(not equal)Given two expressions, returns
true()
if the two expressions do not evaluate to the same value, and returnsfalse()
otherwise.<
or<
(less than)Given two expressions, returns
true()
if the first expression evaluates to a value less than the second. Otherwise, it returnsfalse()
. The less than operator is usually escaped with<
so that it doesn’t look like the opening arrow of an XML tag.<=
or<=
(less than or equal)Given ...
Get XSLT, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.