What is XPATH in XML? - Examples & Functions

Introduction to XPath

We can define XPath is a language that determine a way to locate and process items in XML stands for extensible markup language which is accessed by using an addressing syntax whose path based on document's logical structure or hierarchy. XPath is a syntax that is used to define the parts of an XML document and use path expression to navigate in the document. It is a W3C recommendation. This is one of the major element of XSLT and also used in Xquery, Xpointer and Xlink. It allows programmers to deal with the document at a higher level of abstraction and abstract information that defined in XML information set.

Function of XPath

Imagine a city with many restaurants. Each will have different speciality and also can have different location, stars, year when the established. So all the information stored in the computer of the restaurant reviews, but when we search for the restaurant the data transfer from the resturant's reviews in our computer is through an XML file. So an XML file looks like

<?xml version='1.0'?>
<restaurants>
<category>Cooking</Category>
<name>Le meridian</name>
<type>Chinese<type>
<speciality>Chinese Noodles</speciality>
<location>Central Delhi</location>
<year>2010</year>
<price>$75 for two person</price>
<star>5 star</star>
</restaurants<
 

In code first line denotes the XML version which is looks like a file cabinet. Name of the cabinet is restaurants and inside it there are different information like category, name, type, speciality, location, prices and so on. So restaurants is the root element or we can say root node and the information with the specify name is known as a node which is denoted by the opening and closing tags. So in the above XML file root node is restaurants and the other nodes are category, name, type, speciality, location, prices and so on. There is a method to secure the XML file is that write code for the file in any preferred language like C++, java and so on.

Using Xpath to get data from XML Files

Now we are going to show you sample example of the above XML file:

<?xml version='1.0' encoding='UTF-8'?>
<restaurants>
<category='FAMILY_STYLE'>
<name>The One - Le Meridien</name>
<type>Stone oven Pizza</type> 
<speciality>Mediterranean</speciality>
<location>Central Delhi</location>
<year>1992</year>
<price>$75 for two person</price>
<star>5 star</star>
</restaurants>
<category='Ethnic'>
<name>Moti mahal</name>
<type>Chicken</type>
<speciality>Tandoori Chicken</speciality>
<location>Darya Ganj</location>
<year>1947</year>
<price>2000 for two person</price>
<star>4 star</star>
</restaurants>
<category='Fast Food'>
<name>kennedy's</name>
<type>pies</type>
<speciality>Kennedy's Pies</speciality>
<location>west patel nagar</location>
<year>1998</year>
<price> 1000 for two person</price>
<star>4 star</star>
</restaurants>