Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

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

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


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

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\

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (3)
As of 2025-07-19 12:07 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.