Your XML friend XPATH command line xmllint
March 5th, 2011 mysurface Posted in Text Manipulation, xmllint | Hits: 123087 | 5 Comments »
Do you have a large XML to analyze? to query for info? Maybe you are using a XML viewer, a string search for that. But if your XML have a proper structure and you have understand enough for the structure, you may want to consider to use XPATH.
[Q] What is XPATH?
XPath is a “language” for finding information in an XML document. Something like SQL, it has its own syntax to help you to query for your info in an XML. To know more about XPATH, can check out this XPATH tutorial.
[Q] Is there any XPATH command that I can use to query my XML?
xmllint, which comes with libxml2.
xmllint provides you a shell where you can continuously to query your XML.
xmllint --shell myXML.xml
Shell mode is also a good way for you to learn up XPATH, type ‘help’ in the xmllint shell shows you a list of command it support.
Take books.xml sample as example.
To query all the books title, you can do this in the shell.
> cat //title
-------
<title lang="en">Everyday Italian</title>
-------
<title lang="en">Harry Potter</title>
-------
<title lang="en">XQuery Kick Start</title>
-------
<title lang="en">Learning XML</title>
xmllint shell support grep where you can search with string keywords
> grep XML
/comment() : c-- 20
/bookstore/book[4]/title : t-- 12 Learning XML
grep in shell is not XPATH syntax, for XPATH to do something similar to grep, you can use contains().
> cat //*[contains(*,"XML")]
XPATH supports many functions to enlighten your query.
[Q] I wanna use the XPATH result for further manipulation, can I get it with xmllint?
--xpath option.
You can do this.
xmllint --xpath //title books.xml
And.... xmllint can do more than that.
I hope you enjoy reading this, bye.







February 20th, 2012 at 2:11 am
Interesting justification. I really like to read it Martha
February 27th, 2012 at 8:57 am
nice post
March 11th, 2012 at 3:17 am
The buck stops with all the guy who signs the checks.
Failure or success in operation is caused more with the mental attitude even than by mental capacities.
July 23rd, 2012 at 8:02 pm
Thanks this is exactly what I needed.
I googled for XPath tools and got a lot of fancy online and offline apps, some fancy looking Firefox plugins (which all crashed) and finally found this post!
xmllint is unbeatale when it comes to simplicity & ease of use.
Thanks again!
July 27th, 2012 at 9:57 pm
I’ve primarily been using xmllint for formatting XML documents, but had no idea you could parse XML documents and later issue XPath commands. Until now, I was pretty much using XML Copy Editor to issue XPath commands.
Thank you very much for this!!!