XPath Examples

Practical XPath expressions for real-world scenarios

💡 XPath in Action

Learn XPath through practical examples that demonstrate common use cases. These real-world scenarios show how to select, filter, and navigate XML data effectively using various XPath expressions and techniques.


<!-- Sample XML -->
<bookstore>
    <book category="web">
        <title>Learning HTML</title>
        <price>29.99</price>
    </book>
</bookstore>

<!-- XPath Example -->
//book[@category='web']/title
                                    

Result:

<title>Learning HTML</title>

Common XPath Patterns

🎯

Select All

Get all elements of a type

//book
🔍

Filter by Attribute

Select based on attribute value

//book[@category='fiction']
📍

Position

Select by position in list

//book[1]
🔗

Navigate

Move through element hierarchy

//book/title

🔹 Selecting Elements by Name

The most basic XPath expressions select elements by their tag name. Use // to search anywhere in the document or / for specific paths.

<!-- Sample XML -->
<library>
    <section name="fiction">
        <book>The Great Novel</book>
        <book>Mystery Story</book>
    </section>
    <section name="science">
        <book>Physics 101</book>
    </section>
</library>

<!-- Select all books anywhere -->
//book

<!-- Select books in specific path -->
/library/section/book

<!-- Select all sections -->
//section

Result:

//book: All 3 book elements

//section: Both section elements

🔹 Filtering with Attributes

Use square brackets with @ symbol to filter elements based on their attribute values. This is one of the most powerful XPath features for precise selection.

<!-- Sample XML -->
<products>
    <item id="101" category="electronics" inStock="true">Laptop</item>
    <item id="102" category="books" inStock="false">Novel</item>
    <item id="103" category="electronics" inStock="true">Phone</item>
</products>

<!-- Select by category -->
//item[@category='electronics']

<!-- Select by stock status -->
//item[@inStock='true']

<!-- Select by specific ID -->
//item[@id='102']

Result:

Electronics: Laptop, Phone

In stock: Laptop, Phone

ID 102: Novel

🔹 Using Position and Index

Select elements by their position in a list using numeric indices. Position counting starts at 1, and you can use functions like last() for the final element.

<!-- Sample XML -->
<playlist>
    <song>First Track</song>
    <song>Second Track</song>
    <song>Third Track</song>
    <song>Fourth Track</song>
</playlist>

<!-- Select first song -->
//song[1]

<!-- Select last song -->
//song[last()]

<!-- Select second song -->
//song[2]

<!-- Select first two songs -->
//song[position() <= 2]

Result:

First: First Track

Last: Fourth Track

First two: First Track, Second Track

🔹 Text Content Selection

Select elements based on their text content using the text() function. This is useful for finding elements containing specific words or phrases.

<!-- Sample XML -->
<menu>
    <item>Home</item>
    <item>About Us</item>
    <item>Contact</item>
    <item>Home Delivery</item>
</menu>

<!-- Select exact text match -->
//item[text()='Home']

<!-- Select containing text -->
//item[contains(text(), 'Home')]

<!-- Select starting with text -->
//item[starts-with(text(), 'About')]

Result:

Exact: Home

Contains: Home, Home Delivery

Starts with: About Us

🔹 Complex Real-World Example

Combine multiple XPath techniques to solve complex selection problems. This example shows a complete e-commerce scenario with various filtering requirements.

<!-- Sample XML -->
<store>
    <product id="1" category="electronics" price="299" rating="4.5">
        <name>Wireless Headphones</name>
        <stock>15</stock>
    </product>
    <product id="2" category="electronics" price="599" rating="4.8">
        <name>Smart Watch</name>
        <stock>8</stock>
    </product>
    <product id="3" category="books" price="25" rating="4.2">
        <name>Programming Guide</name>
        <stock>50</stock>
    </product>
</store>

<!-- Electronics under $500 -->
//product[@category='electronics' and @price < 500]

<!-- High-rated products in stock -->
//product[@rating > 4.0 and stock > 10]

<!-- Get product names only -->
//product[@category='electronics']/name

<!-- Products with specific text in name -->
//product[contains(name, 'Watch')]/name

Result:

Under $500: Wireless Headphones

High-rated in stock: Wireless Headphones, Programming Guide

Contains 'Watch': Smart Watch

🧠 Test Your Knowledge

Which XPath selects the first book element?