XPath Operators

Perform operations and comparisons in XPath expressions

🔢 What are XPath Operators?

XPath operators allow you to perform mathematical calculations, comparisons, and logical operations in your XPath expressions. They help filter and select nodes based on specific conditions and values.


<!-- Example XML -->
<products>
    <item price="25">Book</item>
    <item price="50">Pen</item>
</products>

<!-- Using comparison operator -->
//item[@price > 30]
                                    

Result:

Selects: <item price="50">Pen</item>

Types of XPath Operators

Arithmetic

Math operations: +, -, *, div, mod

//item[@price * 2]
⚖️

Comparison

Compare values: =, !=, <, >, <=, >=

//item[@price >= 20]
🔗

Logical

Combine conditions: and, or, not()

//item[@price > 10 and @stock]
🔀

Union

Combine node sets with |

//book | //magazine

🔹 Arithmetic Operators

Arithmetic operators perform mathematical calculations on numeric values. Use them to calculate prices, quantities, or perform other numeric operations in your XPath expressions.

<!-- Sample XML -->
<store>
    <product price="10" quantity="5">Notebook</product>
    <product price="15" quantity="3">Pen Set</product>
</store>

<!-- Addition -->
//product[@price + @quantity > 14]

<!-- Multiplication -->
//product[@price * @quantity > 40]

<!-- Division (use 'div' keyword) -->
//product[@price div 2 < 6]

Result:

Addition: Selects both products

Multiplication: Selects Notebook (10*5=50)

🔹 Comparison Operators

Comparison operators compare two values and return true or false. They're essential for filtering nodes based on attribute values or text content.

<!-- Sample XML -->
<library>
    <book year="2020">Modern Web</book>
    <book year="2023">AI Basics</book>
    <book year="2021">Data Science</book>
</library>

<!-- Equal to -->
//book[@year = '2023']

<!-- Not equal to -->
//book[@year != '2020']

<!-- Greater than -->
//book[@year > 2020]

<!-- Less than or equal -->
//book[@year <= 2021]

Result:

Equal: AI Basics

Greater than: AI Basics, Data Science

🔹 Logical Operators

Logical operators combine multiple conditions in XPath expressions. Use 'and' when all conditions must be true, 'or' when at least one must be true, and 'not()' to negate a condition.

<!-- Sample XML -->
<inventory>
    <item price="25" inStock="true">Laptop</item>
    <item price="15" inStock="false">Mouse</item>
    <item price="30" inStock="true">Keyboard</item>
</inventory>

<!-- AND operator -->
//item[@price > 20 and @inStock='true']

<!-- OR operator -->
//item[@price < 20 or @inStock='false']

<!-- NOT operator -->
//item[not(@inStock='false')]

Result:

AND: Laptop, Keyboard

OR: Mouse

NOT: Laptop, Keyboard

🔹 Union Operator

The union operator (|) combines results from multiple XPath expressions into a single node set. It's useful when you need to select different types of nodes in one query.

<!-- Sample XML -->
<media>
    <book>XML Guide</book>
    <magazine>Tech Weekly</magazine>
    <book>Web Design</book>
    <newspaper>Daily News</newspaper>
</media>

<!-- Select books OR magazines -->
//book | //magazine

<!-- Select multiple different elements -->
//book | //magazine | //newspaper

Result:

First query: XML Guide, Tech Weekly, Web Design

Second query: All four elements

🔹 Combining Multiple Operators

You can combine different operators to create complex XPath expressions that filter nodes based on multiple criteria and calculations.

<!-- Sample XML -->
<shop>
    <product price="20" discount="5" category="electronics">Phone</product>
    <product price="30" discount="10" category="electronics">Tablet</product>
    <product price="15" discount="3" category="books">Novel</product>
</shop>

<!-- Complex expression -->
//product[(@price - @discount > 15) and @category='electronics']

<!-- Multiple conditions with OR -->
//product[@price < 20 or (@discount > 5 and @category='electronics')]

Result:

First: Phone, Tablet

Second: Novel, Tablet

🧠 Test Your Knowledge

Which operator combines multiple XPath expressions?