This should do:
my $old_parent = $root; my $new_parent; my %modules; for my $node ($old_parent->childNodes()) { next if $node->nodeName() ne 'module'; my $mod_name = $node->getAttribute('name'); if ($mod_name eq 'all') { $new_parent = $node; } else { $old_parent->removeChild($node); $modules{$name} = $node; } } my %refcount; for my $node ($new_parent->childNodes()) { next if $node->nodeName() ne 'moduleref'; my $mod_name = $node->getAttribute('name'); my $referred_node = $modules{$name} or next; $referred_node = $referred_node->cloneNode(1) if $refcount{$mod_name}++; $new_parent->replaceChild($referred_node, $node); }
A bit or error checking is needed. You didn't specify what you wanted to do with the modules that aren't referenced. The above removes them.
In reply to Re: replace node in xml
by ikegami
in thread replace node in xml
by joeperl
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |