如何在xml twig中转换表

时间:2013-07-10 07:10:05

标签: xml perl

我尝试了一些xml twig但是它没有完美地工作我错了吗?

#!/usr/bin/perl
use strict;
use warnings;
open(my $out, ">Text.xml") || die "can't open xml $!\n";
use XML::Twig;
my $twig = XML::Twig->new( twig_handlers =>  { "w:tbl" => \&table, },
                           pretty_print => 'indented',
                         )
                    ->parse(\*DATA)
                    ->print($out);
sub table
  { my( $t, $table)= @_;
    foreach my $vmerge ( $table->descendants( 'w:vmerge[@w:val="restart"]'))
      {
        my $span=1;
        my $start_column= $vmerge->parent->parent;
        $vmerge->delete;
        my $column_index = scalar $start_column->prev_siblings( 'w:tc');
        my $tr = $start_column->parent( 'w:tr')->next_sibling( 'w:tr');
        while( $tr)
          {
           my $cont_vmerge= $tr->child( $column_index)->first_descendant( 'w:vmerge[@w:val!="restart"]');
           #print $cont_vmerge;
            if($cont_vmerge) {
                    $span++;
                    $cont_vmerge->delete;
            }
            else              { last;                          }
            $tr= $tr->next_sibling( 'w:tr');
          }

        $start_column->set_att( rowspan => $span);
      }
  }
