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\ |