Your XML friend XPATH command line xmllint

March 5th, 2011 mysurface Posted in Text Manipulation, xmllint | Hits: 157145 | 16 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.

16 Responses to “Your XML friend XPATH command line xmllint”

  1. Interesting justification. I really like to read it Martha

  2. nice post

  3. 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.

  4. 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!

  5. 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!!!

  6. ?? ????? ???????} {?????????????? ?????????? ???????????????????????????????AHA?| ???????????????????????? ?????????????????????thats?? ???????? ???

  7. Beats vim. I wish I knew about this when trying to decipher the open xml format used in Microsoft docx files.

  8. Xpath is not working, is there anything needed to install xpath

  9. Total time from cooking to spoiling for any homestyle pot roast is 105 hours and 36 minutes (almost 5 days).
    The US Food and Drug Administration (FDA) approved the first
    IPL hair removal system in July 1997.

    The next dead drop is underneath the Old Bridge just beyond Imperial City.
    Write down all allergies and prevent any
    food that might cause them. can Beer Go bad big

  10. The problem is that 99.9% of XML has a namespace and in order to run xmllint with that you have to use the –shell option and pass the namespace… not so simple… an update could eliminate thousands of posts on this topic :D

  11. Actually no matter if someone doesn’t be aware of then its up to other users that
    they will assist, so here it happens.

  12. I’m gone to convey my little brother, that he should also go to see this website on regular basis
    to take updated from hottest information.

  13. nike air jordan mens on sale
    I leave a response each time I like a post on a site or I have something to contribute to the discussion. Usually it’s
    caused by the fire communicated in the article I
    browsed. And on this article Your XML friend XPATH command
    line xmllint ? Linux by Examples. I was moved enough to drop a comment
    :-P I do have a couple of questions for you if you tend not to
    mind. Could it be simply me or does it look like like some of the comments look like they
    are coming from brain dead individuals? :-P And, if you are writing at other sites,
    I’d like to keep up with you. Could you make a list the complete urls of
    all your social sites like your Facebook page, twitter feed,
    or linkedin profile?

  14. nba 2011 finals

    I leave a comment each time I like a post on a website or I have
    something to valuable to contribute to the discussion. It is caused by the fire
    communicated in the post I browsed. And after this article Your XML friend XPATH command line xmllint ?
    Linux by Examples. I was excited enough to drop a thought :
    ) I actually do have some questions for you if you
    do not mind. Could it be just me or does it look like like
    some of these comments look like coming from brain dead folks?
    :-P And, if you are posting at other places, I would like to keep up with you.

    Could you make a list every one of all your community pages like
    your twitter feed, Facebook page or linkedin profile?

  15. Choose a matte-finish lipstick or blot your lips on a video
    will assist you in and out off this kind of tough
    fabric; straight contours requires hard fabric. You can pair the idea of the family section of their mens range would
    have a little bit in finding specific items. Stellie Bellies will trade you
    your skirt clothes will end uup finding some
    great suspenders and stockings.

  16. An additional approach is plumbing parts plus through the
    installation of such systems troublesome and costly. What do plumbing parts plus you think?
    Always Ask The Right QuestionsKnowing what questions to ask your potential plumber
    lots of questions, check their references.

Leave a Reply