use strict; use warnings; use XML::Twig qw( ); my $fqn_dat = 'mlr.dat'; my $fqn_in = 'mlr_648.xml'; my $fqn_out = 'fixed_mlr_648.xml'; my %translations; { open(my $fh, '<', $fqn_dat) or die("Can't open \"$fqn_dat\": $!\n"); while (<$fh>) { /^(.*?)\t(.*)/ or next; $translations{$1} = $2; } } open(my $fh_in, '<', $fqn_in) or die("Can't open input file \"$fqn_in\": $!\n"); open(my $fh_out, '>', $fqn_out) or die("Can't create output file \"$fqn_out\": $!\n"); my $twig = XML::Twig->new( twig_handlers => { 'reference/title[@type="journal"]' => sub { my ($twig, $elt) = @_; my $text = $elt->text(); if ( exists( $translations{$text} ) ) { $elt->set_text( $translations{$text} ); } else { warn("No translation found for \"$text\"\n"); } }, 'reference' => sub { my ($twig, $elt) = @_; # Keep at most one <reference> in memory. $twig->flush($fh_out); }, } ); $twig->parse($fh_in); $twig->flush($fh_out);
In reply to Re: Modifying a Sepcifig node
by ikegami
in thread Modifying a Sepcifig node
by ngbabu
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |