Nokogiri: parsing tags


I’m trying to use nokogiri gem to parse individual xhtml tags (not the
whole html document) and preform some processing on them.

Heres an example of a string that I want to process:

str = ’
some texttest …’

The following code will add html/body and head tags, and also document
type which I dont want (I know I could go though html->body children of
the root node but I suspect there is some better way to get expected
part with NG):

f = Nokogiri::HTML(str)’//img’).each do |url|
#some processing
puts f

If I try to use XML fragment:

f = Nokogiri::XML.fragment(str)’//img’).each do |node|
puts f

The code wont parse the html string but it will print it without adding
standard tags to it.

What am I doing wrong?

Many thanks for help!

Use Nokogiri::HTML.fragment(str).

G_ F_ wrote:

Use Nokogiri::HTML.fragment(str).

If I use this it wont parse the image tags. Any idea?