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"]
               

            

 
Free Stylus Studio XML Training: