开发者

Xpath expression in Android for node with two child text nodes

I开发者_如何学Go have a sample XML (Android platform) and I wanted to know the easiest and most efficient approach to get the node value of the text node.

<div id="myid">
   <img src="..." width="1" height="2" alt="Text" />
   <p><strong>Unwanted text</strong>WANTED TEXT</p>
</div>

I can get it easily with XPath 2.0 as:

//div[@id='myid']//p/text() => WANTED TEXT

but in Android I'm getting ...

//div[@id='myid']//p/text() => Unwanted text&nbsp;WANTED TEXT

but how can i select this in Android's XPath without the unwanted text?


I am not sure Android is behaving as expected here. However, try these two statements to see if they return the desired results instead.

Get the first node which is not an element:

//div[@id='myid']//p/node()[not(self::*)]

Get the first node which is a text node:

//div[@id='myid']//p/node()[self::text()]

(Note that I haven't been able to test these on Android, so I can't say for certain what they will return).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