如何在Perl中修改复杂的XML文档以向文本节点添加其他标记?

时间:2011-05-12 02:32:41

标签: xml perl xml-twig

我有一个这样的XML文档:

<article>
  <author>Smith</author>
  <date>2011-10-10</date>
  <description>Article about <b>frobnitz</b>, crulps and furtikurty's. Mainly frobnitz</description>
</article>

我需要在Perl中解析它,然后在一些单词或短语周围添加新标签(例如链接到定义)。我只想标记目标词的第一个实例,并将我的搜索范围缩小到给定标记中的内容(例如仅描述标记)。

我可以使用XML::Twig进行解析,并为description标记设置“twig_handler”。但是当我调用 $ node-&gt; text 时,我会删除带有插入标签的文本。我真正要做的是遍历(非常小)树,以便保留现有标签而不会破坏。因此,最终的XML输出应如下所示:

<article>
  <author>Smith</author>
  <date>2011-10-10</date>
  <description>Article about <b><a href="dictionary.html#frobnitz">frobnitz</a></b>, <a href="dictionary.html#crulps">crulps</a> and <a href="dictionary.html#furtikurty">furtikurty</a>'s. Mainly frobnitz</description>
</article>

我在目标环境中也有XML::LibXML可用,但我不确定如何从那里开始......

到目前为止,这是我的最小测试用例。感谢任何帮助!

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

use XML::Twig;

my %dictionary = (
    frobnitz    => 'dictionary.html#frobnitz',
    crulps      => 'dictionary.html#crulps',
    furtykurty  => 'dictionary.html#furtykurty',
    );

sub markup_plain_text { 
    my ( $text ) = @_;

    foreach my $k ( keys %dictionary ) {
        $text =~ s/(^|\W)($k)(\W|$)}/$1<a href="$dictionary{$k}">$2<\/a>$3/si;
    }

    return $text;
}

sub convert {
    my( $t, $node ) = @_;
    warn "convert: TEXT=[" . $node->text . "]\n";
    $node->set_text( markup_plain_text($node->text) );
    return 1;
}

sub markup {
    my ( $text ) = @_;

    my $t = XML::Twig->new(
        twig_handlers => { description => \&convert },
        pretty_print  => 'indented',
        );
    $t->parse( $text );

    return $t->flush;
}


my $orig = <<END_XML;
<article>
    <author>Smith</author>
    <date>2011-10-10</date>
    <description>Article about <b>frobnitz</b>, crulps and furtikurty's. Mainly frobnitz's</description>
</article>
END_XML
;

markup($orig);

1 个答案:

答案 0 :(得分:3)

这是一个有点棘手的问题,但是XML :: Twig是为这种处理而设计的(我大量使用它)。因此,有一种称为mark的特定方法,它采用正则表达式并标记匹配。

在这种情况下,正则表达式可能会非常大。我使用Regexp :: Assempble来构建它,因此它得到了优化。然后另一个问题是mark不允许你使用匹配的文本来设置属性(我可能会在模块的下一个版本中使用它,这将是有用的),所以我必须首先标记,然后返回并在第二遍中设置href属性(无论如何需要第二遍来“取消链接”已经链接的单词)。

最后一句话:我几乎放弃了编写解决方案,因为您的示例数据有一些拼写错误。没有什么比得到正确的代码更糟糕了,只是为了看到测试仍然失败,因为你在代码和数据中的'定义'中使用'字典',或者'furtykurtle','furtikurty'和'furtijurty'它应该全部是同一个词。所以,在发布之前,请确保您的数据是正确的。值得庆幸的是,我正在编写代码作为测试。

#!/usr/bin/perl 

use strict;
use warnings;

use XML::Twig;
use Regexp::Assemble;

use Test::More tests => 1; 
use autodie qw(open);

my %dictionary = (
    frobnitz    => 'definitions.html#frobnitz',
    crulps      => 'definitions.html#crulps',
    furtikurty  => 'definitions.html#furtikurty',
    );

my $match_defs= Regexp::Assemble->new()
                                ->add( keys %dictionary)
                                ->anchor_word
                                ->as_string;
# I am not familiar enough with Regexp::Assemble to know a cleaner
# way to get get the capturing braces in the regexp
$match_defs= qr/($match_defs)/; 

my $in       = data_para(); 
my $expected = data_para();
my $out;
open( my $out_fh, '>', \$out);


XML::Twig->new( twig_roots => { 'description' => sub { tag_defs( @_, $out_fh, $match_defs, \%dictionary); } },
                twig_print_outside_roots => $out_fh, 
              )
         ->parse( $in);

is( $out, $expected, 'base test');
exit;

sub tag_defs
  { my( $t, $description, $out_fh, $match_defs, $dictionary)= @_;

    my @a= $description->mark( $match_defs, 'a' );

    # word => 1 when already used in this description
    # this might need to have a different scope if you need to tag
    # only the first time the word appears in a section or whatever
    my $tagged_in_description; 

    foreach my $a (@a) 
      { my $word= $a->text;
        warn "checking a: ", $a->sprint, "\n";

        if( $tagged_in_description->{$word})
          { $a->erase; } # we did not need to tag it after all
        else
          { $a->set_att( href => $dictionary->{$word}); }
        $tagged_in_description->{$word}++;
      }

    $t->flush( $out_fh); }


sub def_href
  { my( $word)= @_;
    return $dictionary{word};
  }

sub data_para
  { local $/="\n\n";
    my $para= <DATA>;
    return $para;
  }

__DATA__
<article>
  <author>Smith</author>
  <date>2011-10-10</date>
  <description>Article about <b>frobnitz</b>, crulps and furtikurty's. Mainly frobnitz</description>
</article>

<article>
  <author>Smith</author>
  <date>2011-10-10</date>
  <description>Article about <b><a href="definitions.html#frobnitz">frobnitz</a></b>, <a href="definitions.html#crulps">crulps</a> and <a href="definitions.html#furtikurty">furtikurty</a>'s. Mainly frobnitz</description>
</article>