XPath axis, get all following nodes until

后端 未结 7 1317
醉话见心
醉话见心 2020-12-01 11:18

I have the following example of HTML:


Foo bar

lorem

ipsum

etc

相关标签:
7条回答
  • 2020-12-01 11:57

    how about matching on the second one? If you only want the top section, match the second and grab everything above it .
    doc.xpath("//h2[text()='Bar baz']/preceding-sibling::p").map { |m| m.text } => ["lorem", "ipsum", "etc"]

    or if you don't know the second one, go another level with: doc.xpath("//h2[text()='Foo bar']/following-sibling::h2/preceding-sibling::p").map { |it| it.text } => ["lorem", "ipsum", "etc"]

    0 讨论(0)
提交回复
热议问题