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 | |
|
Re: filtering unwanted tags based on child tag attribute value
by jdporter (Paladin) on Jan 06, 2015 at 15:17 UTC |