Home > Online Product Documentation > Table of Contents > Comparing Values
In queries, you can specify operators that compare values. Comparison operations return Boolean values. If you want to obtain the nodes for which a comparison tests
true, enclose the comparison in a filter.
This section discusses the following topics:
The comparison operators you can specify are listed in Table 63:
|
Operator
|
Description
|
|---|---|
=
|
Equality
|
!=
|
Inequality
|
<
|
Less than
|
<=
|
Less than or equal
|
>
|
Greater than
|
>=
|
Greater than or equal
|
You can specify single or double quotation marks (' or ") as string delimiters in expressions. This makes it easier to construct and pass queries from within scripting languages.
A query can compare values of elements. For example:
The XPath processor compares the value of each
last-name element in the current context with the value
"foo". The result of each comparison is a Boolean value. The XPath processor returns the
last-name elements for which the comparison yields
true.
As mentioned before in
Filtering Results of Queries, the XPath processor evaluates filters with respect to a context. For example, the expression
book[author] means for every
book element that is found, determine whether it has an
author child element. Likewise,
book[author = "Bob"] means for every
book element that is found, determine whether it contains an
author child element whose value is
"Bob".
Comparisons are case sensitive. For example,
"Bob" and
"bob" are not considered to be equal.
Remember that comparisons return Boolean values. For example:
You might think that this query returns authors whose last name is
"Bob". But this is not the case. This query returns a single Boolean value. It tests each
last-name element to determine if its value is
"Bob". As soon as the XPath processor finds a
last-name element that tests true, it returns
true. If no nodes test true, this query returns
false.
To obtain
author elements whose last name is
"Bob", enclose the comparison in a filter as follows:
You can compare
Suppose the objects you want to compare are both node sets. The result is true only in the following case. There is a node in the first node set and a node in the second node set such that the result of performing a comparison on the values of the two nodes is
true. For string values, the comparison can be
= or
!=. For numeric values, the comparison can also be
<,
>,
<=, or
>=.
Now suppose one object to be compared is a node set and the other is a number. The XPath processor searches for a node in the node set that yields a true result when its number value is compared with the number that is not in the node set. If necessary, the XPath processor uses the
number() function to convert values to numeric values. If and only if the XPath processor finds such a node, the result is
true.
Sometimes you want to compare a node set with a string. The XPath processor searches for a node in the node set that yields a true result when its string value is compared with the string that is not in the node set. If necessary, the XPath processor uses the
string() function to convert values to string values. If and only if the XPath processor finds such a node, the result is
true.
Finally, suppose you want to compare a node set with a Boolean value. This tests true if and only if the result of performing the comparison on the Boolean value and on the result of converting the node set to a Boolean value using the
boolean() function is
true.
When neither object to be compared is a node set and the operator is
= or
!=, the XPath processor compares the objects by converting them to a common type and then comparing them. The XPath processor converts the objects to a common type as follows:
boolean() function.
number() function.
If the objects to be compared are neither Boolean nor numeric, the XPath processor compares the string values of the objects as if by applying the
string() function to each object.
The
= comparison returns
true if and only if the objects are equal. The
!= comparison returns
true if and only if the objects are not equal. Numbers are compared for equality according to IEEE 754. Two Boolean values are equal if either both are
true or both are
false. Two strings are equal if and only if they both consist of the same sequence of Universal Character Set (UCS) characters.
When neither object to be compared is a node set and the operator is
<=,
<,
>=, or
>, the XPath processor performs the comparison by converting both objects to numbers and comparing the numbers according to IEEE 754.
The XPath processor always evaluates these comparisons in terms of numbers. You cannot use the less than and greater than operators to order strings. This is especially important to remember when you compare a number with a string. For example, suppose you want to evaluate the expression
The return value is always
false. This is because
number("foo") returns
NaN, and the resulting comparison, shown below, is always
false.
When the XPath processor performs a comparison, if either operand is a Boolean value, the XPath processor automatically converts the other operand to a Boolean value, if necessary, and makes a Boolean comparison.
If either operand is numeric and neither operand is Boolean, the XPath processor automatically converts the other operand to a numeric value, if necessary, and performs a numeric comparison.
If neither operand is numeric or Boolean, the XPath processor performs a string comparison.
The following query finds all authors whose last name is Bob:
The next query finds authors whose degrees are not from Harvard:
The following query returns prices that are greater than 20 dollars. This assumes that the current context contains one or more
price elements.
You can use the
= or
!= operator to compare Boolean values. If you try to use any other operator to compare Boolean values, you receive an error.