Friday, August 26, 2011

[Rails] Re: Nokogiri gem issues

Keith Raymond wrote in post #1018677:
> # I tried using xpath, but was not able to get it to grab elements
> w/ 'class="my-class icon"'
> # only 'class="my-class"'
>

If you give up on installing a newer version of libxml (I've tried it in
the past and found it impossible), you can use xpath() to do what you
want:


<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>

<body>
<h1 class='editor highlight'>Hello world</h1>

<div class="editor_fruit red">Apple</div>

<div class="article_editor">
<div>Not this node</div>
<div class="hide editor">Papillon</div>
</div>

</body>

</html>

require 'nokogiri'

f = File.open('2html.htm')
doc = Nokogiri::HTML(f)

results = doc.xpath("//*[contains(concat(' ', @class, ' '), ' editor
')]").each do |el|
p [
el.attributes['class'].value,
el.children[0].text
]
end


--output:--
["editor highlight", "Hello world"]
["hide editor", "Papillon"]

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate