One possibility: use the setData
method of the XML::LibXML::Text
:
#!/usr/bin/perl
use warnings;
use strict;
use XML::LibXML;
my $x_name = $ARGV[0];
my $doc = XML::LibXML->load_xml(location => $x_name, no_blanks => 1);
my $xpath_expression = '/chapter/section/para/math/text()';
my @nodes = $doc->findnodes( $xpath_expression );
for my $node (@nodes) {
my $content = $node->toString;
$content =~ s#\~rom\~#~T1rom~#sg;
$node->setData($content);
}
$doc->toFile($x_name . '.new', 1);