pingu
pingu

Reputation: 111

Xpath: how do you select the second text node (specific text node)

consider a html page

<html>
apple

orange

drugs

</html>

how can you select orange using xpath ?

/html/text()[2]

doesn't work.

Upvotes: 3

Views: 2001

Answers (2)

akuhn
akuhn

Reputation: 27793

If the strings are separated with <br> it works

  doc = Nokogiri::HTML("""<html>
  apple
  <br>
  orange
  <br>
  drugs
  </html>""")
  p doc.xpath('//text()[2]') #=> orange

Upvotes: 1

Andrew Keith
Andrew Keith

Reputation: 7563

You cant do it directly by selecting. You need to call an xpath string function to cut the text() to get the string you want

substring-after(/html/text()," ") // something like this,

here is a list of string functions

Upvotes: 3

Related Questions