# Nokogiri: parsing tags

**URL:** <https://www.ruby-forum.com/t/nokogiri-parsing-tags/178798>\
**Category:** Rails\
**Created:** [November 11, 2009, 1:27pm UTC](https://www.ruby-forum.com/t/nokogiri-parsing-tags/178798 "2009-11-11T13:27:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![introvert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/c37758/32.png) [@introvert](https://www.ruby-forum.com/u/introvert)\
**Post date:** [November 11, 2009, 1:27pm UTC](https://www.ruby-forum.com/t/nokogiri-parsing-tags/178798/1 "2009-11-11T13:27:38Z")

</div>

Hello,

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)  
f.search(’//img’).each do |url|  
#some processing  
end  
puts f

If I try to use XML fragment:

f = Nokogiri::XML.fragment(str)  
f.search(’//img’).each do |node|  
#node.remove  
end  
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!

---

<div class="post-metadata">

**Author:** ![introvert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/c37758/32.png) [@introvert](https://www.ruby-forum.com/u/introvert)\
**Post date:** [November 12, 2009, 9:39am UTC](https://www.ruby-forum.com/t/nokogiri-parsing-tags/178798/2 "2009-11-12T09:39:43Z")

</div>

Use Nokogiri::HTML.fragment(str).

---

<div class="post-metadata">

**Author:** ![introvert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/c37758/32.png) [@introvert](https://www.ruby-forum.com/u/introvert)\
**Post date:** [November 12, 2009, 7:47pm UTC](https://www.ruby-forum.com/t/nokogiri-parsing-tags/178798/3 "2009-11-12T19:47:17Z")

</div>

G\_ F\_ wrote:

> Use Nokogiri::HTML.fragment(str).

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