2

我将 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>

我怎么能需要作为源而不做任何更改。

4

1 回答 1

1

你的语句有问题newkeep_encodingkeep_atts_order参数被声明为twig_handlers. 我认为这不是您想要的,因为这样做的唯一一件事就是在 XML 中找到名为keep_atts_orderor的元素时立即消失。keep_encoding

我认为这更像您的想法:

my $xml_twig_content = XML::Twig->new( keep_atts_order => 1,
                                       keep_encoding => 1,
                                       pretty_print => 'indented',
                                     );
于 2012-11-08T07:05:59.233 回答