Dear Monks,
I have two questions in XML::Twig.
1. In the below string i have two type of elements namely 'p' and 'a'. I want to generate 'id' for those two elements separately. I tried coding as shown below, but i am getting the ids sequencely for both 'p' and 'a' as shown below. Is it not possible to generate id for two or more elements using 'add_id' in XML::Twig? I can able to achieve that by creating two objects (commented code), but i think that is not elegant way. How to achieve that by creating single object.
Here the ids are sequence for both the elements. I want to start the n
+ew id for 'a' element again from sec1.
output i got:
-------------
<xml>
<p id="fig1">here the paragraph comes</p>
<p id="fig2">here the paragraph comes</p>
<p id="fig3">here the paragraph comes</p>
<p id="fig4">here the paragraph comes</p>
<a id="sec5">here the paragraph comes</a> #note here id is not resta
+rting
<a id="sec6">here the paragraph comes</a>
<a id="sec7">here the paragraph comes</a>
<a id="sec8">here the paragraph comes</a>
<a id="sec9">here the paragraph comes</a>
<a id="sec10">here the paragraph comes</a>
<a id="sec11">here the paragraph comes</a>
<a id="sec12">here the paragraph comes</a>
<a id="sec13">here the paragraph comes</a>
<a id="sec14">here the paragraph comes</a>
<a id="sec15">here the paragraph comes</a>
<a id="sec16">here the paragraph comes</a>
</xml>
Required Output:
----------------
<xml>
<p id="fig1">here the paragraph comes</p>
<p id="fig2">here the paragraph comes</p>
<p id="fig3">here the paragraph comes</p>
<p id="fig4">here the paragraph comes</p>
<a id="sec1">here the paragraph comes</a> #note here it restarts aga
+in
<a id="sec2">here the paragraph comes</a>
<a id="sec3">here the paragraph comes</a>
<a id="sec4">here the paragraph comes</a>
<a id="sec5">here the paragraph comes</a>
<a id="sec6">here the paragraph comes</a>
<a id="sec7">here the paragraph comes</a>
<a id="sec8">here the paragraph comes</a>
<a id="sec9">here the paragraph comes</a>
<a id="sec10">here the paragraph comes</a>
<a id="sec11">here the paragraph comes</a>
<a id="sec12">here the paragraph comes</a>
</xml>
use strict;
use XML::Twig;
my $string = '<xml>
<p>here the paragraph comes</p>
<p>here the paragraph comes</p>
<p>here the paragraph comes</p>
<p>here the paragraph comes</p>
<a>here the paragraph comes</a>
<a>here the paragraph comes</a>
<a>here the paragraph comes</a>
<a>here the paragraph comes</a>
<a>here the paragraph comes</a>
<a>here the paragraph comes</a>
<a>here the paragraph comes</a>
<a>here the paragraph comes</a>
<a>here the paragraph comes</a>
<a>here the paragraph comes</a>
<a>here the paragraph comes</a>
<a>here the paragraph comes</a>
</xml>';
my $twig = new XML::Twig(
twig_handlers => {
p => sub { $_->add_id() }
+#generate id
a => sub { $_->add_id() },
+ #generate id
},
pretty_print => 'indented' #print format
);
#my $twig1 = new XML::Twig(
#
# twig_handlers => {
# a => sub { $_->add_id
+() } #generate id
# },
# pretty_print => 'indented' #print format
#);
$twig->set_id_seed( "fig" );
$twig->parse($string);
$string = $twig->sprint;
#$twig1->set_id_seed( "sec" );
#$twig1->parse($string);
#$string = $twig1->sprint;
print $string;
2. I want to generated the id in the format id="sec01", id="sec02"...id="sec10"....I can achieve that using regex and sprintf. Also i tried $twig->set_id_seed( "fig0" ), but it is adding '0' to all 'id'. I searched full documentation but there is no methods equivalent to this. Is there a way in this module itself to achieve that.
Thanks in advance.
updated:Changed 'a' element id values from 'fig' to 'sec' to avoid confusion.