1

我是新手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>
4

1 回答 1

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>
于 2013-07-08T12:18:18.263 回答