Chapter 55: XPath Examples

XPath Examples tutorial — written as if I’m your personal teacher sitting next to you, whiteboard ready, explaining slowly and clearly, with lots of drawings, step-by-step reasoning, many small-to-realistic examples, common mistakes people make, “try this yourself” exercises, and real-world context.

We will start from the very simplest expressions and gradually build up to more powerful and useful ones.

Preparation: The XML document we will use in almost all examples

Save this small but realistic XML as library.xml (or paste it as a string when you test):

XML

Example 1 – The absolute simplest XPath expressions

1.1 Select all <title> elements

xpath
//title

→ Returns 4 nodes:

  • Atomic Habits
  • Rich Dad Poor Dad
  • The Alchemist
  • India Today

1.2 Select only book titles

xpath
//book/title

→ Returns 3 nodes (magazine title is excluded)

1.3 Select the title of the first book only

xpath
//book[1]/title

or

xpath
(//book/title)[1]

→ “Atomic Habits”

Important difference (very common confusion):

xpath
//book[1]/title → title of the first book child of any parent
(//book/title)[1] → the very first title in document order

In this case both give the same result, but in larger documents they can differ.

Example 2 – Using attributes in conditions

2.1 Select books published in 2018

xpath
//book[@year = "2018"]

→ returns the Atomic Habits book

2.2 Select books in English

xpath
//book[@lang = "en"]

→ returns all three books

2.3 Select books that are in stock

xpath
//book[@inStock = "true"]

→ Atomic Habits + The Alchemist

2.4 Select books that are not in stock

xpath
//book[@inStock != "true"]

or better:

xpath
//book[not(@inStock = "true")]

→ Rich Dad Poor Dad

Example 3 – Numeric comparisons

3.1 Select books cheaper than 400 INR

xpath
//book[price[@currency="INR"] and number(price) < 400]

→ Rich Dad Poor Dad (349)

3.2 Select books more expensive than 400 INR

xpath
//book[number(price) > 400]

→ Atomic Habits (499)

Important note: number(price) is necessary because price is text — without it, “499” is compared as string.

Example 4 – Combining conditions with and / or

4.1 Books that are cheap AND in stock

xpath
//book[number(price) < 400 and @inStock = "true"]

→ (none in our document)

4.2 Books that are either cheap OR old (before 2000)

xpath
//book[number(price) < 400 or @year < 2000]

→ Rich Dad Poor Dad (cheap + old)

4.3 Books that are not in stock

xpath
//book[not(@inStock = "true")]

or

xpath
//book[@inStock != "true"]

Example 5 – Using position() and last()

5.1 Select the second book

xpath
(//book)[2]

→ Rich Dad Poor Dad

5.2 Select the last book

xpath
//book[last()]

→ The Alchemist

5.3 Select every second book (odd positions)

xpath
//book[position() mod 2 = 1]

→ book 1 + book 3 (Atomic Habits + The Alchemist)

Example 6 – Using text() and string functions

6.1 Select books whose title contains “Dad”

xpath
//book[contains(title, "Dad")]

→ Rich Dad Poor Dad

6.2 Select books whose title starts with “The”

xpath
//book[starts-with(title, "The")]

→ The Alchemist

6.3 Select books whose title does not contain “Habits”

xpath
//book[not(contains(title, "Habits"))]

→ Rich Dad Poor Dad + The Alchemist

Example 7 – Combining multiple conditions (realistic filtering)

7.1 Books that are:

  • in English
  • published after 2000
  • cost less than 1000 INR
  • in stock
xpath
//book[
@lang = "en" and
@year > 2000 and
price[@currency="INR" and number(.) < 1000] and
@inStock = "true"
]

→ Atomic Habits (only one matches all conditions)

Example 8 – Try yourself exercises (do these!)

  1. Select all prices that are in INR
  2. Select the title of the second book
  3. Select books cheaper than 400 INR
  4. Select books published after 2000
  5. Select all author names
  6. Select the last book in the document
  7. Select books that are not in stock and cost more than 300 INR
  8. Select books whose title contains “Dad” or starts with “A”
  9. Select books that have a price and are in stock
  10. Select the third element that is a child of <library>

Summary – XPath Operators Quick Reference

Operator Purpose Example XPath Tip / Common mistake
= equals @year = 2018 Case-sensitive for strings
!= not equals @inStock != “true” Works for strings too
< > <= >= numeric comparison number(price) < 500 Use number() — otherwise string comparison
and both true @year > 2000 and price < 1000 Use parentheses when combining
or at least one true @category=”fiction” or @category=”self-help” Use parentheses when mixing with and
not() logical NOT not(@inStock = “true”) not() is a function — needs parentheses
+ – * div mod arithmetic price * 1.18 div for division (not /)
union (combine node-sets) `//book/title

Would you like to continue with one of these next?

  • Very deep dive into comparison operators (strings vs numbers, case sensitivity, number(), string())
  • Complex logical conditions – combining and/or/not with parentheses
  • Arithmetic inside predicates – calculating totals, discounts, GST…
  • The pipe operator – combining different branches
  • XPath functions that work with operators (contains, starts-with, normalize-space, count, sum…)
  • 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! 😊

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *