Copyright

What is XPATH in XML? - Examples & Functions

Instructor: Sudha Aravindan

Sudha has a Doctor of Education Degree and is currently working as a Information Technology Specialist.

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'?>

<bookstore>

<category>Cooking</Category>

<language>Chinese</language>

<title>Chinese Noodles</title>

<author>Chen Li</author>

<year>2010</year>

<price>$12.99</price>

</bookstore>

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'?>

<bookstore>

<book category='COOKING'>

<lang>Chinese</lang>

<title>Chinese Noodles</title>

<author>Chen Li</author>

<year>2010</year>

<price>$12.99</price>

</book>

<book category='CHILDREN'>

<lang>English</lang>

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

<author>Peggy White</author>

<year>2015</year>

<price>$9.99</price>

</book>

<book category='GARDENING'>

<lang>French</lang>

<title >Rose and Care</title>

<author>Christian Croix</author>

<year>2011</year>

<price>$9.99</price>

</book>

</bookstore>

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
Support