如何在xml twig中添加属性

时间:2013-07-08 09:58:25

标签: perl xml-twig

我是xml::Twig的新手。如何使用特定vmerge计数的vmerge代码计数添加属性和值?我怎么开始呢?在这个过程中使用了什么逻辑?

到目前为止我的脚本:

use XML::Twig;
my $XMLHandler = XML::Twig->new(TwigHandlers =>  {
    },
    pretty_print => 'indented',
);
$XMLHandler->parsefile("sample.xml");
$XMLHandler->print;

输入XML:

<xml>
    <table>
        <tr>
            <column><vmerge value="start"/></column>
            <column></column>
            <column></column>
            <column><vmerge value="start"/></column>
        </tr>
        <tr>
            <column><vmerge/></column>
            <column></column>
            <column></column>
            <column><vmerge/></column>
        </tr>
        <tr>
            <column><vmerge value="start"/></column>
            <column></column>
            <column></column>
            <column><vmerge/></column>
        </tr>
        <tr>
            <column><vmerge/></column>
            <column></column>
            <column></column>
            <column><vmerge/></column>
        </tr>
    </table>
</xml>

我需要输出如下:

<xml>
    <table>
        <tr>
            <column rowspan="2"></column>
            <column></column>
            <column></column>
            <column rowspan="4"></column>
        </tr>
        <tr>
            <column></column>
            <column></column>
            <column></column>
            <column></column>
        </tr>
        <tr>
            <column rowspan="2"></column>
            <column></column>
            <column></column>
            <column></column>
        </tr>
        <tr>
            <column></column>
            <column></column>
            <column></column>
            <column></column>
        </tr>
    </table>
</xml>

1 个答案:

答案 0 :(得分:3)

我认为这就是你想要的:

#!/usr/bin/perl

use strict;
use warnings;

use XML::Twig;
my $twig = XML::Twig->new( twig_handlers =>  { table => \&table, },
                           pretty_print => 'indented',
                         )
                    ->parse(\*DATA)
                    ->print;

sub table
  { my( $t, $table)= @_;
    foreach my $vmerge ( $table->descendants( 'vmerge[@value="start"]'))
      { my $span=1;
        my $start_column= $vmerge->parent;
        $vmerge->delete;
        my $column_index= scalar $start_column->prev_siblings( 'column');

        my $tr= $start_column->parent( 'tr')->next_sibling( 'tr');
        while( $tr)
          { my $cont_vmerge= $tr->child( $column_index)->first_child( 'vmerge[@value!="start"]');
            if( $cont_vmerge) { $span++; $cont_vmerge->delete; }
            else              { last;                          }
            $tr= $tr->next_sibling( 'tr');
          }
        $start_column->set_att( rowspan => $span);
      }
  }

__DATA__
<xml>
<table>
<tr>
<column><vmerge value="start"/></column>
<column></column>
<column></column>
<column><vmerge value="start"/></column>
</tr>
<tr>
<column><vmerge/></column>
<column></column>
<column></column>
<column><vmerge/></column>
</tr>
<tr>
<column><vmerge value="start"/></column>
<column></column>
<column></column>
<column><vmerge/></column>
</tr>
<tr>
<column><vmerge/></column>
<column></column>
<column></column>
<column><vmerge/></column>
</tr>
</table>
</xml>