I have the following example of HTML:
Foo bar
lorem
ipsum
etc
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"]