XML Tutorial menu

XML XPath

XPath is a small language for finding elements and attributes inside an XML document, like an address for data.

The Example Document

All examples on this page use this file, shop.xml:

Example

xml
<?xml version="1.0" encoding="UTF-8"?>
<shop name="Saddar Mobiles">
  <product id="p1" category="phone">
    <name>Galaxy A15</name>
    <price>42000</price>
    <stock>8</stock>
  </product>
  <product id="p2" category="accessory">
    <name>Fast Charger</name>
    <price>2500</price>
    <stock>30</stock>
  </product>
  <product id="p3" category="phone">
    <name>Redmi 13</name>
    <price>38500</price>
    <stock>0</stock>
  </product>
</shop>

Paths

XPath paths look like folder paths:

XPathSelects
/shopThe root element
/shop/productAll product elements under the root
/shop/product/nameAll name elements of products
//nameEvery name element anywhere in the document
.The current element
..The parent of the current element
@idThe id attribute
/shop/@nameThe name attribute of shop
  • One slash / means "a direct child"
  • Two slashes // mean "anywhere below"

Example

text
//product/name/text()
Output
Galaxy A15
Fast Charger
Redmi 13

text() selects the text inside an element.

Predicates

A predicate is a condition in square brackets. It filters the results:

XPathSelects
//product[1]The first product (XPath counts from 1)
//product[last()]The last product
//product[@category='phone']Products whose category is phone
//product[price > 10000]Products with price above 10000
//product[stock = 0]/nameNames of out-of-stock products
//product[@id='p2']/priceThe price of product p2

Example

text
//product[@category='phone' and stock > 0]/name/text()
Output
Galaxy A15

XPath counts from 1, not from 0 like most programming languages. //product[1] is the first product.

Wildcards

XPathSelects
/shop/*All child elements of shop
//product/@*All attributes of every product
//*Every element in the document

Useful Functions

FunctionExampleResult here
count()count(//product)3
sum()sum(//stock)38
contains()//product[contains(name, 'Charger')]The charger product
starts-with()//product[starts-with(name, 'R')]Redmi 13
string-length()string-length(//product[1]/name)10

Example

text
sum(//product[@category='phone']/stock)
Output
8

Trying XPath

  • In Chrome or Edge DevTools, open the Console on any page and type $x("//h1") to test XPath on HTML
  • xmllint on the command line: xmllint --xpath "//product/name/text()" shop.xml
  • Python, JavaScript, PHP, Java and C# all support XPath; the next lesson shows how

Where XPath is Used

  • Reading data from XML files and API responses
  • Web scraping and automated testing tools (Selenium, Playwright) use XPath to find page elements
  • XSLT, a language that transforms XML into HTML or other formats, is built on XPath

Exercise

Exercise

Using shop.xml, which XPath selects the names of products that are out of stock?

Learn XML with a teacher

Join the Full Stack Development course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp