Nokogiri XML建筑商Newlines Riddle
为了构建大型且相当麻烦的AIXM(XML)文件,我目前正在从“构建器” GEM切换到Nokogiri的内置XML构建器。 文档fragment
s非常方便地构建用于组装最终文档的块。
大多数document fragment
s都有一个容器,一切都像魅力一样工作:
require 'nokogiri'
Nokogiri::XML::DocumentFragment.parse('').tap do |document|
Nokogiri::XML::Builder.with(document) do |builder|
builder.root do |root|
root.foo('bar')
root.fii('bir')
end
end
end.to_xml
<root>
<foo>bar</foo>
<fii>bir</fii>
</root>
不幸的是,一些docuds fragment
s仅包含一堆元素。由于某种原因,结果不再格式化了,一切都在一行上最终出现:
require 'nokogiri'
Nokogiri::XML::DocumentFragment.parse('').tap do |document|
Nokogiri::XML::Builder.with(document) do |builder|
builder.foo('bar')
builder.fii('bir')
end
end.to_xml
<foo>bar</foo><fii>bir</fii>
在每个元素之后,是否有一种新闻线,就像集装箱版本中吗?
(我未能成功地寻找将文档的一部分提取到新的文档fragment
中的方法root
。)
感谢您的帮助!
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:
require 'nokogiri'
Nokogiri::XML::DocumentFragment.parse('').tap do |document|
Nokogiri::XML::Builder.with(document) do |builder|
builder.root do |root|
root.foo('bar')
root.fii('bir')
end
end
end.to_xml
<root>
<foo>bar</foo>
<fii>bir</fii>
</root>
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:
require 'nokogiri'
Nokogiri::XML::DocumentFragment.parse('').tap do |document|
Nokogiri::XML::Builder.with(document) do |builder|
builder.foo('bar')
builder.fii('bir')
end
end.to_xml
<foo>bar</foo><fii>bir</fii>
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
让我自己回答这个问题:
从我使用libxml2的其他实现中阅读的内容,这似乎是libxml2的“功能”,而不是在诺科吉里(Nokogiri)的结尾发生。
无论如何,我正在解决这个可读性问题,现在这样做:
然后使用它:
获取:
Let me answer the question myself:
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.
In any case, I'm working around this readability issue likes so now:
Then use it:
To get: