Here we’ll show how XPath works. Let’s take the following XML as a lab rat:

Select the year of the second item

//item[2]/year

Select all USD prices (price nodes)

//price[@currency=’USD’]

Get all names of items with price>20

//price[text()>20]/preceding::title/text()

Get text of all EUR price nodes having any text

//price[@currency=”EUR” and text()]/text()

Get everything following any title with non-English language

//title[ not (@lang=’en’) ]/ following::*

Get all names of items with price>20

//price[text()>20]/preceding::title/text()

Get all items of 20th century

//item[(year/text()>=1901) and (year/text()<=2000)]

or

//item[(year>=1901) and (year<=2000)]

Get all names of items with price>20

//price[text()>20]/preceding::title/text()

Get all attributes

//@*

Get all authors and years

//author | //year

Get all titles starting with Gone

//title[starts-with(text(), “Gone”)]

Get total sum of all USD prices

*Function sum() is supported starting from XPath 2.0
sum(//price[@currency=’USD’])

or longer version:

/list/sum(item/prices/price[@currency=’USD’])