0

Given the following html:

<html>
 <body>
  <div>
   <p>During the interim there shall be nourishment supplied</p>
  </div>
  <div>
   <a href="/interim">In the interim</a>
  </div>
 </body>
</html>

and the following xpath:

//*[contains(.,'interim')]

The elements returned by this include the HTML tag, the BODY tag, etc. So every ancestor parent is returned.

I only want the immediate container nodes, i.e. the P and the A.

geoidesic
  • 4,649
  • 3
  • 39
  • 59

1 Answers1

1

You can use

//*[contains(text(),'interim')]

i.e. replace . with text()

This will check all elements that have a text node child that contains the text interim.

The difference being that ., when used with a string related function, returns the entire text content of the element, which is why it returns every ancestor.

Keith Hall
  • 15,362
  • 3
  • 53
  • 71
  • However this only returns the A element if the html is like so: `

    During the interim there shall be nourishment supplied

    In the interim there will be an interim again
    ` Ideally, it should in this case return A and DIV
    – geoidesic Aug 19 '16 at 16:22
  • I've asked this as a new question here: http://stackoverflow.com/questions/39043885/how-can-i-select-only-the-immediate-parent-node-of-a-text-string-using-xpath-for – geoidesic Aug 19 '16 at 16:33