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.

Prasad


In reply to 'ID' generation issue and 'ID' format in XML::Twig by prasadbabu

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.