XPath Operators

Welcome to The Coding College! In this guide, we’ll dive into XPath Operators, which are used to create more precise and complex XPath expressions. XPath offers a variety of operators for comparison, logical operations, arithmetic calculations, and more. These operators enhance the flexibility and power of XPath queries.

Categories of XPath Operators

XPath operators are broadly classified into:

  1. Comparison Operators
  2. Logical Operators
  3. Arithmetic Operators
  4. Node Set Operators

1. Comparison Operators

XPath comparison operators are used to compare values, either within an XML document or with a constant value.

OperatorDescriptionExample XPath ExpressionExplanation
=Equal to//book[@id="1"]Selects <book> elements with id="1".
!=Not equal to//book[@id!="2"]Selects <book> elements with id not 2.
<Less than//price[. < 30]Selects <price> elements less than 30.
<=Less than or equal to//price[. <= 30]Selects <price> elements <= 30.
>Greater than//price[. > 30]Selects <price> elements greater than 30.
>=Greater than or equal to//price[. >= 30]Selects <price> elements >= 30.

2. Logical Operators

Logical operators are used to combine multiple conditions in an XPath expression.

OperatorDescriptionExample XPath ExpressionExplanation
andBoth conditions must be true//book[@id="1" and @genre="Programming"]Selects <book> with id="1" and genre="Programming".
orAt least one condition must be true//book[@id="1" or @genre="Fiction"]Selects <book> with id="1" or genre="Fiction".
not()Negates a condition//book[not(@id="2")]Selects <book> elements without id="2".

3. Arithmetic Operators

XPath supports arithmetic operators for numerical calculations.

OperatorDescriptionExample XPath ExpressionExplanation
+Addition//price[. + 10 > 40]Adds 10 to <price> and selects prices > 40.
-Subtraction//price[. - 5 < 25]Subtracts 5 from <price> and selects prices < 25.
*Multiplication//price[. * 2 = 60]Multiplies <price> by 2 and selects prices = 60.
divDivision//price[. div 2 = 20]Divides <price> by 2 and selects prices = 20.
modModulus (remainder)//price[. mod 3 = 0]Selects <price> elements divisible by 3.

4. Node Set Operators

Node set operators work on collections of nodes and are particularly useful for combining or comparing sets of nodes.

OperatorDescriptionExample XPath ExpressionExplanation
``Union (combine results)`//title
=Compare node set with a value//book[@genre="Programming"]/titleSelects <title> of <book> with genre="Programming".
!=Compare node set with a value//book[@genre!="Programming"]/titleSelects <title> of <book> without genre="Programming".

Example XML Document

We’ll use this XML structure for examples:

<library>
  <book id="1" genre="Programming">
    <title>XML Basics</title>
    <author>John Doe</author>
    <price>29.99</price>
  </book>
  <book id="2" genre="Fiction">
    <title>The Great Adventure</title>
    <author>Jane Smith</author>
    <price>19.99</price>
  </book>
</library>

Practical Examples

1. Select Books with Prices Greater Than 20

XPath Expression:

//book[price > 20]

Result:

<book id="1" genre="Programming">
  <title>XML Basics</title>
  <author>John Doe</author>
  <price>29.99</price>
</book>

2. Select Books with Genre “Programming” AND Price Less Than 30

XPath Expression:

//book[@genre="Programming" and price < 30]

Result:

<book id="1" genre="Programming">
  <title>XML Basics</title>
  <author>John Doe</author>
  <price>29.99</price>
</book>

3. Select Titles OR Authors

XPath Expression:

//title | //author

Result:

<title>XML Basics</title>
<title>The Great Adventure</title>
<author>John Doe</author>
<author>Jane Smith</author>

4. Select Books with IDs Not Equal to “2”

XPath Expression:

//book[@id != "2"]

Result:

<book id="1" genre="Programming">
  <title>XML Basics</title>
  <author>John Doe</author>
  <price>29.99</price>
</book>

5. Apply Arithmetic Operators

  • Increase Price by 10 and Filter:
    XPath Expression: //price[. + 10 > 30] Result: <price>29.99</price>
  • Find Prices Divisible by 5:
    XPath Expression: //price[. mod 5 = 0] Result: <price>19.99</price>

Conclusion

XPath operators significantly enhance the capabilities of XPath expressions, allowing you to handle complex queries with ease. By mastering comparison, logical, arithmetic, and node set operators, you can extract exactly the data you need from XML documents.

Leave a Comment