Hello everyone, I'm having the strangest using XML::Twig.

Here's the thing: I've got a huge XML file, you can see a snippet below:

<?xml version="1.0" encoding="utf-16"?> <!DOCTYPE tmx SYSTEM "tmx14.dtd"> <tmx version="1.4"> <header creationtool="MemoQ" creationtoolversion="6.2.21" segtype="sen +tence" adminlang="en-us" creationid="" srclang="pt-pt" o-tmf="MemoQTM +" datatype="unknown"> <prop type="defclient"> </prop> <prop type="defproject"> </prop> <prop type="defdomain"> </prop> <prop type="defsubject"> </prop> <prop type="description"> </prop> <prop type="targetlang">it</prop> <prop type="name">pt_PT-it_IT</prop> </header> <body> <tu changedate="20120625T175037Z" changeid="ana"> <prop type="client"> </prop> <prop type="project"> </prop> <prop type="domain"> </prop> <prop type="subject"> </prop> <prop type="corrected">no</prop> <prop type="aligned">yes</prop> <tuv xml:lang="pt-pt"> <prop type="x-context-pre">&lt;seg&gt;O recinto do Pavilhão Atlâ +ntico, versátil por natureza, está vocacionado para receber os mais v +ariados eventos.&lt;/seg&gt;</prop> <prop type="x-context-post">&lt;seg&gt;A Sala Atlântico, com uma + arena de 5 200 m2, abriga, com uma versatilidade única, todo o tipo +de eventos.&lt;/seg&gt;</prop> <seg>Composto por três áreas integradas, todos os espaços são fa +cilmente adaptados às características de cada evento.</seg> </tuv> <tuv xml:lang="it"> <seg>È composto da tre aree fra di esse integrate, le quali sono + tutte facilmente adattabili alle caratteristiche specifiche di ogni +evento.</seg> </tuv> </tu> <tu changedate="20130625T175037Z" changeid="ana"> <prop type="client"> </prop> <prop type="project"> </prop> <prop type="domain"> </prop> <prop type="subject"> </prop> <prop type="corrected">no</prop> <prop type="aligned">yes</prop> <tuv xml:lang="pt-pt"> <prop type="x-context-post">&lt;seg&gt;Composto por três áreas i +ntegradas, todos os espaços são facilmente adaptados às característic +as de cada evento.&lt;/seg&gt;</prop> <seg>O recinto do Pavilhão Atlântico, versátil por natureza, est +á vocacionado para receber os mais variados eventos.</seg> </tuv> <tuv xml:lang="it"> <seg>Lo spazio di pertinenza del Pavilhão Atlântico, versatile p +er natura, è adatto a ricevere gli eventi più svariati.</seg> </tuv> </tu> <tu changedate="20140625T175037Z" changeid="ana"> <prop type="client"> </prop> <prop type="project"> </prop> <prop type="domain"> </prop> <prop type="subject"> </prop> <prop type="corrected">no</prop> <prop type="aligned">yes</prop> <tuv xml:lang="pt-pt"> <prop type="x-context-post">&lt;seg&gt;Composto por três áreas i +ntegradas, todos os espaços são facilmente adaptados às característic +as de cada evento.&lt;/seg&gt;</prop> <seg>Composto por três áreas integradas, todos os espaços são fa +cilmente adaptados às características de cada evento.</seg> </tuv> <tuv xml:lang="it"> <seg>Lo spazio di pertinenza del Pavilhão Atlântico, versatile p +er natura, è adatto a ricevere gli eventi più svariati.</seg> </tuv> </tu> <tu changedate="20140625T175037Z" changeid="ana"> <prop type="client"> </prop> <prop type="project"> </prop> <prop type="domain"> </prop> <prop type="subject"> </prop> <prop type="corrected">no</prop> <prop type="aligned">yes</prop> <tuv xml:lang="pt-pt"> <prop type="x-context-post">&lt;seg&gt;Composto por três áreas i +ntegradas, todos os espaços são facilmente adaptados às característic +as de cada evento.&lt;/seg&gt;</prop> <seg>Teste</seg> </tuv> <tuv xml:lang="it"> <seg>Lo spazio di pertinenza del Pavilhão Atlântico, versatile p +er natura, è adatto a ricevere gli eventi più svariati.</seg> </tuv> </tu> <tu changedate="20110625T175037Z" changeid="ana"> <prop type="client"> </prop> <prop type="project"> </prop> <prop type="domain"> </prop> <prop type="subject"> </prop> <prop type="corrected">no</prop> <prop type="aligned">yes</prop> <tuv xml:lang="pt-pt"> <prop type="x-context-post">&lt;seg&gt;Composto por três áreas i +ntegradas, todos os espaços são facilmente adaptados às característic +as de cada evento.&lt;/seg&gt;</prop> <seg>Teste</seg> </tuv> <tuv xml:lang="it"> <seg>Lo spazio di pertinenza del Pavilhão Atlântico, versatile p +er natura, è adatto a ricevere gli eventi più svariati.</seg> </tuv> </tu> </body> </tmx>

Basically I have code that: scans the xml file (using XML::Twig), every time it finds a "tu" element it puts its "seg" element in a hash associated with its "changedate" attribute if the changedate is newer than the one in the hash or if the "seg" element doesn't exist in the hash.

Then, I do a second pass, where I compare each "seg" element of every "tu" element with the one in the hash and delete the "tu" element if it's older than the one in the hash.

But enough talking, here's the code:

