Categories
Development

XPath in Examples

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

<?xml version="1.0" encoding="UTF-8"?>
<list>
   <item new='true'>
     <title lang="en">Pop-Music DVD</title>
     <author >K. A. Bred</author>
     <year>2012</year>
     <prices>
       <price currency="USD">29.99</price>
       <price currency="EUR">23.2</price>
     </prices>
  </item>
  <item new='false'>
    <title>Gone with the wind</title>
    <author>M. Mitchell</author>
    <year>1936</year>
    <prices>
      <price currency="USD">19.05</price>
      <price currency="EUR">15</price>
    </prices>
  </item>
</list>
Select the year of the second item
//item[2]/year
<year>1936</year>
Select all USD prices (price nodes)
//price[@currency=’USD’]
<price currency="USD">29.99</price>
<price currency="USD">19.05</price>
Get all names of items with price>20
//price[text()>20]/preceding::title/text()
Pop-Music DVD
Get text of all EUR price nodes having any text
//price[@currency=”EUR” and text()]/text()
23.2
15
Get everything following any title with non-English language
//title[ not (@lang=’en’) ]/ following::*
 <author>M. Mitchell</author>
 <year>1936</year>
 <prices>
     <price currency="USD">19.05</price>
     <price currency="EUR">15</price>
 </prices>
Get all names of items with price>20
//price[text()>20]/preceding::title/text()
Pop-Music DVD
Get all items of 20th century
//item[(year/text()>=1901) and (year/text()<=2000)]

or

//item[(year>=1901) and (year<=2000)]
<item new='false'>
        <title>Gone with the wind</title>
        <author>M. Mitchell</author>
        <year>1936</year>
        <prices>
            <price currency="USD">19.05</price>
            <price currency="EUR">15</price>
        </prices>
    </item>
Get all names of items with price>20
//price[text()>20]/preceding::title/text()
Pop-Music DVD
Get all attributes
//@*
new="true"
lang="en"
currency="USD"
currency="EUR"
new="false"
currency="USD"
currency="EUR"
Get all authors and years
//author | //year
<author>K. A. Bred</author>
<year>2012</year>
<author>M. Mitchell</author>
<year>1936</year>
Get all titles starting with Gone
//title[starts-with(text(), “Gone”)]
<title>Gone with the wind</title>
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’])
49.04
How to find Xpath using the browser Web Developer Tools (F12)

Leave a Reply

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


The reCAPTCHA verification period has expired. Please reload the page.

This site uses Akismet to reduce spam. Learn how your comment data is processed.