How do I create Document Fragments with Nokogiri?
- by viatropos
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?