首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在xml twig中转换表格

如何在xml twig中转换表格
EN

Stack Overflow用户
提问于 2013-07-10 15:10:05
回答 1查看 144关注 0票数 0

我尝试了一些xml小枝,但它不能完美地工作,我错了吗?

代码语言:javascript
复制
#!/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>

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

EN

回答 1

Stack Overflow用户

发布于 2013-07-10 15:53:28

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

代码语言:javascript
复制
Can't call method "first_descendant" on an undefined value at script.pl line 23.
 at script.pl line 7.

这行代码是:

代码语言:javascript
复制
my $cont_vmerge= $tr->child( $column_index)->first_descendant( 'w:vmerge[@w:val!="restart"]');

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

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

代码语言:javascript
复制
DB<1> b 23 ! defined $tr->child( $column_index)

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

代码语言:javascript
复制
DB<3> x $column_index

这就产生了:

代码语言:javascript
复制
4

代码语言:javascript
复制
DB<4> x scalar $tr->prev_siblings( "w:tr" )

这就产生了:

代码语言:javascript
复制
5

因此,第六个<w:tr>有不到五个孩子。看看这个。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17564461

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档