Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select all empty elements with XPath

Tags:

xpath

Assume the following markup:

<html>
  <body>
    <p>
      <strong>  </strong>
      <strong>
      </strong>
      <strong><em>Bar</em>  </strong>
      <strong><em>  </em>  </strong>
    </p>
  </body>
</html>

How can I get the following elements with a XPath query?

<strong>  </strong>
<strong>
</strong>
<strong><em>  </em>  </strong>

I've thought it would something like //*[normalize-space(text()) = '' and not(node())] for the first both cases but it's not. And I have no clue how to catch the third case.

To be more precisely: I'm searching for all nodes which contain only white space, new lines and child nodes with the same.

like image 993
witrin Avatar asked Apr 05 '15 14:04

witrin


1 Answers

The following XPath query catches them all:

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

But not:

<strong><em>Bar</em>  </strong>
like image 109
witrin Avatar answered Sep 21 '22 08:09

witrin