Chapter 54: XPath Operators
XPath Operators tutorial — written as if I am your personal teacher sitting next to you with a whiteboard, markers, tea, and unlimited patience.
We will go very slowly, step by step, from the very basics to advanced usage. Every operator will be explained with:
- clear definition
- visual tree examples
- many small → medium → realistic examples
- comparison with other operators
- common beginner mistakes
- “try this yourself” exercises
- real-world context
Let’s start.
Lesson 0 – Why operators matter in XPath
XPath is not only about paths (/book/title). The real power comes when you combine paths with conditions, comparisons, logic, arithmetic and functions.
Operators are the tools that let you:
- Filter nodes ([@price > 1000])
- Combine conditions (and, or, not)
- Do math (price * quantity)
- Compare strings and numbers (contains(), starts-with(), =, !=, <, >)
Without operators, XPath would be just a simple path language. With operators — it becomes a full query language.
Lesson 1 – The four main groups of operators
| Group | Purpose | Most common operators | Frequency in real code |
|---|---|---|---|
| Comparison | Compare values | =, !=, <, <=, >, >= | ★★★★★ Very high |
| Logical | Combine conditions | and, or, not() | ★★★★★ Very high |
| Arithmetic | Do math on numbers | +, -, *, div, mod | ★★★ Medium |
| Node-set | Combine or filter sets of nodes | ` | , union(same as |
Lesson 2 – Comparison operators (the ones you will use the most)
| Operator | Meaning | Works on | Example XPath | What it selects | Common trap / note |
|---|---|---|---|---|---|
| = | equals | strings & numbers | //book[@year = 2018] | Books from 2018 | Case-sensitive for strings |
| != | not equals | strings & numbers | //book[@inStock != “true”] | Books not in stock | != works with strings too |
| < | less than | numbers | //book[price < 500] | Books cheaper than 500 | Use < in XML attributes |
| <= | less than or equal | numbers | //book[price <= 499] | Books up to 499 | Same as above |
| > | greater than | numbers | //book[price > 1000] | Expensive books | Use > in XML attributes |
| >= | greater than or equal | numbers | //book[price >= 499] | Books 499 or more | Same as above |
Very important note about strings vs numbers
//book[price = 499] ← compares as NUMBER
//book[price = "499"] ← compares as STRING — usually fails!Correct ways:
//book[number(price) = 499] ← force number comparison
//book[price = "499.00"] ← exact string matchReal-world tip In real XML, prices often have decimals → always use number() when comparing numerically.
Lesson 3 – Logical operators (and, or, not)
| Operator | Meaning | Example XPath | What it selects |
|---|---|---|---|
| and | both true | //book[@year > 2000 and price < 1000] | Modern books that are still affordable |
| or | at least one true | //book[@category=”fiction” or @category=”self-help”] | Fiction or self-help books |
| not() | logical NOT | //book[not(@inStock = “true”)] | Books not in stock |
Realistic example — books that are either cheap OR old:
//book[price < 400 or @year < 2000]Very common mistake:
//book[@year = 2018 or 1997] ← WRONG!Correct:
//book[@year = 2018 or @year = 1997]Lesson 4 – Arithmetic operators (math inside XPath)
| Operator | Meaning | Example XPath | Result / use case |
|---|---|---|---|
| + | addition | //price + 100 | Calculate price + tax |
| – | subtraction | //price – 50 | Discounted price |
| * | multiplication | //quantity * //price | Total cost = qty × price |
| div | division | //total div 2 | Half price |
| mod | modulo (remainder) | position() mod 2 = 1 | Select odd-positioned elements |
Real example — select books where price after 18% GST is less than 600
//book[number(price) * 1.18 < 600]Lesson 5 – Node-set operator: the pipe
= union — combine two node-sets
//book/title | //magazine/title→ all titles from books and magazines
//book | //magazine→ all books and all magazines
Lesson 6 – Try yourself exercises (do these!)
- Select all books that are either from 2018 or cost less than 400
- Select titles that contain “Dad” and have price less than 400
- Select books where price after 18% tax is greater than 500
- Select all titles (from books and magazines)
- Select books that are not in stock and cost more than 300
- Select odd-positioned books (position() mod 2 = 1)
- Select books published after 2000 and in English
Lesson 7 – Real-world context (where these operators are really used)
- Browser DevTools → $x(“//div[@class=’product’ and .//price > 1000]”)
- Web scraping → //td[. > 500 and contains(., ‘USD’)]
- XSLT → <xsl:if test=”price > 1000 and @currency=’EUR'”>
- Automated testing → //button[text()=’Submit’ and @disabled=’disabled’]
- e-Invoice / EDI → //cbc:PayableAmount[@currencyID=’INR’ and number(.) > 100000]
- SOAP responses → //ns:Amount[ns:Currency=’USD’ or ns:Currency=’EUR’]
Would you like to continue with one of these next?
- Very deep dive into comparison operators (strings vs numbers, case sensitivity)
- Logical combinations – complex filters with and/or/not
- Arithmetic in predicates – calculating inside filters
- The pipe operator in detail (union tricks)
- XPath functions that work with operators (contains, starts-with, normalize-space…)
- Real-world examples — e-invoice filtering, RSS item selection, SOAP payload checks
Just tell me which direction feels most useful or interesting for you right now! 😊
