murugu has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

Iam having problem with row and colspan in the cals table model in XML.

the problem is I want to convert the colspan attribute to colstart and colend attributes where colstart stands for the entry at which span starts and colend is for entry at which the spanning ends.

the problem occurs when the rowspan also present. when rowspan occurs finding the colstart gets difficult.

Input: <table> <thead> <ROW><ENTRY colspan="3"><p>a</p></ENTRY></ROW> <ROW><ENTRY colspan="2" rowspan="2"><p>b</p></ENTRY><ENTRY></ENTRY></R +OW> <ROW><ENTRY colspan="2" rowspan="2"><p></p></ENTRY></ROW><ROW><ENTRY>c +</ENTRY></ROW> </thead> </table> Output: <table> <thead> <ROW> <ENTRY colend="3" colstart="1"> <p>a</p> </ENTRY> </ROW> <ROW> <ENTRY colend="2" colstart="1"> <p>b</p> </ENTRY> <ENTRY></ENTRY> </ROW> <ROW> <ENTRY colend="2" colstart="1"> <p></p> </ENTRY> </ROW> <ROW> <ENTRY>c</ENTRY> </ROW> </thead> </table>
Just a snippet from my code:
if ($b->get_xpath("//table")) { foreach my $table ($b->get_xpath("//table")) { if ($table->get_xpath("thead")) { foreach my $thead ($table->get_xpath("thead")) { if ($thead->get_xpath("ROW")) { foreach my $row ($thead->get_xpath("ROW")) { if ($row->get_xpath("ENTRY")) { foreach my $entry ($row->get_xpath("EN +TRY")) { if ($entry->att("colspan")) { my $colspan= $entry->att("cols +pan"); my $cl=$colspan - 1; while ($cl--) { my $elt=new XML::Twig::Elt +("ENTRY","DELETE"); $elt->paste(after=>$entry) +; } } } } } if ($thead->get_xpath("ROW")) { foreach my $row ($thead->get_xpath("ROW")) { if ($row->get_xpath("ENTRY")) { foreach my $entry ($row->get_xpath("ENTRY" +)) { my $colpl=$entry->prev_siblings("ENTRY +"); if ($entry->att("rowspan")) { my $rowspan= $entry->att("rowspan" +); my $rw=$rowspan - 1; my $temp=$row; while ($rw--) { $temp=$temp->next_sibling("ROW +"); my $elt=new XML::Twig::Elt("EN +TRY","RDELETE"); my $t; if ($colpl) { $t=$colpl; my @t=$temp->children; my ($place)=$temp->get_xpa +th("ENTRY[$t]"); $elt->paste(after=>$place) +; } else { $t=1; my ($place)=$temp->get_xpa +th("ENTRY[$t]"); $elt->paste(before=>$place +); }; } } } foreach my $entry ($row->get_xpath("ENTRY" +)) { if (grep{/colspan/i}$entry->att_names) { my $p=$entry->att("colspan"); my $colpl=$entry->prev_siblings("E +NTRY"); $entry->set_atts({"colstart"=>$col +pl+1,"colend"=>$colpl+$p}); $entry->del_att("colspan"); } } foreach my $entry ($row->get_xpath("ENTRY" +)) { if ($entry->text=~/^R?DELETE$/) { $entry->delete; } } } } } } } } } }

I think that im not doing it in a better way. Is there any better way to do this or else an better algorithm may help this to solve this efficiently.

Any suggestions from monks are welcome.

Million thanks in advance.

Update:

Im using XML::Twig , what im doing is,

  1. im inserting dummy entry for the rowspanned entries and colspanned entries.
  2. then find the position.
  3. then delete the dummy entries

---Murugesan

Replies are listed 'Best First'.
Re: Finding colspan and rowspan.
by bsb (Priest) on Jul 19, 2004 at 08:20 UTC
    next if ($table->get_xpath("­thead"));
    should save you some indentation, recycle your tabs.

    No algorithm tips, sorry

    update or unless even...