{"id":107,"date":"2007-07-25T11:24:04","date_gmt":"2007-07-25T16:24:04","guid":{"rendered":"http:\/\/raffy.ch\/blog\/2007\/07\/25\/parsing-xml-on-the-command-line\/"},"modified":"2007-07-25T11:24:04","modified_gmt":"2007-07-25T16:24:04","slug":"parsing-xml-on-the-command-line","status":"publish","type":"post","link":"https:\/\/raffy.ch\/blog\/2007\/07\/25\/parsing-xml-on-the-command-line\/","title":{"rendered":"Parsing XML on the Command Line"},"content":{"rendered":"<p>I haven&#8217;t written about UNIX scripting in a while. It was yesterday in the afternoon that our QA guy came over and asked me some questions about VI. Among his problems was the &#8220;parsing of an XML&#8221; file. He wanted to extract elements from specific branches of an XML structure. I told him that VI was not XML aware. It treats XMLs just like any other text file; line by line. He was not happy with my answer and kept bugging me. Then he said: &#8220;You should write a tool called XMLgrep&#8221;. And that was it. I was pretty sure that someone had written a tool that would do exactly that.<\/p>\n<p>After 30 seconds on google, I found it: <a href=\"http:\/\/xmlstar.sourceforge.net\">XMLStarlet<\/a>. It took me about 30 minutes to get the hang of the tool, but it is really cool. It takes <a href=\"http:\/\/www.w3schools.com\/xpath\/\">XPATH<\/a> queries as an input. My knowledge of XPATH goes back to my <a href=\"http:\/\/thor.cryptojail.net\">thesis<\/a> and is a bit rusty, but I finally got it right. Here is an example of how to apply an XPATH query to an XML file:<\/p>\n<p><code>xmlstarlet sel -t -c \"\/archive\/ActiveList[@name='Public Webmail']\/description\" JSOX_ActiveLists.xml<br \/>\n<\/code><\/p>\n<p>another one:<\/p>\n<p><code>xmlstarlet sel -t -m \"\/archive\/ActiveList\" -v \"concat (@name,'<br \/>\n')\" JSOX_ActiveLists.xm<br \/>\n<\/code><\/p>\n<p>Yes, there is a newline in this command. However, it didn&#8217;t really work for me. What I wanted to do is separating the different outputs with a newline, but for some reason this didn&#8217;t work. I tried all kinds of things, but no luck. Oh well.<\/p>\n<p>Here is another link that might be useful. It&#8217;s a nice <a href=\"http:\/\/www.ibm.com\/developerworks\/library\/x-starlet.html\">tutorial<\/a> on XMLStarlet.<\/p>\n<p>[tags]xml,parsing,command line,xpath,xmlstarlet[\/tags]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I haven&#8217;t written about UNIX scripting in a while. It was yesterday in the afternoon that our QA guy came over and asked me some questions about VI. Among his problems was the &#8220;parsing of an XML&#8221; file. He wanted to extract elements from specific branches of an XML structure. I told him that VI [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-107","post","type-post","status-publish","format-standard","hentry","category-unix-scripting"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/raffy.ch\/blog\/wp-json\/wp\/v2\/posts\/107","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/raffy.ch\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/raffy.ch\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/raffy.ch\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/raffy.ch\/blog\/wp-json\/wp\/v2\/comments?post=107"}],"version-history":[{"count":0,"href":"https:\/\/raffy.ch\/blog\/wp-json\/wp\/v2\/posts\/107\/revisions"}],"wp:attachment":[{"href":"https:\/\/raffy.ch\/blog\/wp-json\/wp\/v2\/media?parent=107"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/raffy.ch\/blog\/wp-json\/wp\/v2\/categories?post=107"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/raffy.ch\/blog\/wp-json\/wp\/v2\/tags?post=107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}