use strict; use warnings; use XML::Twig; my %self2pnam; # Self => pnam for all color elements my $twig=XML::Twig->new( # create self2pnam twig_handlers=>{ color => sub { $self2pnam{$_->att('Self')}= $_->att( 'pnam'); } }, pretty_print => 'indented', ); $twig->parse(\*DATA); # now we can process the content elements foreach my $content ($twig->root->children( 'content')) { my $box= $content->prev_elt( 'box[@type="text"]'); $content->set_tag( 'updated_tag') ->del_atts ->set_att( Color => $self2pnam{$box->att( 'frameColorID')}) ; } $twig->print; __DATA__
h3