0

私はいくつかのxml小枝を試しましたが、私が間違っていたのは完全には機能しませんでしたか?

#!/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>

実行中にいくつかのエラーが発生します

4

1 に答える 1

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"]');

したがって、少なくとも 1 回$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

したがって、6 番目の<w:tr>子は 5 人未満です。それを見てください。

于 2013-07-10T07:53:27.877 に答える