views:

25

answers:

2

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?

+1  A: 

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

Brian Agnew
@maxshlepzig - you'll also want to look at the predicate section in http://www.w3schools.com/xpath/xpath_syntax.asp to specify the first following-sibling.
DevNull
+1  A: 

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]')
maxschlepzig