我尝试了一些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>在执行过程中出现了一些错误,我在这个过程中出错了,有谁可以帮助我吗?
发布于 2013-07-10 15:53:28
我运行了你的代码,它给出了以下运行时错误:
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)是未定义的。
我使用调试器集(切换-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>有不到五个孩子。看看这个。
https://stackoverflow.com/questions/17564461
复制相似问题