1

我在做 xml 转换,在这里我将根标签定义为书,但是如何在这里定义未知的根标签,我需要通用语法,

use twig;

my $story_file = XML::Twig->new(
                                   twig_handlers =>{
                                            'book' => \&book,
                                            keep_atts_order => 1,
                                            },
                                            pretty_print => 'indented',
                                  );

$story_file->parsefile('unknown.xml');
$story_file->print($out);

我试过了,

use twig;

my $story_file = XML::Twig->new(
                                   twig_handlers =>{
                                            root => \&book,
                                            keep_atts_order => 1,
                                            },
                                            pretty_print => 'indented',
                                  );

$story_file->parsefile('unknown.xml');
$story_file->print($out);

它没有用,如果有任何用于根标签的 comman syntex,请举个例子

4

1 回答 1

1

twig_handlers 的键是 XPath,所以你可以使用/*.

use XML::Twig;

sub root { print "root\n"; }

my $story_file = XML::Twig->new(
   twig_handlers =>{
      '/*' => \&root,
   },
   keep_atts_order => 1,
   pretty_print    => 'indented',
);

$story_file->parsefile('unknown.xml');
$story_file->print($out);

我安装了 3.41。

于 2012-10-22T05:08:49.057 回答