Skip to main content

How to Use XPath Comparison Operators

While XPath is excellent for navigating the structure of an XML document, its real power is unlocked when you use it to filter nodes based on their values. The key to this filtering is the use of predicates (expressions inside square brackets [...]), which rely on comparison operators to test a node's content or attributes.

Core Concept: Predicates

A predicate is a filter expression written inside square brackets []. It is used to find nodes that meet a specific condition. Comparison operators are almost always used inside these predicates.

Syntax: //nodename[condition]

XPath Comparison Operators

XPath provides a standard set of operators for comparing values. When used with text nodes, the comparison is based on the numeric value if the text can be converted to a number; otherwise, it's a string comparison.

OperatorDescription
=Equals: True if the values are equal.
!=Not Equals: True if the values are not equal.
<Less Than: True if the first value is less than the second.
>Greater Than: True if the first value is greater than the second.
<=Less Than or Equal To: True if the first value is less than or equal to the second.
>=Greater Than or Equal To: True if the first value is greater than or equal to the second.

Practical Examples

All examples will refer to the following XML document:

<bookstore>
<book category="cooking">
<title>Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="children">
<title>Harry Potter</title>
<author>J. K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="web">
<title>Learning XML</title>
<author>Tom Nolan</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>

Equals (=)

This is the most common operator, used to find a node with an exact value.

Example: Select the book written by "J. K. Rowling".

//book[author = 'J. K. Rowling']

Output: this selects the single <book> element for "Harry Potter".

<book category="children">
<title>Harry Potter</title>
<author>J. K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
note

You can also use it to check attribute values by prefixing the attribute name with @.

//book[@category = 'cooking']

Output: this selects the <book> element for "Everyday Italian".

<book category="cooking">
<title>Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>

Not Equals (!=)

This operator selects all nodes that do not match a specific value.

Example: Select all books that were not published in 2005.

//book[year != 2005]

Output: this selects the <book> element for "Learning XML".

<book category="web">
<title>Learning XML</title>
<author>Tom Nolan</author>
<year>2003</year>
<price>39.95</price>
</book>

Greater Than (>) and Less Than (<)

These operators are used for numeric or chronological comparisons.

example: Select all books with a price greater than 30.00.

//book[price > 30.00]

Output: this selects the <book> element for "Learning XML".

<book category="web">
<title>Learning XML</title>
<author>Tom Nolan</author>
<year>2003</year>
<price>39.95</price>
</book>

Example: Select all books published before 2005.

//book[year < 2005]

Output: this also selects the <book> element for "Learning XML".

<book category="web">
<title>Learning XML</title>
<author>Tom Nolan</author>
<year>2003</year>
<price>39.95</price>
</book>

Greater Than or Equal To (>=) and Less Than or Equal To (<=)

These operators are inclusive, including the value being compared.

Example: Select all books published in or after 2005.

//book[year >= 2005]

Result: This selects the two <book> elements for "Everyday Italian" and "Harry Potter".

<book category="cooking">
<title>Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="children">
<title>Harry Potter</title>
<author>J. K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>

Conclusion

Comparison operators are the key to unlocking the filtering power of XPath. By using them inside predicates ([...]), you can move beyond simple structural navigation and create precise queries that select nodes based on their actual content and attribute values.

  • Use = and != for equality checks on text or attributes.
  • Use >, <, >=, and <= for numeric comparisons on values like prices, years, or counts.