在xml文件中转义实体

时间:2012-11-08 04:56:17

标签: perl xml-twig

我将xml转换为xml文件,我尝试将文本转换为源代码。我目前正在使用xml :: Twig,我需要输出而不需要在xml中进行任何更改。

我试过:

的xml:

<book>
<book-meta>
<book-id pub-id-type="doi">98568</book-id>
<copyright-statement>Copyright &#x00A9; 1999 Relati</copyright-statement>
<imprint-text type="PublisherInfo">This edition published in the Taylor &#x0026; 2002.</imprint-text>
</book-meta>
</book>

脚本:

use strict;
use XML::Twig;
use XML::Xpath;
open(my $output , '>', "Output.xml") || die "can't open the Output $!\n";
my $xml_twig_content = XML::Twig->new(
twig_handlers => {
keep_atts_order => 1,
keep_encoding => 1,
},
pretty_print => 'indented',
);
$xml_twig_content->parsefile('sample.xml');
$xml_twig_content->print($output);

输出:

<book>
<book-meta>
<book-id pub-id-type="doi">98568</book-id>
<copyright-statement>Copyright © 1999 Relati</copyright-statement>
<imprint-text type="PublisherInfo">This edition published in the Taylor &amp; 2002.</imprint-text>
</book-meta>
</book>

我需要输出:

<book>
<book-meta>
<book-id pub-id-type="doi">98568</book-id>
<copyright-statement>Copyright &#x00A9; 1999 Relati</copyright-statement>
<imprint-text type="PublisherInfo">This edition published in the Taylor &#x0026; 2002.</imprint-text>
</book-meta>
</book>

我如何在没有任何变化的情况下作为来源。

1 个答案:

答案 0 :(得分:1)

您的new声明存在问题:keep_encodingkeep_atts_order参数声明为twig_handlers。我认为这不是你想要的,因为这样做的唯一方法就是在XML中找到名为keep_atts_orderkeep_encoding的元素后立即死亡。

我认为这更像你的想法:

my $xml_twig_content = XML::Twig->new( keep_atts_order => 1,
                                       keep_encoding => 1,
                                       pretty_print => 'indented',
                                     );