3

consider a html page

<html>
apple

orange

drugs

</html>

how can you select orange using xpath ?

/html/text()[2]

doesn't work.

4

2 回答 2

3

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

于 2009-10-18T00:29:32.060 回答
1

如果字符串是分开的,<br>它可以工作

  doc = Nokogiri::HTML("""<html>
  apple
  <br>
  orange
  <br>
  drugs
  </html>""")
  p doc.xpath('//text()[2]') #=> orange
于 2009-11-24T15:43:31.160 回答