Jack Jack - 1 year ago 98
HTML Question

How can I select the next element using Nokogiri without calling "next_element" method?

When I run my code on a different machine I get the following error:
ruby: symbol lookup error: /usr/local/ruby/lib/ruby/gems/2.1.0/gems/nokogiri- undefined symbol: xmlNextElementSibling

When the process start I get the following warning:
WARNING: Nokogiri was built against LibXML version 2.7.8, but has dynamically loaded 2.6.30
I cannot update nokogiri at the moment so I tried to dig into the source code and the problem seems to be related to the next_element method called by Nokogiri which works on my machine but explodes on the other.
Thus my question: how can I easily select the next HTML element (in particular, the next "tr" in a table)?

This is the code I am using

def get_weight(spot)

frame = spot.at_css("table[id='1']").css("tr")[1]
starter = frame.at('tr:contains("D7r5")')
return nil if starter == nil
starter = starter.next_element
peso = []
until starter.css("td")[1].nil?
data = starter.css("td")[1].inner_html.gsub(/\t{0..100}/, "").strip
ndecimal = data.scan(/[0-9]{1,4},?.?[0-9]{0,3}/).last.gsub(",", ".")
nmeasure = data.scan(/[kK][Gg]|\s[g]\s|[g]/).last
case nmeasure
when (/[kK][Gg]/)
peso << ndecimal.to_f
when (/g|\s[g]\s|[g]/)
peso << ndecimal.to_f / 1000
starter.next_element.nil? ? break : starter = starter.next_element
return peso.max == 0 ? nil : peso.max

Answer Source

XPath equivalent for next_element is following-sibling::*[1]. Or just following-sibling::*, if the XPath will be executed using somethng like as at_xpath(), which return only the first matched node by default.