I want to find text enclosed by tilde (~
) and prepend the text with some string, e.g. replace ~it~
with ~T1it~
in an XML file, then save the result to another file. I know how to get the text using XPath and how to replace it, but I don't know how to put the replaced text in their places and output it.
Here's my input XML:
<?xml version="1.0"?>
<chapter>
<section>
<para id="p001">this is<math>~rom~This is roman~normal~</math>para</para>
<para id="p002">this is<math>~rom~This is roman~normal~</math>para</para>
<para id="p003">this is<math>~rom~This is roman~normal~</math>para</para>
</section>
<abstract>
<para id="p004">This is <math>~rom~This is roman~normal~</math>para</para>
<para id="p005">this is<math>~rom~This is roman~normal~</math>para</para>
<para id="p006">this is<math>~rom~This is roman~normal~</math>para</para>
</abstract>
</chapter>
Here's my Perl script:
use strict;
use warnings;
use XML::LibXML;
#use XML::LibXML::Text;
use Cwd 'abs_path';
my $x_name=abs_path($ARGV[0]);
my $doc = XML::LibXML->load_xml(location => $x_name, no_blanks => 1);
my $xpath_expression='/chapter/section/para/math';
my @nodes = $doc->findnodes( $xpath_expression );
foreach my $node(@nodes){
my $content = $node->textContent;
$content=~s#\~rom\~#~T1rom~#sg;
print $content,"\n";
}
Here's my desired output:
<?xml version="1.0"?>
<chapter>
<section>
<para id="p001">this is<math>~T1rom~This is roman~normal~</math>para</para>
<para id="p002">this is<math>~T1rom~This is roman~normal~</math>para</para>
<para id="p003">this is<math>~T1rom~This is roman~normal~</math>para</para>
</section>
<abstract>
<para id="p004">This is <math>~rom~This is roman~normal~</math>para</para>
<para id="p005">this is<math>~rom~This is roman~normal~</math>para</para>
<para id="p006">this is<math>~rom~This is roman~normal~</math>para</para>
</abstract>
</chapter>