0
votes

I am try to select the "string b" text node using XPath with the HtmlAgilliyPack.

<div>
  string a<br/>
  string b<br/>
  string c<br/>
</div>

I am not sure how to select the text?

This won't work //div/text(1)

Anybody has some suggestions?

2
You can't select string b using xpath.. Tell us little more about your problem. - a1204773
@Loclip of course he can. - Jens Erat
Even in different ways. ;-) - hr_117
wow I didn't know that - a1204773

2 Answers

2
votes

There are two problems with your expression:

  • XPath starts counting at 1, so you want the second text node
  • text() is a node filter which does not accept arguments. If you want to limit to the second text node, use the predicate [position() = 2] or the short version [2].

Use this expression:

//div/text()[2]

Selecting text nodes can include some hassles, chopping leading and trailing whitespace and omitting whitespace-only text nodes is implementation-dependent.

0
votes

Try:

//div/br[1]/following-sibling::text()[1]'

The direct following text after the first br.