Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XPath to select elements with no children?

<div>
<p> text 1 </p> 
<p>
    <a href="http://www.google.com">text 2</a>
</p> 
<p> text 3  </p> 
<p> text 4 </p> 
</div>

I used this: //p but this also gives me the <p> where text 2 is. I want <p>s without children. I need: text 1, text 3, text 4

like image 546
cosmos7263 Avatar asked Sep 02 '25 06:09

cosmos7263


1 Answers

Here are XPaths for various interpretations of no children...

  • No element children:

    //p[not(*)]
    
  • No text children:

    //p[not(text())]
    
  • No children of any type:

    //p[not(node())]
    

Replace p above with any other element name, or * to target elements regardless of name.

like image 143
kjhughes Avatar answered Sep 04 '25 20:09

kjhughes