I'm getting a lot done with HTML::TreeBuilder / HTML::Element but am now puzzled as to how to merge two anchor elements. I'd like to merge two adjacent anchor elements, if they are found in a table cell, such that the text of both is concatenated but the href from the second one is retained and the href from the first one discarded. (If there are other attributes, I don't mind a way that allows me to keep them.)

What I have so far is below. The line with N= shows that I, can in principle get the right data. But I can't get splice_content() to work in this situation. Is there a better or easier way to merge two adjacent <a> elements? Or how do I fix this way?

#!/usr/bin/perl use warnings; use strict; use HTML::TreeBuilder; my $root = HTML::TreeBuilder->new; $root->implicit_tags(0); $root->parse_file( \*DATA ); foreach my $td ( $root->look_down( _tag => 'td' ) ) { my $td2 = $td->clone(); my $merge = HTML::TreeBuilder->new; $merge->implicit_tags(0); next unless ( scalar $td2->content_list > 2 ); my @anchor=(); my $href=''; foreach my $a ( $td2->look_down( _tag => 'a' ) ) { push( @anchor, $a->as_trimmed_text() ); $href = $a->attr('href'); } print qq(N=$href\t),join(' ', @anchor),qq(\n); $merge->unshift_content( [ 'a', {'href'=>$href}, @anchor ], ); $td->splice_content( 1, 2, $merge ); $merge->delete(); } print $root->as_HTML( undef, " " ); $root->delete(); exit( 0 ); __DATA__ <table> <tr> <td><a href="/a.shtml">OK</a></td> <td>OK, too 1</td> <td><a href="/parta1.shtml">parta1</a> <a href="/parta2.shtml">parta2</a></td> </tr> <tr> <td><a href="/c.shtml">OK</a></td> <td>OK, too 2</td> <td><a href="/partb1.shtml">partb1</a> <a href="/partb2.shtml">partb2</a></td> </tr> </table>

PS the next unless clause is not fussy about what kind of elements. Is there a way to have it count just anchors?


In reply to Merging two anchors with HTML::TreeBuilder by mldvx4

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.