Use Nokogiri::HTML.fragment(str).
On Nov 11, 5:27 am, "Al F."
<rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>
wrote:> 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 = ''<img
>
src="http://www.canadascapital.gc.ca/data/2/rec_imgs/5005_Pepsi_H1NB.gif"/>
> some text<a href="#">test</a> ....''
>
> 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!
> --
> Posted viahttp://www.ruby-forum.com/.