Evaluate XPath in the Linux Command Line



Introduction

XPath is a powerful language used for navigating and selecting elements within an XML document. It is commonly used in conjunction with XSLT, a language used for transforming XML documents, to extract specific information from a document. In this article, we will discuss how to evaluate XPath expressions in the Linux command line, using the command-line tool xmllint.

Installing xmllint

Before we can begin evaluating XPath expressions in the Linux command line, we must first install xmllint. xmllint is part of the libxml2 library, which is included in most Linux distributions. To check if xmllint is already installed on your system, open a terminal and type "xmllint --version". If xmllint is not installed, it can be installed using the package manager for your Linux distribution. For example, on Ubuntu, it can be installed using the command "sudo apt-get install libxml2-utils".

Evaluating XPath Expressions

Once xmllint is installed, we can begin evaluating XPath expressions in the Linux command line. The basic syntax for evaluating an XPath expression using xmllint is as follows ?

xmllint --xpath "expression" file.xml

Where "expression" is the XPath expression to be evaluated, and "file.xml" is the XML document to be searched.

Example

For example, let's say we have the following XML document, named "example.xml" ?

<?xml version="1.0"?> <bookstore> <book category="COOKING"> <title lang="en">Everyday Italian</titl> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book> <book category="CHILDREN"> <title lang="en">Harry Potter</title> <author>J. K. Rowling</author> <year>2005</year> <price>29.99</price> </book> <book category="WEB"> <title lang="en">XQuery Kick Start</title> <author>James McGovern</author> <author>Per Bothner</author> <author>Kurt Cagle</author> <author>James Linn</author> <author>Vaidyanathan Nagarajan</auth> <year>2003</year> <price>49.99</price> </book> </bookstore>

If we want to select all book elements in the document, we can use the following XPath expression ?

xmllint --xpath "//book" example.xml

Output 

This will return the following output ?

<book category="COOKING"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book> <book category="CHILDREN"> <title lang="en">Harry Potter</title> <author>J. K. Rowling</author> <year>2005</year> <price>29.99</price> </book> <book category="WEB"> <title lang="en">XQuery Kick Start</title> <author>James McGovern</author> <author>Per Bothner</author> <author>Kurt Cagle</author> <author>James Linn</author> <author>Vaidyanathan Nagarajan</author> <year>2003</year> <price>49.99</price> </book> ```

We can also use XPath to select specific elements within the document. For example, if we want to select the title of all books written by J. K. Rowling, we can use the following XPath expression ?

Example

xmllint --xpath "//book[author='J. K. Rowling']/title" example.xml

Output 

This will return the following output ?

<title lang="en">Harry Potter</title>

We can also use XPath to select elements based on their attribute values. For example, if we want to select all books in the "COOKING" category, we can use the following XPath expression ?

Example

xmllint --xpath "//book[@category='COOKING']" example.xml

Output 

This will return the following output ?

<book category="COOKING"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book>

We can also use XPath to select specific elements based on their position within the document. For example, if we want to select the second book element in the document, we can use the following XPath expression ?

Example

xmllint --xpath "(//book)[2]" example.xml

Output 

This will return the following output ?

<book category="CHILDREN"> <title lang="en">Harry Potter</title> <author>J. K. Rowling</author> <year>2005</year> <price>29.99</price> </book>

Conclusion

XPath is a powerful language for navigating and selecting elements within an XML document. In this article, we have discussed how to evaluate XPath expressions in the Linux command line using the command-line tool xmllint. We have also covered some basic examples of how to select elements based on their position, attribute values, and the values of their child elements. With the knowledge of these basic concepts, you can now use xmllint to extract specific information from your XML documents in the Linux command line.

Updated on: 2023-01-27T12:53:04+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements