1

我是 xml 树枝的新手,如何在 xml-twig 中的两个元素之间添加空格?

输入:

<xml>
<fig id="fig6_4">
<label><xref ref-type="page" id="page_54"/>[Figure 4]</label>
<caption>The Klein Sexual Orientation Grid</caption>
</fig>
</xml>

脚本:

$xml_twig_content = XML::Twig->new(
                                   twig_handlers => {
                                   'fig' => \&figure,
},
                                  );
$xml_twig_content->parsefile('sample.xml');

sub figure{
my ($xml_twig_content, $figure) = @_;
my @figchild = $figure->children;
foreach my $chid (@figchild){
if ($chid->name =~ /label/){
        my $t = $chid->text;
        $chid->set_inner_xml($t . ' ');
        $chid->erase;
}

输出:

<xml>
<fig id="fig6_4">
[Figure 4] <caption>The Klein Sexual Orientation Grid</caption>
</fig>
</xml>

我需要:

<xml>
    <fig id="fig6_4">
    <xref ref-type="page" id="page_54"/>[Figure 4] <caption>The Klein Sexual Orientation Grid</caption>
    </fig>
    </xml>

我怎样才能在两个元素之间插入空格.....

4

2 回答 2

2

我不清楚目标是什么——你的输出数据格式看起来不是特别理想。尽管如此,下面的示例应该足以让您上路。它解决了两点:

  1. 当前输出中缺少“外部参照”。
  2. 如何在文档中添加任意空格(基本上是 PCDATA 内容)

附带说明:我以前没有使用过 XML::Twig;如果您熟悉 XML 概念,该文档实际上非常好。

use strict;
use warnings;

use XML::Twig;

my $twig = XML::Twig->new(
    twig_handlers => {
        'fig' => \&figure
    },
    pretty_print => 'indented',
);

$twig->parse(do { local $/; <DATA> });

$twig->print;

sub figure {
    my ( $twig, $figure ) = @_;

    # Find all children of type label (would there really be more than 1??)
    foreach my $label ($figure->children('label')) {
        # Replace the label with its chidren nodes
        $label->replace_with($label->cut_children);

        # Find the caption and place 4 spaces before it
        if (my $caption = $figure->first_child('caption')) {
            my $some_whitespace = XML::Twig::Elt->new('#PCDATA' => '    ');
            $some_whitespace->paste(before => $caption);
        }
    }
}

__DATA__
<xml>
<fig id="fig6_4">
<label><xref ref-type="page" id="page_54"/>[Figure 4]</label>
<caption>The Klein Sexual Orientation Grid</caption>
</fig>
</xml>
于 2012-12-26T10:04:43.307 回答
2

我会在 上使用处理程序fig/label,因为这是唯一需要修改的元素。然后,处理程序中的代码需要为元素添加空格后缀,然后删除标记:

XML::Twig->new( twig_handlers => { 'fig/label' => sub { $_->suffix( ' ')->erase; }});
于 2012-12-26T12:27:16.813 回答