I'd like to process an XHTML document and remove certain types of nodes if they are blank. I've started trying to do that with HTML::TreeBuilder::XPath and seem to find the I want to remove. The following subroutine finds them just fine but appears to fail to delete the found empty nodes:

sub readthefile { my ($file)= (@_); print qq(File=$file\n); my $xhtml = HTML::TreeBuilder::XPath->new; $xhtml->implicit_tags(1); $xhtml->parse_file($file) or die("Could not parse '$file' : $!\n"); for my $list ($xhtml->findnodes('/html/body//div/ul/li')) { if($list->is_empty) { print qq(DELETE\n); $list->delete(); # this line does not do what I thought it + would :( } } print $xhtml->as_XML_indented; $xhtml->delete; return (1); }

As it is, the routine just prints out "DELETE" the right number of times but then at the end still prints the unmodified original XHTML. How do I excise the chosen nodes from the final output if they are empty?


In reply to Deleting nodes with HTML::TreeBuilder::XPath 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.