Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re: parsing xml

by graff (Chancellor)
on May 05, 2014 at 03:56 UTC ( #1084992=note: print w/replies, xml ) Need Help??

in reply to parsing xml

Welcome to the Monastery. If you want to be able to find out which files have non-empty "fail-message" elements, so that you can see how many different values there are for fail messages, you probably want to use XPath expressions (which you can look up elsewhere - it's not strictly a Perl thing).

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"):

<foo> <bar id="t1"> <fail-message></fail-message> </bar> <baz id="t2"> <fail-message>yar</fail-message> </baz> </foo>
If I want to extract non-empty "fail-message" elements from that sort of XML data, the XPath expression would be:
To apply that expression using my "exp" tool, the (bash) command line would be:
exp -p "//*[fail-message!='']" test.xml
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).

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1084992]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2018-05-21 19:50 GMT
Find Nodes?
    Voting Booth?