I am just trying to find out how to to a to_xml
with a Nokogiri::XML::Document
or a Nokogiri::XML::DocumentFragment
.
Alternatively, I would like to use xPath on a Nokogiri::XML::DocumentFragment
. I was unable to ascertain how to do that, however I am successfully parsing a Nokogiri::XML::Document
.
I am later including a parsed and modified DocumentFragment
into another piece of XML, but I'm really getting bitten on what I thought would be some really simple things.
Like trying to do a to_xml
on a doc or docfrag, and NOT INCLUDING that xml line at the top. Why so hard?
Here is how to do this in rails:
skip_instruct: true
The simplest way to get the XML for a
Document
without the leading "PI" (processing instruction) is to callto_s
on the root element instead of the document itself:The 'correct' way to do it at the document or builder level, though, is to use
SaveOptions
:Note that
DocumentFragment
s do not automatically include this PI:If you are seeing a PI in your fragment output, it means it was there when you parsed it.
If so, and you want to get rid of any PIs, you
can do soshould be able to do so with a little XPath:…except that this does not appear to work due to oddness with XPath in DocumentFragments. To work around these bugs do this instead:
If you have a
Builder
object, do either of: