Neta Meta
Neta Meta

Reputation: 4047

getting a parent node and its child, where the child has a certain text

I am trying to retrieve a parent node and it's child, where the child has a certain text.

this is my html:

<bod>
    <div>
         <span>something</span>
    </div>
</body>

i am using the following to catch the child :

 $the_child = $xpath->query('(//*[text()[contains(., "something")]])[1]');

but how do i also catch his first parent ?

Upvotes: 2

Views: 136

Answers (2)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243569

Pure XPath solution:

(//*[*[contains(., 'something')]])[1] | (//*/*[contains(., 'something')])[1]

Upvotes: 0

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324790

Once you have the item, just get its ->parentNode property.

Upvotes: 3

Related Questions