0

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?

3
  • You can't select string b using xpath.. Tell us little more about your problem.
    – a1204773
    Jun 15, 2013 at 11:20
  • 1
    @Loclip of course he can.
    – Jens Erat
    Jun 15, 2013 at 11:52
  • Even in different ways. ;-)
    – hr_117
    Jun 15, 2013 at 11:53

2 Answers 2

2

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
0

Try:

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

The direct following text after the first br.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.