in reply to parsing xml
For example, a while back I figured out how to use the XPath facilities in XML::LibXML, and it was so easy and so cool, I posted some sample code for a generic tool to take an XPath expression and an XML file as command-line args, and output the portions of a given file (if any) that matched a given XPath: Re: XPath command line utility....
Consider a sample XML file like this (let's call this file "test.xml"):
If I want to extract non-empty "fail-message" elements from that sort of XML data, the XPath expression would be:<foo> <bar id="t1"> <fail-message></fail-message> </bar> <baz id="t2"> <fail-message>yar</fail-message> </baz> </foo>
To apply that expression using my "exp" tool, the (bash) command line would be://*[fail-message!='']
Since I might want to see all the markup (including attributes) for nodes that match the expression, I included a '-x' option on my "exp" script to do just that. In this case, seeing the markup would be helpful if I want to look at the empty fail-messages (by using "=" instead of "!=" in the XPath expression).exp -p "//*[fail-message!='']" test.xml
|
|---|