如何仅删除注释语法?

时间:2013-01-08 09:06:08

标签: perl

我想从XML文件中收集所有标签。如何仅删除注释语法?

XML文件:

<xml>
<contrib-group>
<contrib contrib-type="author">
<name>
<surname>Holt</surname>
<given-names> Maurice<!--<xref ref-type="fn" rid="fnI_1"><sup>1</sup></xref>--></given-names>
</name>
</contrib>
</contrib-group>
</xml>

我需要输出为:

<xml>
<contrib-group>
<contrib contrib-type="author">
<name>
<surname>Holt</surname>
<given-names> Maurice<xref ref-type="fn" rid="fnI_1"><sup>1</sup></xref></given-names>
</name>
</contrib>
</contrib-group>
</xml>

如何删除评论..不删除包含?

脚本:

#!/usr/bin/perl
use warnings;
use strict;
use XML::Twig;

open(my $output , '>', "split.xml") || die "can't open the Output $!\n";
my $xml = XML::Twig->new( twig_handlers => { xref => sub{comments => 'drop'} } );
$xml->parsefile("sample.xml");
$xml->print($output);

我不能这样做...如何在不删除包含的情况下删除<!-- -->

1 个答案:

答案 0 :(得分:6)

#!/usr/bin/perl
use warnings;
use strict;

use XML::Twig;

open my $output , '>', 'split.xml' or die "Can't open: $!\n";
my $xml = XML::Twig->new( comments      => 'process',       # Turn on comment processing
                          twig_handlers =>
                              { '#COMMENT' => \&uncomment }
                        );
$xml->parsefile('sample.xml');
$xml->print($output);

sub uncomment {
    my ($xml, $comment) = @_;
    $comment->set_outer_xml($comment->text);                # Replace the comment with its contents.
}