open( TRANSLATIONS, $translations_file ) or die "Could not open '$translations_file': $!"; while( ) { if( my ($word, $translation) = split /\|/ ) { push( @translations, { match => qr/\b$word\b/i, replacement => $translation, } ); } } close( TRANSLATIONS ); my $twig= new XML::Twig( twig_handlers => { 'FOO' => \&foo }, pretty_print => 'indented', output_filter => 'safe', ); $twig->parsefile( $xml_file ); $twig->print; #### sub foo { my ($t, $elt)= @_; # apply translations to the name, and text of all descendant nodes if( my $name = $elt->att('NAME') ) { $elt->set_att( 'NAME', apply_translations($name) ); } my @descendants = $elt->descendants( 'BAR' ); foreach my $descendant (@descendants) { if( my $text = $descendant->text ) { $descendant->set_text( apply_translations( $text ) ); } } } sub apply_translations { my $text = shift; foreach (@translations) { $text =~ s/$_->{match}/$_->{replacement}/g; } return $text; } #### Duration Airport #### Durée A鲯port