You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
To build large and rather cumbersome AIXM (XML) files, I'm currently switching from the "builder" gem to Nokogiri's built-in XML builder. DocumentFragments come in quite handy to build the blocks with which to assemble the final document.
Most DocumentFragments have a container with which everything works like a charm:
Unfortunately, a few DocumentFragments merely contain a bunch of elements. For some reason, the result is not formatted anymore, everything ends up on one line:
Is there a way have newslines after each element as in the containered version?
(I've unsuccessfully looked for methods to extract part of a document into a new DocumentFragment, say, using the first example to build, then extract a DocumentFragment containing only the children of root.)
Thanks for your help!
The text was updated successfully, but these errors were encountered:
From what I read about other implementations using libxml2, this appears to be a "feature" of libxml2 rather than to happen on Nokogiri's end, right?
In any case, I'm working around this readability issue likes so now:
defbuild_fragmentNokogiri::XML::DocumentFragment.parse('').tapdo |document|
Nokogiri::XML::Builder.with(document)do |builder|
yieldbuilderenddocument.elements.each{_1.add_next_sibling("\n")}# add newline between tags on top levelendend
Please see my explanation at #2521 (comment) for how libxml2 decides to format subtrees when serializing.
TL;DR, if any child of a node is a TEXT, CDATA, or ENTITY_REF node, then that node (and recursively its subtree) will be printed literally, without formatting and indentation.
I came across this bit somewhere deep down on Stackoverflow. And knowing that this decision is made by libxml2 ultimately led to this workaround. It's nicely tucked away and doesn't pollute my builder models, so works for me. 🎉
To build large and rather cumbersome AIXM (XML) files, I'm currently switching from the "builder" gem to Nokogiri's built-in XML builder.
DocumentFragment
s come in quite handy to build the blocks with which to assemble the final document.Most
DocumentFragment
s have a container with which everything works like a charm:Unfortunately, a few
DocumentFragment
s merely contain a bunch of elements. For some reason, the result is not formatted anymore, everything ends up on one line:Is there a way have newslines after each element as in the containered version?
(I've unsuccessfully looked for methods to extract part of a document into a new
DocumentFragment
, say, using the first example to build, then extract aDocumentFragment
containing only the children ofroot
.)Thanks for your help!
The text was updated successfully, but these errors were encountered: