tags:

views:

518

answers:

2

If I have a bunch of elements like:

<p>A paragraph <ul><li>Item 1</li><li>Apple</li><li>Orange</li></ul></p>

Is there a built in nokogiri method that would get me all, for example, p elements that contain the text "Apple"? (the example element above would match, for instance).

+1  A: 

Try using this XPath:

p = doc.xpath('//p[//*[contains(text(), "Apple")]]')
andre-r
+2  A: 

Here is an XPath that works:

require 'nokogiri'

doc = Nokogiri::HTML(DATA)
p doc.xpath('//li[contains(text(), "Apple")]')

__END__
<p>A paragraph <ul><li>Item 1</li><li>Apple</li><li>Orange</li></ul></p>

Hope that helps

Aaron Patterson