4

XML::Twig モジュールを使用して、XML ファイルからすべてのコメントを削除しています。サンプルファイルは-

<?xml version="1.0" encoding="UTF-8"?>
<Node_A>
node A content 1
<!-- One Line Comment A1-->
<![CDATA[this portion within the two comments is being
REMOVED which is not the intention]]>
<!-- Two Line Comment
Two Line Comment-->
node A content 3
<!-- Two Line Comment
Two Line Comment-->
<![CDATA[this portion within the two comments is being
REMOVED which is not the intention]]>
<!-- Two Line Comment
Two Line Comment-->
<![CDATA[
this portion is fine]]>

<Node_B> node B content
<Node_C> node c content
</Node_C>
<!-- One Line Comment -->
some data one
<!-- Multi  Line Comment
Line 3Comment
1Line Comment
2Line Comment
Line 5Comment
Line Comment-->
some data again two 
<!-- Multi  Line Comment
Line 3Comment
Line 5Comment
Line Comment-->

few more
</Node_B>

</Node_A>

私は次のようなスクリプトを使用しました-

#!/usr/bin/perl 

use strict;
use warnings;
use XML::Twig;
my $infile = 'demo.xml';
my $twig = XML::Twig->new (comments => 'drop', pretty_print => 'indented')->parsefile($infile);
$twig->print ();

このスクリプトは、私の意図ではない 2 つのコメント内の「CDATA」部分を削除しています。出力は次のようになります-

<?xml version="1.0" encoding="UTF-8"?>
<Node_A>
node A content 1

<![CDATA[
this portion is fine]]><Node_B> node B content
<Node_C> node c content
</Node_C>

some data one

some data again two 


few more
</Node_B></Node_A>

コメントを削除するだけで、すべての CDATA 部分とその他のものをそのまま維持するには、何を追加する必要がありますか?

前もって感謝します。

4

1 に答える 1

4

投稿した demo.xml ファイルを使用してスクリプトを実行すると、次の出力が得られます。

<?xml version="1.0" encoding="UTF-8"?>
<Node_A>
node A content 1

<![CDATA[this portion within the two comments is being
REMOVED which is not the intention]]>

node A content 3

<![CDATA[this portion within the two comments is being
REMOVED which is not the intention]]><![CDATA[
this portion is fine]]><Node_B> node B content
<Node_C> node c content
</Node_C>

some data one

some data again two


few more
</Node_B></Node_A>

これは私には問題ないように見えます。XML::Twig (またはそれが依存するXML::Parser )のバグのあるバージョンを使用していると思われます。Perl 5.14.2、XML::Twig 3.35、および XML::Parser 2.41 を使用しています。

于 2011-11-15T06:17:40.397 に答える