Learning XPath

Hi all…

Today we are going to learn about XPath.

XPath introduction

First of all we will try to understand, what is XPath. 
XML has a hierarchical structure with parent and child nodes. So we should have a mechanism to navigate through these tags to get their values. XPath is used to do this task. XPath is a major component of XSLT and you must have a good knowledge about it to write XSLT documents.
Let’s see how XPath is going to work.

XPath path expressions

The top most element in an XML document is called the root node. The root node is expressed in XPath as ‘/’.

Let’s go to deep with an example. I am going to choose my old favorite example XML document.




 <mobilephone id="1">
  D6563
  Xperia Z2a
  Sony
  2014
  
   137mm
   72mm
   11mm
  
  163g
  
   Android v4.4.2
   Quad-core 2.3 GHz
   HTML 5
  
 

 <mobilephone id="2">
  VS876
  Lucid 3
  LG
  2012
  
   131.6mm
   66mm
   9.9mm
  
  123.9g
  
   Android v4.4.2
   Quad-core 1.2 GHz
   HTML
  
 


 <mobilephone id="3">
  S7582
  Galaxy S Duos 2
  Samsung
  2013
  
   121.5mm
   63.1mm
   10.6mm
  
  118g
  
   Android v4.2
   Dual-core 1.2 GHz
   HTML
  
 

You can use /mobilephones or /mobilephones/mobilephone/model to select from the root node.
Or you can select as model and it will search for all the nodes with model tags. This way is not good for large XML documents.
If you select as //model if you really do not know the root node or you know only a part of your XML.

To select attributes, we can use @ symbol. For example if you need to get the id attribute you can use @id or for all attributes @* .

A dot ‘.‘ can represent current node and ‘..‘ can represent the parent node.

I know it is a mess for the beginners. You can understand clearly with the following examples. Try them in your own.

Ex 1:

Get the model of the first mobile phone. So we can write the path as follows.

//mobilephone[1]/model/text()

This will give the result as Text=‘D6563’ .

Let’s examine this syntax.
First it goes to the mobilephone tag and get the list of mobilephones. We have used [1] here. So it will check for the first mobilephone tag. Then /model is going inside the model tag and text() is getting the output value.

Ex 2:

Get the brand of mobiles phones which were released before 2014.

//mobilephone[released-year<2014]/brand/text()

This will give the result as Text=‘Samsung’.

Ex 3:

Get the brands of all phones which have ‘Xperia’ as a part of its name.

//mobilephones/mobilephone[starts-with(name, 'Xperia')]/brand

This will give the result as Element=‘Sony’ .

Ex 4:

Get the count of available mobile phone nodes.

count(//mobilephones/mobilephone/brand)

This will give the output as Double=‘3.0’ .

Ex 5:

Get the name of the mobile phone which has the id as 1.

//mobilephone[@id='1']/name/text()


This will give the output as Text=‘Xperia Z2a’ .


Hope you can understand what is done by XPath. There are more functions available to work with XPath. Try to get sum, sort like expressions…

See you again…

Leave a comment

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