Comparison expressions return the result of comparing two values. XPath 2.0 actually supports four kinds of comparison expressions: value comparisons, general comparisons, node comparisons, and order comparisons. We'll take a look at them all here, starting with value and general comparisons. These two types of comparisons have been added so that XPath 2.0 can support comparisons with both single values and with sequences. Value ComparisonsYou use the value comparison operators when you're working with atomic values. Here they are:
These operators give you a result of true or false . Here's an examplesay that $temperature holds the value 68; in that case, this expression would evaluate to true : $temperature lt 72 This comparison is true only if $planet has a single < name > child element and its value is "Venus": $planet/name eq "Venus"
General ComparisonsYou can use general comparisons on sequences (including singleton sequences). Here are the general comparisons:
ESCAPING RULES As in XPath 1.0, when you use XPath expressions inside an XML document, the XML escaping rules for special characters should be followed. For example, "<" should be written as "<". You use these operators on sequences. (What actually happens is that a value comparison operator, eq , ne , lt , le , gt , or ge depending on which corresponding general comparison operator was usedis used to compare individual items in the sequence.) The software evaluating a general comparison usually will return a value of true as soon as it finds an item in the first operand and an item in the second operand for which the value comparison is true. Here's an example pointing out how these operators deal with sequences, not just individual values. In this case, we're comparing two sequences, (1, 2) and (2, 3) with the general equality operator: (1, 2) = (2, 3) In this case, the result is true because the value 2 appears in both sequences. Here, however, the result is false, because there is no value in the first sequence that is equal to a value in the second: (1, 2) = (3, 4) As with value comparisons, this comparison is true only if $planet has a single <name> child element and its value is "Venus": $planet/name = "Venus" Node ComparisonsYou can use node comparison expressions to compare nodes using the is operator. A comparison expression with the is operator is true if the two operands are nodes that are identical; otherwise it is false. For example, this comparison is true only if the left and right sides each evaluate to exactly the same single node: //planet[name="Venus"] is //planet[days=116.75] Order ComparisonsYou use order comparison expressions to compare the order of nodes; both operands here must be either a single node or an empty sequence (if either operand is an empty sequence, the result of the comparison is an empty sequence). Here are the order comparison operators:
Using the << operator returns true if the first operand node is earlier than the second operand node in document order; otherwise it returns false . Using the >> operator returns true if the first operand node is later than the second operand node in document order; otherwise it returns false . Here's an example: //planet[name="Venus"] << //planet[days=116.75] This example returns true if the node matched by //planet[name="Venus"] comes earlier in document order than the node matched by //planet[days=116.75] . |