in reply to XML::Twig replace method behaving counter-intuitively
I know it's not really an answer to your question, but what about:
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.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>
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: XML::Twig replace method behaving counter-intuitively
by Human (Initiate) on Dec 01, 2007 at 04:55 UTC | |
by Jenda (Abbot) on Dec 01, 2007 at 18:01 UTC | |
by Human (Initiate) on Dec 03, 2007 at 20:27 UTC | |
by Jenda (Abbot) on Dec 03, 2007 at 21:38 UTC |