我有选择地修复一些元素和属性.不幸的是,我们的输入文件包含单引号和双引号属性值.此外,某些属性值包含引号(在值内).
使用XML :: Twig,我看不出如何保留属性值周围存在的任何引号.
这是示例代码:
use strict; use XML::Twig; my $file=qq(<file> <label1 attr='This "works"!' /> <label2 attr="This 'works'!" /> </file> ); my $fixes=0; # count fixes my $twig = XML::Twig->new( twig_handlers => { '[@attr]' => sub {fix_att(@_,\$fixes);} },# ... keep_atts_order => 1,keep_spaces => 1,keep_encoding => 1,); #$twig->set_quote('single'); $twig->parse($file); print $twig->sprint(); sub fix_att { my ($t,$elt,$fixes) =@_; # ... }
上面的代码返回label1的无效XML:
<label1 attr="This "works"!" />
如果我添加:
$twig->set_quote('single');
然后我们会看到label2的XML无效:
<label2 attr='This 'works'!' />
是否有保留现有报价的选项?或者有更好的方法来选择性地固定树枝?