in reply to Deleting nodes with HTML::TreeBuilder::XPath
Given this HTML:
Run on list items, it does delete empty items:<div> <ul> <li>one</li> <li>two</li> <li>three</li> </ul> <!-- List with 2 empty List Items --> <ul> <li></li> <li>two</li> <li></li> </ul> <!-- Empty List --> <ul> </ul> </div
Run on list elements, it deletes the list itself:sub delete_empty_list_item { my $xhtml = HTML::TreeBuilder->new; $xhtml->implicit_tags(1); $xhtml->parse_file($file); for my $list_item ($xhtml->findnodes('/html/body//div/ul/li')) { if ($list_item->is_empty) { print qq(DELETE\n); $list_item->delete(); } } print $xhtml->as_XML_indented; $xhtml->eof; } OUTPUT: <div> <ul> <li>one</li> <li>two</li> <li>three</li> </ul> <ul> <li>two</li> </ul> <ul> </ul> </div>
You could combine the two if you need to delete both empty items and empty lists.sub delete_empty_list { my $xhtml = HTML::TreeBuilder->new; $xhtml->implicit_tags(1); $xhtml->parse_file($file); for my $list ($xhtml->findnodes('/html/body//div/ul')) { if ($list->is_empty) { print qq(DELETE\n); $list->delete(); } } print $xhtml->as_XML_indented; $xhtml->eof; } OUTPUT: <div> <ul> <li>one</li> <li>two</li> <li>three</li> </ul> <ul> <li></li> <li>two</li> <li></li> </ul> </div>
|
|---|