如何在两个元素之间添加空格?

时间:2012-12-26 06:14:22

标签: perl xml-twig

我是xml twig的新手,如何在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>

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

2 个答案:

答案 0 :(得分:2)

我不清楚目标是什么 - 您的输出数据格式看起来特别理想。尽管如此,下面的示例应该足以让您继续前进。它涉及两点:

  1. 当前输出中缺少'xref'。
  2. 如何将任意空格(基本上是PCDATA内容)添加到文档
  3. 作为旁注:我之前没有使用过XML :: Twig;如果您对XML概念感到满意,documentation实际上非常好。

    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>
    

答案 1 :(得分:2)

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

XML::Twig->new( twig_handlers => { 'fig/label' => sub { $_->suffix( ' ')->erase; }});