2012-11-08 39 views
2

我將xml轉換爲xml文件,我嘗試將文本轉換爲源文件。我目前正在使用xml :: Twig,並且我需要在xml中沒有任何更改的輸出。在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> 

我怎樣才能不需要任何改變的源代碼?

+0

爲什麼你包括XML :: XPath的嗎?你的代碼不需要它。 – mirod

回答

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', 
            ); 
+1

謝謝你它正在工作... – user1787633