tags:

views:

213

answers:

2

in jquery its quite simple

for instance

$("br").parent().contents().each(function() {

but for nokogiri, xpath,

its not working out quite well

var = doc.xpath('//br/following-sibling::text()|//br/preceding-sibling::text()').map do |fruit| fruit.to_s.strip end
A: 

I'm not familiar with nokogiri, but are you trying to find all the children of any element that contains a <br/>? If so, then try:

//*[br]/node()

In any case, using text() will only match text nodes, and not any sibling elements, which may or may not be what you want. If you actually only want text nodes, then

//*[br]/text()

should do the trick.

James Sulak
He is looking for `//br/../text()` which matches all text nodes at the same level as the break.
Adrian
+1  A: 
require 'rubygems'
require 'nokogiri'

doc = Nokogiri::HTML(DATA.read)
fruits = doc.xpath('//br/../text()').map { |text| text.content.strip }
p fruits


__END__
<html>
<body>
  <div>
    apple<br>
    banana<br>
    cherry<br>
    orange<br>
  </div>
</body> 
Adrian