user1811486
user1811486

Reputation: 1314

How can convert table in xml twig

I tried some xml twig but It didn't work perfectly what I wrong?

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

some errors come during execution what I wrong in this process can any one help?

Upvotes: 0

Views: 163

Answers (1)

Birei
Birei

Reputation: 36282

I ran your code and it gives the following runtime error:

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

That line is:

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

So at least once $tr->child( $column_index) is undefined.

I ran again the code with the debugger set (switch -d) and added a conditional breakpoint:

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

Then continue and examine variables when it stops:

DB<3> x $column_index

That yields:

4

And

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

That yields:

5

So, the sixth <w:tr> has less than five childs. Look at it.

Upvotes: 1

Related Questions