user7254823
user7254823

Reputation:

xpath, how to get outer div based on inner?

Here is a sample of my HTML:

<div>
<a class="id2342">jack d m</a>
 x1
</div>

Basically, how would I get both "jack d m" & "x1" ?

Here is the code I have so far which only gives me "jack d m"

$row = $xpath->query('//a[@class="id2342"]')->item(0)->nodeValue;

Upvotes: 1

Views: 554

Answers (1)

kjhughes
kjhughes

Reputation: 111686

This XPath,

string(//a[@class="id2342"]/..)

will return the string value of the parent of the a element whose @class attribute is id2342:

jack d m
 x1

You can replace string() with normalize-space() to normalize the whitespace in the returned string and return

jack d m x1

as requested.

Upvotes: 1

Related Questions