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:

About Comparison Operators

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
Table 63. Comparison Operator Descriptions

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.

How the XPath Processor Evaluates Comparisons

A query can compare values of elements. For example:

last-name [. = "foo"] 
               

            

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:

/bookstore/book/author/last-name="Bob"
               

            

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:

/bookstore/book/author[last-name="Bob"]
               

            

Comparing Node Sets

You can compare

  • Two node sets
  • A node set and a number
  • A node set and a string
  • A node set and a Boolean value

Two Node Sets

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 >=.

A Node Set and a Number

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.

A Node Set and a String

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.

A Node Set and a Boolean Value

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.

Comparing Single Values With = and !=

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:

  • If at least one object to be compared is Boolean, the XPath processor converts the other object to Boolean as if by applying the boolean() function.
  • If at least one object to be compared is a number, and neither is Boolean, the XPath processor converts the nonnumber object to a number as if by applying the 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.

Comparing Single Values With <=, <, >, and >=

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.

Comparison
True If and Only If
<
The first number is less than the second number.
<=
The first number is less than or equal to the second number.
>
The first number is greater than the second number.
>=
The first number is greater than or equal to the second number.
Table 64. Comparison Operator Descriptions

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

a < "foo"
               

            

The return value is always false. This is because number("foo") returns NaN, and the resulting comparison, shown below, is always false.

a < NaN
               

            

Priority of Object Types in Comparisons

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.

Examples of Comparisons

The following query finds all authors whose last name is Bob:

author[last-name = "Bob"]
               

            

The next query finds authors whose degrees are not from Harvard:

author/degree[@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.

price [. > 20]
               

            

Operating on Boolean Values

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.

 
Free Stylus Studio XML Training: