How do I create Document Fragments with Nokogiri?
Posted
by viatropos
on Stack Overflow
See other posts from Stack Overflow
or by viatropos
Published on 2010-04-24T01:10:36Z
Indexed on
2010/04/24
1:13 UTC
Read the original article
Hit count: 464
I have an html document like this:
<div class="something">
<textarea name="another"/>
<div class="nested">
<label>Nested Label</label>
<input name="nested_input"/>
</div>
</div>
I have gone through and modified some of the html tree by building it into a Nokogiri::HTML::Document
like so:
html = Nokogiri::HTML(IO.read("test.html"))
html.children.each do ...
Now I want to be able to extract the nested part into a document so I can apply a stylesheet to it, or so I can manipulate it as if it were like a Rails partial. Something like this:
fragment = Nokogiri::HTML(html.xpath("//div[@class='nested']).first)
Is there a way to do that? Such a way that when I output it, it doesn't wrap it in<html>
tags and turn it into an HTML document, I just want HTML, no document.
Is this possible?
© Stack Overflow or respective owner