I am grooving on XML::Twig, but I can't seem to master the usage of the replace method. In short, I have a series of rules defined in XML, and I want "duplicate" rules to overwrite the first instance of the rule, in a way that maintains the ordering of the rules. XML::Twig's 'replace' method isn't quite working as I thought it would. My test XML file:
<?xml version="1.0" ?> <tcf> <tweak name = "T1"> <description>D1</description> </tweak> <tweak name = "T2"> <description>D2</description> </tweak> <tweak name = "T3"> <description>D3</description> </tweak> <tweak name = "T2"> <description>This should overwrite the old T2.</description> </tweak> </tcf>
My code (trimmed way down to just show the problem):
#!/usr/bin/perl use strict; use XML::Twig; # As each tweak tag is processed, this subroutine is called. Here we +will # see if any previous tweak tag had the same name element. If so, we +will # overwrite the previous tweak tag with this new tweak tag's contents, + then # delete the new tweak tag. sub pruner { my $this_tweak = $_; my $tweakname = $this_tweak->att('name'); my $exp = "/tcf/tweak[\@name=\"$tweakname\"]"; my @matches = $this_tweak->get_xpath($exp); print "TWEAK: "; $this_tweak->print; print " (Found $#matches other parsed tweaks with the same name)\n +"; # If the tweak's name is found elsewhere, replace the first # instance with the latest one, then delete the latest one. if ($#matches == 1) { print "\tReplacing\n\t\t"; @matches[0]->print; print "\n\twith\n\t\t"; $this_tweak->print; print "\n"; $this_tweak->replace(@matches[0]); } } my $twig = XML::Twig->new(expand_external_ents => 1, twig_handlers => { 'tweak' => \&pruner } ); $twig->parsefile("/home/igo/StormLogic/MythiC/Tweaker/test5.tcf"); print "TWIG:\n"; $twig->print; print "\n";
When I run that code on that XML file, here is the output:
$ ./twig_replace_test.pl TWEAK: <tweak name="T1"><description>D1</description></tweak> (Found 0 + other parsed tweaks with the same name) TWEAK: <tweak name="T2"><description>D2</description></tweak> (Found 0 + other parsed tweaks with the same name) TWEAK: <tweak name="T3"><description>D3</description></tweak> (Found 0 + other parsed tweaks with the same name) TWEAK: <tweak name="T2"><description>This should overwrite the old T2. +</description></tweak> (Found 1 other parsed tweaks with the same nam +e) Replacing <tweak name="T2"><description>D2</description></tweak> with <tweak name="T2"><description>This should overwrite the old T2 +.</description></tweak> TWIG:
It should print the Twig at the end, but it gets stuck. Am I abusing the replace method and/or putting the Twig into a broken state? This is one particular instance of the problems I've had trying this sort of thing. I wanted to understand what I'm doing wrong with this tiny example first so I can tackle the larger case on my own. Thanks!

In reply to XML::Twig replace method behaving counter-intuitively by Human

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.