In jQuery you have the :eq selector to reduce the set of matched elements to one with a given index.
How can the same be done with Nokogiri? I searched long, but couldn’t find a way.
See this example:
require 'nokogiri'
html ='
<div>
<p>foo</p></div>
<span>
<p>bar</p>
<p>foobar</p></span>
'
doc = Nokogiri::HTML(html)
p doc.search('p:eq(0)')
# -> []
It seems that the only to reduce the set of matched nodes, is to use Ruby and an array method like this:
Neither Nokogiri’s XPath nor its CSS selectors support a functonality that is equal to jQuery’s
:eqoperator.