How do I find all child nodes within matched elements (including text nodes)?
in jquery its pretty simple
eg
$("br").parent().contents().each(function() {
but for nokogiri, xpath,
doesn't work very well
var = doc.xpath('//br/following-sibling::text()|//br/preceding-sibling::text()').map do |fruit| fruit.to_s.strip end
+2
soaplove88
source
to share
2 answers
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>
+2
akuhn
source
to share
I'm not familiar with nokogiri, but are you trying to find all the children of any containing element <br/>
? If so, try:
//*[br]/node()
In any case, using text () will only match text nodes, not any marriage elements, which may or may not be what you want. If you only want text nodes then
//*[br]/text()
must do the trick.
0
James sulak
source
to share