打印顶部没有 XML 标题行的 XML 文档 [英] Print an XML document without the XML header line at the top
问题描述
我只是想找出如何使用 Nokogiri::XML::Document
或 Nokogiri::XML::DocumentFragment 到
to_xml
代码>.
I am just trying to find out how to to a to_xml
with a Nokogiri::XML::Document
or a Nokogiri::XML::DocumentFragment
.
或者,我想在 Nokogiri::XML::DocumentFragment
上使用 xPath.我无法确定如何做到这一点,但是我成功解析了 Nokogiri::XML::Document
.
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
.
我稍后将解析和修改的 DocumentFragment
包含到另一段 XML 中,但我真的被我认为是一些非常简单的事情所困扰.
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.
就像尝试在 doc 或 docfrag 上执行 to_xml
一样,而不包括顶部的那行 xml.为什么这么难?
Like trying to do a to_xml
on a doc or docfrag, and NOT INCLUDING that xml line at the top. Why so hard?
推荐答案
获取 Document
的 XML 的最简单方法,无需前导PI"(处理指令) 是在根元素上调用 to_s
而不是文档本身:
The simplest way to get the XML for a Document
without the leading "PI" (processing instruction) is to call to_s
on the root element instead of the document itself:
require 'nokogiri'
doc = Nokogiri.XML('<hello world="true" />')
puts doc
#=> <?xml version="1.0"?>
#=> <hello world="true"/>
puts doc.root
#=> <hello world="true"/>
在文档或构建器级别执行此操作的正确"方法是使用 SaveOptions
:
The 'correct' way to do it at the document or builder level, though, is to use SaveOptions
:
formatted_no_decl = Nokogiri::XML::Node::SaveOptions::FORMAT +
Nokogiri::XML::Node::SaveOptions::NO_DECLARATION
puts doc.to_xml( save_with:formatted_no_decl )
#=> <hello world="true"/>
# Making your code shorter, but horribly confusing for future readers
puts doc.to_xml save_with:3
#=> <hello world="true"/>
请注意,DocumentFragment
不会自动执行包括这个 PI:
Note that DocumentFragment
s do not automatically include this PI:
frag = Nokogiri::XML::DocumentFragment.parse('<hello world="true" />')
puts frag
#=> <hello world="true"/>
如果您在片段输出中看到一个 PI,则意味着在您解析它时它就在那里.
If you are seeing a PI in your fragment output, it means it was there when you parsed it.
xml = '<?xml version="1.0"?><hello world="true" />'
frag = Nokogiri::XML::DocumentFragment.parse(xml)
puts frag
#=> <?xml version="1.0"?><hello world="true"/>
如果是这样,并且您想摆脱任何 PI,您可以这样做 应该使用一点 XPath 即可:
If so, and you want to get rid of any PIs, you can do so should be able to do so with a little XPath:
frag.xpath('//processing-instruction()').remove
puts frag
...除了这似乎不起作用由于DocumentFragments 中 XPath 的奇怪之处.要解决这些错误,请改为执行此操作:
…except that this does not appear to work due to oddness with XPath in DocumentFragments. To work around these bugs do this instead:
# To remove only PIs at the root level of the fragment
frag.xpath('processing-instruction()').remove
puts frag
#=> <hello world="true"/>
# Alternatively, to remove all PIs everywhere, including inside child nodes
frag.xpath('processing-instruction()|.//processing-instruction()').remove
如果您有 Builder
对象,请执行以下任一操作:
If you have a Builder
object, do either of:
builder = Nokogiri::XML::Builder.new{ |xml| xml.hello(world:"true") }
puts builder.to_xml
#=> <?xml version="1.0"?>
#=> <hello world="true"/>
puts builder.doc.root.to_xml
#=> <hello world="true"/>
formatted_no_decl = Nokogiri::XML::Node::SaveOptions::FORMAT +
Nokogiri::XML::Node::SaveOptions::NO_DECLARATION
puts builder.to_xml save_with:formatted_no_decl
#=> <hello world="true"/>
这篇关于打印顶部没有 XML 标题行的 XML 文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!