Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

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

by gellyfish (Monsignor)
on Jul 11, 2006 at 13:18 UTC ( [id://560447]=note: print w/replies, xml ) Need Help??


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

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

/J\

  • Comment on Re: 'ID' generation issue and 'ID' format in XML::Twig

Replies are listed 'Best First'.
Re^2: 'ID' generation issue and 'ID' format in XML::Twig
by prasadbabu (Prior) on Jul 11, 2006 at 13:29 UTC

    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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2025-07-19 12:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.