mldvx4 has asked for the wisdom of the Perl Monks concerning the following question:
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?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Deleting nodes with HTML::TreeBuilder::XPath
by tangent (Parson) on Jun 20, 2019 at 14:05 UTC | |
|
Re: Deleting nodes with HTML::TreeBuilder::XPath
by marto (Cardinal) on Jun 20, 2019 at 12:00 UTC | |
|
Re: Deleting nodes with HTML::TreeBuilder::XPath
by bliako (Abbot) on Jun 20, 2019 at 12:08 UTC |