Abs
Abs

Reputation: 57926

Finding all empty DOM Nodes

Is it possible to identify a DOM node if it is empty using just xPath?

For example, any node like so:

<div></div>

I am hoping to do just a length on the returned nodes of the xPath like so:

$doc = new DOMDocument();

$doc->loadHTML($html);

$xpath = new DOMXPath($doc);

$xpath_rule = "XPATH";

$returned_nodes = $xpath->query($xpath_rule);

if($returned_nodes->length > 0){ // it is not empty

If this can't be done with xPath, how can I do this efficiently by traversing the whole DOM tree?

Only xPath 1.0 is supported by PHP5.

Upvotes: 2

Views: 314

Answers (1)

Jakub Roztocil
Jakub Roztocil

Reputation: 16232

This XPath expression matches all elements without children or containing only whitespace:

//*[not(*) and not(normalize-space(.))]

Upvotes: 3

Related Questions