Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

'ID' generation issue and 'ID' format in XML::Twig

by prasadbabu (Prior)
on Jul 11, 2006 at 13:10 UTC ( #560442=perlquestion: print w/ replies, xml ) Need Help??
prasadbabu has asked for the wisdom of the Perl Monks concerning the following question:

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

Comment on 'ID' generation issue and 'ID' format in XML::Twig
Select or Download Code
Re: 'ID' generation issue and 'ID' format in XML::Twig
by gellyfish (Monsignor) on Jul 11, 2006 at 13:18 UTC

    The 'id' must be unique within an XML document (assuming of course that it is an ID.)

    /J\

      gellyfish yes you are correct. I know ID must be unique. I am having xml document in which it has something like this

      .......... <aff id="aff001">...</aff> <aff id="aff002">...</aff> <aff id="aff003">...</aff> ..... <sec id="sec001">...</sec> <sec id="sec002">...</sec> <sec id="sec003">...</sec>

      I want to generate like the above?

      Prasad

        Yeah but in your original "required output" you show two elements with the same value for 'id', so forgive me for being a little confused.

        Update: right got you. You can set the id seed in the appropriate handler, you will need some extra logic if the 'a' and 'p' elements are intermixed but basically you can do something like:

        use strict; use XML::Twig; my $string =<<EOXML; <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> EOXML my $seen_a = 0; my $twig = new XML::Twig( twig_handlers => { p => sub { $_->add_id() }, a => sub { $_->set_id_seed('sec') unl +ess $seen_a++ ;$_->add_id() }, }, pretty_print => 'indented' ); $twig->set_id_seed( "fig" ); $twig->parse($string); print $twig->sprint;

        /J\

        You can (ab)use the magical increment properties of ++:

        my $c='aff001'; print $c++, "\n" for 0..5; __END__ aff001 aff002 aff003 aff004 aff005 aff006

        You should be careful with overflows, though:

        my $c='aff998'; print $c++, "\n" for 0..5; __END__ aff998 aff999 afg000 afg001 afg002 afg003

        --
        David Serrano

Re: 'ID' generation issue and 'ID' format in XML::Twig
by Ieronim (Friar) on Jul 11, 2006 at 13:49 UTC
    Keep it simple: use set_att instead of id_seed and format the id value as you want:
    #!/usr/bin/perl 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 $i; my $j; my $twig = new XML::Twig( twig_handlers => { p => sub { $_->set_att(id => sprintf "fig%02d", ++$i) }, #gene +rate id a => sub { $_->set_att(id => sprintf "sec%02d", ++$j) }, #gene +rate id }, pretty_print => 'indented' #print format ); $twig->parse($string); $string = $twig->sprint; print $string;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://560442]
Approved by marto
Front-paged by derby
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2014-08-30 01:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (291 votes), past polls