What is XPATH in XML? - Examples & Functions

Instructor: Sudha Aravindan

Sudha is currently an Information Technology Specialist and a EdD student at the University of Delaware.

Data transferred from the web server to the computers and mobile devices that we all use is stored in files that are saved with an . xml file name, known as XML files. One of the methods to read data from XML files is by using a language known as XPath. This lesson will tell you how to use XPath to find XML data.

Introduction to XPath

When you browse a shoe store website and add shoes to your shopping cart, the specific shoes that you select by size, color, brand, etc., and the price of the shoe are all different pieces of data that are transmitted from the computer server where the data is stored to the web browser that you are using to browse the shoe store. As a customer, you may not be interested in learning more about how many red shoes are sold or what the most popular brand of shoe is, but the shoe store and all companies in general need to learn more about the data to understand what products are sold and how many sales are generated.

All of the information that is transferred between the web server and the computers we use can be saved as XML files, and we can then use the XPath query language to search the XML data files, and even compute values based on selected criteria. XPath stands for XML Path Language, and the most important function of XPath is that it tells us the path, or address, of the various pieces of data in the XML file.

Function of XPath

Imagine a bookstore with many books. Each book will have a different title and can have different authors, different prices, different years of publication, and can be on different subjects. All of this information is stored in the computer of the bookstore, but when we search the bookstore, one of the ways that the data is transferred from the bookstore computer to our computer is through an XML file. An XML file contains data, and looks like this:

<?xml version='1.0'?>




<title>Chinese Noodles</title>

<author>Chen Li</author>




Note that the first line states the XML version. If you look at the XML file, it is structured like a file cabinet. The name of the file cabinet is bookstore. Inside the file cabinet there are different files or different pieces of information - category, language, title, author, year, price. Bookstore is known as the root element, or root node, because all other information is within the bookstore. Each piece of information inside the root node is known as a node. The nodes are the names between the tags < > and </ >.

In this XML file, the root node is bookstore. The nodes are: category, language, title, author, year, price.

One way to search the XML file is by writing code using a programming language like C++, Java or Visual Basic. Because XPath is a querying language, it needs a processor to process and convert the query language, or script, that we humans type into a format that the computer can understand, and to then return the result back in human language. There also are online XPath tools that will run an XPath script and return results from the XML file. An XML file is similar to a Word file and can be read using a simple word processing program like Notepad, for example.

Using XPath to Get Data From XML Files

Below is a sample XML file:

<?xml version='1.0' encoding='UTF-8'?>


<book category='COOKING'>


<title>Chinese Noodles</title>

<author>Chen Li</author>




<book category='CHILDREN'>


<title>The Fairy in the Window</title>

<author>Peggy White</author>




<book category='GARDENING'>


<title >Rose and Care</title>

<author>Christian Croix</author>





As you can see, this file lists the books in the bookstore and shows all the information about the books:

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create An Account