__DATA__
<xml>   
<w:tbl><w:tblPr><w:tblStyle w:val="TableGrid"/><w:tblpPr w:leftFromText="180" w:rightFromText="180" w:vertAnchor="text" w:horzAnchor="margin" w:tblpY="976"/><w:tblW w:w="0" w:type="auto"/><w:tblLook w:val="01E0"/></w:tblPr><w:tblGrid><w:gridCol w:w="1771"/><w:gridCol w:w="885"/><w:gridCol w:w="886"/><w:gridCol w:w="1771"/><w:gridCol w:w="1771"/><w:gridCol w:w="1772"/></w:tblGrid><w:tr><w:tc><w:tcPr><w:tcW w:w="1771" w:type="dxa"/><w:vmerge w:val="restart"/></w:tcPr><w:p><w:pPr><w:framePr w:hspace="180" w:wrap="around" w:vanchor="text" w:hanchor="margin" w:y="976"/></w:pPr><w:r><w:t>Testing</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="885" w:type="dxa"/></w:tcPr><w:p><w:pPr><w:framePr w:hspace="180" w:wrap="around" w:vanchor="text" w:hanchor="margin" w:y="976"/></w:pPr><w:r><w:t>A</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="886" w:type="dxa"/></w:tcPr><w:p><w:pPr><w:framePr w:hspace="180" w:wrap="around" w:vanchor="text" w:hanchor="margin" w:y="976"/></w:pPr><w:r><w:t>B</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="1771" w:type="dxa"/></w:tcPr><w:p><w:pPr><w:framePr w:hspace="180" w:wrap="around" w:vanchor="text" w:hanchor="margin" w:y="976"/></w:pPr><w:r><w:t>Simple</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="1771" w:type="dxa"/></w:tcPr><w:p><w:pPr><w:framePr w:hspace="180" w:wrap="around" w:vanchor="text" w:hanchor="margin" w:y="976"/></w:pPr><w:r><w:t>Darak</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="1772" w:type="dxa"/></w:tcPr><w:p><w:pPr><w:framePr w:hspace="180" w:wrap="around" w:vanchor="text" w:hanchor="margin" w:y="976"/></w:pPr><w:r><w:t>Uniq</w:t></w:r></w:p></w:tc></w:tr><w:tr><w:tc><w:tcPr><w:tcW w:w="1771" w:type="dxa"/><w:vmerge/></w:tcPr><w:p><w:pPr><w:framePr w:hspace="180" w:wrap="around" w:vanchor="text" w:hanchor="margin" w:y="976"/></w:pPr></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="1771" w:type="dxa"/><w:gridSpan w:val="2"/></w:tcPr><w:p><w:pPr><w:framePr w:hspace="180" w:wrap="around" w:vanchor="text" w:hanchor="margin" w:y="976"/></w:pPr></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="1771" w:type="dxa"/></w:tcPr><w:p><w:pPr><w:framePr w:hspace="180" w:wrap="around" w:vanchor="text" w:hanchor="margin" w:y="976"/></w:pPr></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="1771" w:type="dxa"/></w:tcPr><w:p><w:pPr><w:framePr w:hspace="180" w:wrap="around" w:vanchor="text" w:hanchor="margin" w:y="976"/></w:pPr></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="1772" w:type="dxa"/><w:vmerge w:val="restart"/></w:tcPr><w:p><w:pPr><w:framePr w:hspace="180" w:wrap="around" w:vanchor="text" w:hanchor="margin" w:y="976"/></w:pPr><w:r><w:t>Dife</w:t></w:r></w:p></w:tc></w:tr><w:tr><w:tc><w:tcPr><w:tcW w:w="1771" w:type="dxa"/><w:vmerge/></w:tcPr><w:p><w:pPr><w:framePr w:hspace="180" w:wrap="around" w:vanchor="text" w:hanchor="margin" w:y="976"/></w:pPr></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="1771" w:type="dxa"/><w:gridSpan w:val="2"/></w:tcPr><w:p><w:pPr><w:framePr w:hspace="180" w:wrap="around" w:vanchor="text" w:hanchor="margin" w:y="976"/></w:pPr></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="1771" w:type="dxa"/></w:tcPr><w:p><w:pPr><w:framePr w:hspace="180" w:wrap="around" w:vanchor="text" w:hanchor="margin" w:y="976"/></w:pPr></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="1771" w:type="dxa"/></w:tcPr><w:p><w:pPr><w:framePr w:hspace="180" w:wrap="around" w:vanchor="text" w:hanchor="margin" w:y="976"/></w:pPr></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="1772" w:type="dxa"/><w:vmerge/></w:tcPr><w:p><w:pPr><w:framePr w:hspace="180" w:wrap="around" w:vanchor="text" w:hanchor="margin" w:y="976"/></w:pPr></w:p></w:tc></w:tr><w:tr><w:tc><w:tcPr><w:tcW w:w="1771" w:type="dxa"/><w:vmerge/></w:tcPr><w:p><w:pPr><w:framePr w:hspace="180" w:wrap="around" w:vanchor="text" w:hanchor="margin" w:y="976"/></w:pPr></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="1771" w:type="dxa"/><w:gridSpan w:val="2"/></w:tcPr><w:p><w:pPr><w:framePr w:hspace="180" w:wrap="around" w:vanchor="text" w:hanchor="margin" w:y="976"/></w:pPr><w:r><w:t>Something</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="1771" w:type="dxa"/></w:tcPr><w:p><w:pPr><w:framePr w:hspace="180" w:wrap="around" w:vanchor="text" w:hanchor="margin" w:y="976"/></w:pPr><w:r><w:t>Something</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="1771" w:type="dxa"/></w:tcPr><w:p><w:pPr><w:framePr w:hspace="180" w:wrap="around" w:vanchor="text" w:hanchor="margin" w:y="976"/></w:pPr></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="1772" w:type="dxa"/><w:vmerge/></w:tcPr><w:p><w:pPr><w:framePr w:hspace="180" w:wrap="around" w:vanchor="text" w:hanchor="margin" w:y="976"/></w:pPr></w:p></w:tc></w:tr><w:tr><w:tc><w:tcPr><w:tcW w:w="1771" w:type="dxa"/><w:vmerge/></w:tcPr><w:p><w:pPr><w:framePr w:hspace="180" w:wrap="around" w:vanchor="text" w:hanchor="margin" w:y="976"/></w:pPr></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="1771" w:type="dxa"/><w:gridSpan w:val="2"/></w:tcPr><w:p><w:pPr><w:framePr w:hspace="180" w:wrap="around" w:vanchor="text" w:hanchor="margin" w:y="976"/></w:pPr></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="1771" w:type="dxa"/></w:tcPr><w:p><w:pPr><w:framePr w:hspace="180" w:wrap="around" w:vanchor="text" w:hanchor="margin" w:y="976"/></w:pPr></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="1771" w:type="dxa"/></w:tcPr><w:p><w:pPr><w:framePr w:hspace="180" w:wrap="around" w:vanchor="text" w:hanchor="margin" w:y="976"/></w:pPr></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="1772" w:type="dxa"/><w:vmerge/></w:tcPr><w:p><w:pPr><w:framePr w:hspace="180" w:wrap="around" w:vanchor="text" w:hanchor="margin" w:y="976"/></w:pPr></w:p></w:tc></w:tr><w:tr><w:tc><w:tcPr><w:tcW w:w="1771" w:type="dxa"/><w:vmerge/></w:tcPr><w:p><w:pPr><w:framePr w:hspace="180" w:wrap="around" w:vanchor="text" w:hanchor="margin" w:y="976"/></w:pPr></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="1771" w:type="dxa"/><w:gridSpan w:val="2"/></w:tcPr><w:p><w:pPr><w:framePr w:hspace="180" w:wrap="around" w:vanchor="text" w:hanchor="margin" w:y="976"/></w:pPr></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="3542" w:type="dxa"/><w:gridSpan w:val="2"/></w:tcPr><w:p><w:pPr><w:framePr w:hspace="180" w:wrap="around" w:vanchor="text" w:hanchor="margin" w:y="976"/></w:pPr><w:r><w:t>Retrive</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="1772" w:type="dxa"/></w:tcPr><w:p><w:pPr><w:framePr w:hspace="180" w:wrap="around" w:vanchor="text" w:hanchor="margin" w:y="976"/></w:pPr><w:r><w:t>Total</w:t></w:r></w:p></w:tc></w:tr></w:tbl>
</xml>

在执行期间出现了一些错误我在这个过程中出错了可以帮助吗?

1 个答案:

答案 0 :(得分:1)

我运行了你的代码并且它给出了以下运行时错误:

Can't call method "first_descendant" on an undefined value at script.pl line 23.
 at script.pl line 7.

该行是:

my $cont_vmerge= $tr->child( $column_index)->first_descendant( 'w:vmerge[@w:val!="restart"]');

所以至少一次$tr->child( $column_index)未定义。

我再次使用调试器集(switch -d)运行代码并添加了一个条件断点:

DB<1> b 23 ! defined $tr->child( $column_index)

然后继续并在停止时检查变量:

DB<3> x $column_index

产量:

4

DB<4> x scalar $tr->prev_siblings( "w:tr" )

产量:

5

所以,第六个<w:tr>少于五个孩子。看看它。