Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

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:
//*[fail-message!='']
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).


Comment on Re: parsing xml
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1084992]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (12)
As of 2015-07-08 07:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (96 votes), past polls