0
有一个XML-Twig示例显示如何将id属性与递增值一起添加到指定元素。是否有简单的方法将增加的id添加到所有元素。向所有元素添加递增的id属性
#!/bin/perl -w
#########################################################################
# #
# This example adds an id to each player #
# It uses the set_id method, by default the id attribute will be 'id' #
# #
#########################################################################
use strict;
use XML::Twig;
my $id="player001";
my $twig= new XML::Twig(twig_handlers => { player => \&player });
$twig->parsefile("nba.xml"); # process the twig
$twig->flush;
exit;
sub player
{ my($twig, $player)= @_;
$player->set_id($id++);
$twig->flush;
}
*每个*元素,或每个''元素?你的代码对于后者是正确的,尽管你可能不想覆盖现有的ID。 –
Schwern