use 5.010; use strict; use warnings; use XML::Twig; #use Digest::MD5 qw(md5); my $filename = 'pt_PT-it_IT-novo2.tmx'; my $out_filename = 'out.tmx'; open my $out, '>', $out_filename; binmode $out, ':encoding(UTF-8)'; print "PASSAGEM 1...\n"; my $first_pass = new XML::Twig (twig_handlers => {tu => \&first_pass}) +; $first_pass->parsefile($filename); $first_pass->purge(); print "FEITO\n"; print "\nPASSAGEM 2....\n"; my $second_pass = new XML::Twig (#twig_roots => { 'tu' => 1 }, #twig_print_outside_roots => 1, pretty_print => 'indented', twig_handlers => {tu => \&second_pass}); $second_pass->parsefile($filename); close $out; print "\nFEITO\n"; {my %hash; sub first_pass { my($twig, $tu) = @_; my $seg = $tu->first_child('tuv')->first_child('seg')->text; my $changedate = $tu->att('changedate'); #$changedate = substr $changedate, 0, 8; #my $hash = md5($seg); if ( (!(exists($hash{$seg})) ) || (($hash{$seg}) lt $changedate) ) { $hash{$seg} = $changedate; } $twig->purge(); } sub second_pass { my($twig, $tu) = @_; #print $original_tu->text; my $seg = $tu->first_child('tuv')->first_child('seg')->text; my $changedate = $tu->att('changedate'); #$changedate = substr $changedate, 0, 8; #my $hash = md5($seg); if (!(($hash{$seg}) eq $changedate)) { print "================================\n"; print "APAGADO\n"; print $seg; print "\n POIS DATA DE ORIGINAL: "; print $changedate; print " E MAIS ANTIGA QUE ENCONTRADA: "; print $hash{$seg}; print "\n=================================\n"; $tu->delete; } $twig->flush($out); } }

The problem is that I'm getting all kinds of "body" and "header" tags in the middle of the document, here's the above xml document after it's processed:

<?xml version="1.0" encoding="utf-16"?> <!DOCTYPE tmx SYSTEM "tmx14.dtd"> <tmx version="1.4"> <header adminlang="en-us" creationid="" creationtool="MemoQ" creatio +ntoolversion="6.2.21" datatype="unknown" o-tmf="MemoQTM" segtype="sen +tence" srclang="pt-pt"> <prop type="defclient"> </prop> <prop type="defproject"> </prop> <prop type="defdomain"> </prop> <prop type="defsubject"> </prop> <prop type="description"> </prop> <prop type="targetlang">it</prop> <prop type="name">pt_PT-it_IT</prop> </header> <body></body> </tmx> <?xml version="1.0" encoding="utf-16"?> <!DOCTYPE tmx SYSTEM "tmx14.dtd"> <tmx version="1.4"> <header adminlang="en-us" creationid="" creationtool="MemoQ" creatio +ntoolversion="6.2.21" datatype="unknown" o-tmf="MemoQTM" segtype="sen +tence" srclang="pt-pt"> <prop type="defclient"> </prop> <prop type="defproject"> </prop> <prop type="defdomain"> </prop> <prop type="defsubject"> </prop> <prop type="description"> </prop> <prop type="targetlang">it</prop> <prop type="name">pt_PT-it_IT</prop> </header> <body> <tu changedate="20130625T175037Z" changeid="ana"> <prop type="client"> </prop> <prop type="project"> </prop> <prop type="domain"> </prop> <prop type="subject"> </prop> <prop type="corrected">no</prop> <prop type="aligned">yes</prop> <tuv xml:lang="pt-pt"> <prop type="x-context-post">&lt;seg>Composto por três áreas in +tegradas, todos os espaços são facilmente adaptados às característica +s de cada evento.&lt;/seg></prop> <seg>O recinto do Pavilhão Atlântico, versátil por natureza, e +stá vocacionado para receber os mais variados eventos.</seg> </tuv> <tuv xml:lang="it"> <seg>Lo spazio di pertinenza del Pavilhão Atlântico, versatile + per natura, è adatto a ricevere gli eventi più svariati.</seg> </tuv> </tu> <tu changedate="20140625T175037Z" changeid="ana"> <prop type="client"> </prop> <prop type="project"> </prop> <prop type="domain"> </prop> <prop type="subject"> </prop> <prop type="corrected">no</prop> <prop type="aligned">yes</prop> <tuv xml:lang="pt-pt"> <prop type="x-context-post">&lt;seg>Composto por três áreas in +tegradas, todos os espaços são facilmente adaptados às característica +s de cada evento.&lt;/seg></prop> <seg>Composto por três áreas integradas, todos os espaços são +facilmente adaptados às características de cada evento.</seg> </tuv> <tuv xml:lang="it"> <seg>Lo spazio di pertinenza del Pavilhão Atlântico, versatile + per natura, è adatto a ricevere gli eventi più svariati.</seg> </tuv> </tu> <tu changedate="20140625T175037Z" changeid="ana"> <prop type="client"> </prop> <prop type="project"> </prop> <prop type="domain"> </prop> <prop type="subject"> </prop> <prop type="corrected">no</prop> <prop type="aligned">yes</prop> <tuv xml:lang="pt-pt"> <prop type="x-context-post">&lt;seg>Composto por três áreas in +tegradas, todos os espaços são facilmente adaptados às característica +s de cada evento.&lt;/seg></prop> <seg>Teste</seg> </tuv> <tuv xml:lang="it"> <seg>Lo spazio di pertinenza del Pavilhão Atlântico, versatile + per natura, è adatto a ricevere gli eventi più svariati.</seg> </tuv> </tu> </body> </tmx> </body> </tmx>
Dear monks, what's wrong with my code?

In reply to SOLVED-X-Post from StackOverflow->Strange problem when deleting certain elements in a XML file using XML::Twig. by Dasen

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.