I know it's not really an answer to your question, but what about:

use XML::Rules; my $parser = XML::Rules->new( style => "filter", rules => [ _default => 'raw', tweak => sub { my ($tag, $attr, undef, undef, $parser) = @_; if (exists $parser->{pad}{$attr->{name}}) { %{$parser->{pad}{$attr->{name}}} = %$attr; return; } else { $parser->{pad}{$attr->{name}} = $attr; return [$tag => $attr]; } }, tcf => sub {delete $parser->{pad}; return $_[0] => $_[1]}, ] ); $parser->filter(\*DATA); __DATA__ <?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>
What the code does is it builds a datastructure with the contents of the <tcf> tag (the outmost tag that has a subroutine rule) storing most tags "literaly" (whatever that means) and doing something special for the <tweak> tags. For each such tag (after it's fully parsed and the inner tags are processed according to the rules) it checks whether there is a backreference in the $parser->{pad} hash to a previous instance of <tweak> with the same name. If there is it replaces the contents and attributes of that tag and returns nothing. If there is no such backreference it creates one and returns an arrayref containing the tagname and the hash with attributes and content (this causes this to be added into the _content of the parent tag and later be written into the resulting XML.

I hope the explanation makes some sense :-) The filter mode of XML::Rules and the way the built datastructures are serialized to XML is a bit hard to explain.


In reply to Re: XML::Twig replace method behaving counter-intuitively by Jenda
in thread 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.