Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

filtering unwanted tags based on child tag attribute value

by vikas1316 (Initiate)
on Jan 06, 2015 at 14:53 UTC ( [id://1112345]=perlquestion: print w/replies, xml ) Need Help??

vikas1316 has asked for the wisdom of the Perl Monks concerning the following question:

I am somehow stuck and banging my head. I have to delete unwanted TRADES from a huge XML file.

<TRADEEXT> <TRADE origin = 1,version =1> <EVENT externtype ='PROC'> </EVENT> </TRADE> <TRADE origin = 1,version =1> <EVENT externtype ='PROCC'> </EVENT> </TRADE> </TRADEEXT>

Now, the second TRADE is having externtype = 'PROCC' inside <EVENT> node which is not legitimate(legitimate value is 'PROC')

Hence the final output should be

<TRADEEXT> <TRADE origin = 1,version =1> <EVENT externtype ='PROC'> </EVENT> </TRADE> <TRADEEXT>

which should get pasted to new file. My code is

use strict; use warnings; use XML::Twig; my $twig = new XML::Twig( twig_handlers => { TRADE => \&TRADE } ); $twig->parsefile('1513.xml'); $twig->set_pretty_print('indented'); $twig->print_to_file('out.xml'); sub TRADE { my ( $twig, $TRADE ) = @_; foreach my $c ($TRADE->children('EVENT')) { $c->cut($TRADE) unless $c->att('eventtype') eq "PROC" ; } }

Unfortunately, it's deleting EVENT tag instead of TRADE tag. Any hint will be appreciated.

Replies are listed 'Best First'.
Re: filtering unwanted tags based on child tag attribute value
by Eily (Monsignor) on Jan 06, 2015 at 15:10 UTC

    I'm surprised this does anything at all since your input file has 'externtype' attributes and you are working on 'eventtype' (and under warnings, you have to check that an attribute is defined before comparing it with a string)

    You are using the cut method on $c, which is a child of $TRADE. $TRADE->cut; should work better.

Re: filtering unwanted tags based on child tag attribute value
by jdporter (Paladin) on Jan 06, 2015 at 15:17 UTC

    Try testing attribute 'externtype' rather than 'eventtype'?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1112345]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2024-04-24 04:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found