How to match the brother of a certain XML element in ruby?

I played around with nokogiri in ruby and the XML searching feature, e.g.:

a = Nokogiri.XML(open 'a.xml')
x = a.search('//div[@class="foo"]').text

which works quite nice.

But how can I specify to match the next (brother) element on the same level (and only the next)?

For example for this input:

<div>
  <div>...</div>
  <div>...</div>
  <div class="foo"></div>
  <div>EXTRACT ME</dev>
  ...
</div>

The actual input is some non-XHTML html, but so far Nokogiri.XML does not complain.

Btw, what filter syntax f.search actually expects? xpath?

Answers


Taking the hint from Brian Agnew and DevNull I guess that f.search actually expects xpath syntax and using the following-sibling predicate the following expression matches what was asked:

a = x.search('//div[@class="foo"]/following-sibling::div[1]')

I think you want XPath's following-sibling predicate.


Need Your Help

Socket.io Chat on Dreamhost: Hiding the URL

node.js socket.io dreamhost

My socket.io chat is accessible through the URL http://example.com:8080/. Dreamhost does not allow us to bind 80, so I am binding it on 8080.

xlrd library not working with xlsx files.any way to covert xlsx to xls using python?

python excel xls xlsx xlrd

I want to convert xlsx file to xls format using python. The reason is that im using xlrd library to parse xls files, but xlrd is not able to parse xlsx files.