Nokogiri XML建筑商Newlines Riddle

发布于 2025-01-25 06:07:33 字数 1200 浏览 7 评论 0原文

为了构建大型且相当麻烦的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. 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:

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 DocumentFragments 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清风挽心 2025-02-01 06:07:33

让我自己回答这个问题:

从我使用libxml2的其他实现中阅读的内容,这似乎是libxml2的“功能”,而不是在诺科吉里(Nokogiri)的结尾发生。

无论如何,我正在解决这个可读性问题,现在这样做:

def build_fragment
  Nokogiri::XML::DocumentFragment.parse('').tap do |document|
    Nokogiri::XML::Builder.with(document) do |builder|
      yield builder
    end
    document.elements.each { _1.add_next_sibling("\n") }   # add newline between tags on top level
  end
end

然后使用它:

build_fragment do |builder|
  builder.foo('bar')
  builder.fii('bir')
end

获取:

<foo>bar</foo>
<fii>bir</fii>

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:

def build_fragment
  Nokogiri::XML::DocumentFragment.parse('').tap do |document|
    Nokogiri::XML::Builder.with(document) do |builder|
      yield builder
    end
    document.elements.each { _1.add_next_sibling("\n") }   # add newline between tags on top level
  end
end

Then use it:

build_fragment do |builder|
  builder.foo('bar')
  builder.fii('bir')
end

To get:

<foo>bar</foo>
<fii>bir</fii>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文