What XPath can I use to get all text nodes after and including the first paragraph node?

nokogiri, ruby, xpath

Solution

You have to find the `<p/>` node and return all `text()` nodes, both inside and following. Depending what XPath capabilities Nokogiri has, use one of these queries:

//p[1]/(descendant::text() | following::text())

If it doesn't work, use this instead, which needs to find the first paragraph twice and can be a little bit, but probably unnoticeably, slower:

(//p[1]/descendant::text() | //p[1]/following::text())

A probably unsupported XPath 2.0 alternative would be:

//text()[//p[1] << .]

which means "all text nodes preceded by the first `<p/>` node in document".

Problem

I'm new to Nokogiri, and Ruby in general. I want to get the text of all the nodes in the document, starting from and inclusive of the first paragraph node. I tried the following with XPath but I'm getting nowhere: ``` puts page.search("//p[0]/text()[next-sibling::node()]") ``` This doesn't work. What do I have to change?

Original source