Monks, I have some problem in my coding.
I have the following xml as a input and in that i just want to change the 'title' element with 'tit' everywhere except second sub level title, in the below example it is the title that contains 'second sub level', which should not be changed.
For the above job i tried using ignore_elts method but it converts all the title into tit and when i saw the output it is missing, but i just want the text back in the ouput as i shown below. How to do this?
use strict;
use XML::Twig;
my $sel = 'title';
my $ign = 'title/title/title';
my $twig = XML::Twig->new(
ignore_elts => { "$ign" => 1},
twig_handlers => { "$sel" => \&title},
pretty_print => 'indented'
);
$twig->parsefile('1.xml');
$twig->print;
sub title
{
my ($twig, $tit) = @_;
$tit->set_gi('tit');
}
input:
<?xml version="1.0"?>
<!DOCTYPE stats SYSTEM "stats.dtd">
<stats>
<title id="1">testing<name><snm>Houston</snm>, <fnm>Allan</fnm></name>
+<g>69</g><ppg>20.1</ppg><rpg>3.4</rpg><apg>2.8</apg><blk>14</blk></ti
+tle>
<title id="2">testing<name><snm>Houston</snm>, <fnm>Allan</fnm></name>
+<g>69</g><ppg>20.1</ppg><rpg>3.4</rpg><apg>2.8</apg><blk>14</blk></ti
+tle>
<title id="3">
<name1>Raja m</name1><g>49</g>
<title>
testing the sub title in title
</title>
<title>
this is pcdata in subtitle
<title>
second sub level
</title>
</title>
</title>
</stats>
output:
<?xml version="1.0"?>
<!DOCTYPE stats SYSTEM "stats.dtd">
<stats>
<tit id="1">testing<name><snm>Houston</snm>, <fnm>Allan</fnm></name>
+<g>69</g><ppg>20.1</ppg><rpg>3.4</rpg><apg>2.8</apg><blk>14</blk></ti
+t>
<tit id="2">testing<name><snm>Houston</snm>, <fnm>Allan</fnm></name>
+<g>69</g><ppg>20.1</ppg><rpg>3.4</rpg><apg>2.8</apg><blk>14</blk></ti
+t>
<tit id="3">
<name1>Raja m</name1>
<g>49</g>
<tit>
testing the sub title in title
</tit>
<tit>
this is pcdata in subtitle
</tit>
</tit>
</stats>
Expected output:
<?xml version="1.0"?>
<!DOCTYPE stats SYSTEM "stats.dtd">
<stats>
<tit id="1">testing<name><snm>Houston</snm>, <fnm>Allan</fnm></name>
+<g>69</g><ppg>20.1</ppg><rpg>3.4</rpg><apg>2.8</apg><blk>14</blk></ti
+t>
<tit id="2">testing<name><snm>Houston</snm>, <fnm>Allan</fnm></name>
+<g>69</g><ppg>20.1</ppg><rpg>3.4</rpg><apg>2.8</apg><blk>14</blk></ti
+t>
<tit id="3">
<name1>Raja m</name1>
<g>49</g>
<tit>
testing the sub title in title
</tit>
<tit>
this is pcdata in subtitle
<title>
second sub level
</title>
</tit>
</tit>
</stats>
Regards,
Anniyan
(CREATED in HELL by DEVIL to s|EVILS|GOODS|g in WORLD)