I want to get rid of white space between the <h1> and <h2> tags, and am trying to use HTML::Element->splice_content to do it. I guess I'm doing it wrong. Can someone tell me why?
use strict; use warnings; use HTML::TreeBuilder; my $html = '<h1>Blah</h1> &nbsp;<p>&nbsp;<br> <h2>Blah</h2>'; my $element_root = HTML::TreeBuilder->new_from_content($html); $element_root->dump; print "\n"; $element_root->splice_content(1,1); $element_root->dump;
Outputs:
<html> @0 (IMPLICIT) <head> @0.0 (IMPLICIT) <body> @0.1 (IMPLICIT) <h1> @0.1.0 "Blah" " á" <p> @0.1.2 "á" <br> @0.1.2.1 <h2> @0.1.3 "Blah" <html> @0 (IMPLICIT) <head> @0.0 (IMPLICIT)
UPDATE: Thanks to everyone below. Practically every suggestion was useful. In the end, I realized that the main mistake I was making was doing the splice from the root node, rather than the body. I'm still not 100% satisfied, but at least now the splice works. The code I'm using now is:
use strict; use warnings; use HTML::Treebuilder; my $html = '<h1>Blah</h1> &nbsp;<p>&nbsp;<br> <h2>Blah</h2>'; my $element_root = HTML::TreeBuilder->new_from_content($html); my $body = $element_root->look_down( _tag => "body"); $body->dump; print "\n"; $body->splice_content(1,2); $body->dump;

In reply to Help with HTML::Element->splice_content by tphyahoo

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.