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.


In reply to filtering unwanted tags based on child tag attribute value by vikas1316

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.