tags:

views:

33

answers:

2

I have some html that looks pretty much like this.

<p>
 <a img src="img src">
 <strong>foo</strong>
 <strong>bar</strong>
 <strong>baz</strong>
 <strong>eek</strong>
 This is the text I want to select using xpath.
</p>

How can I select only this particular text node as indicated above using xpath?

A: 

"/p/text()" xpath expression will select the text from "p" node in above XML (Posted in question).

/p/text()[normalize-space()] 

this will remove trailing spaces from string. This xpath produces exactly what you want.

There is very good tutorial at http://www.w3schools.com/xpath/

YoK
Actually /p/text() will also return 'foo', 'bar', 'baz' and 'eek'. Trying to avoid that.
Rasputin Jones
text() function should return you only text in node and not text from child nodes. I have even verified this using firefox XPath checker addon. It works!!!.Please verify and let me know how you are verifying same.
YoK
@Rasputin Jones have updated my answer. Please check.
YoK
A: 

How do I get at only this particular text element in question using xpath?

Use:

/p/text()[last()]
Dimitre Novatchev