Nick
Nick

Reputation: 326

XPath to get only the text from within an element (PHP)

Apologies if this has been answered before.

I've been trying to construct an XPath expression to retrieve only the text portion from an element such as this (containing text as well as other elements)

...
  <td id="test">
     The text I want.
     <table>
        Things I do not want
        .....
        .....
  </td>

I haven't been able to find any way to separate "the text I want" from the other content inside the element.

Your help is appreciated.

Upvotes: 1

Views: 695

Answers (1)

Paul Butcher
Paul Butcher

Reputation: 6956

//td/text() should retrieve the text node children under td, when the first node rule is in effect, this will be the one you want, otherwise, filter this with square brackets if you want a particular text child

Upvotes: 6

Related Questions