use strict; use warnings; use XML::Twig; my $org = < Note VII Title Page Note VII Preface Copyright Page XML my $mod = < Note VII Title Page Copyright Page Preface XML my $twig = XML::Twig->new (twig_roots => {'head' => \&fetchNode,}); my %subs; $twig->parse ($mod); $twig->purge (); $twig = XML::Twig->new ( twig_roots => {'head' => \&editNode,}, twig_print_outside_roots => 1 ); $twig->parse ($org); $twig->flush (); sub fetchNode { my ($t, $elt) = @_; my $text = $elt->text (); $subs{$text} = $elt; $t->purge (); } sub editNode { my ($t, $elt) = @_; my $text = $elt->text (); $subs{$text}->replace ($elt); $t->flush (); } #### Note VII Title Page Note VII Preface Copyright Page