Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^2: 'ID' generation issue and 'ID' format in XML::Twig

by prasadbabu (Prior)
on Jul 11, 2006 at 13:29 UTC ( [id://560449]=note: print w/replies, xml ) Need Help??


in reply to Re: 'ID' generation issue and 'ID' format in XML::Twig
in thread 'ID' generation issue and 'ID' format in XML::Twig

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

Replies are listed 'Best First'.
Re^3: 'ID' generation issue and 'ID' format in XML::Twig
by gellyfish (Monsignor) on Jul 11, 2006 at 13:32 UTC

    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\

Re^3: 'ID' generation issue and 'ID' format in XML::Twig
by Hue-Bond (Priest) on Jul 11, 2006 at 14:55 UTC

    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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://560449]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-18 02:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found