Marcel
Marcel

Reputation: 233

Xpath with htmlagilitypack

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?

Upvotes: 0

Views: 178

Answers (2)

hr_117
hr_117

Reputation: 9627

Try:

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

The direct following text after the first br.

Upvotes: 0

Jens Erat
Jens Erat

Reputation: 38732

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.

Upvotes: 2

Related Questions