ruby - XInclude 包含元素的命名空间

标签 ruby xml nokogiri xml-namespaces xinclude

考虑这两个 XML 文档:

a.xml

<a xmlns="foo" xmlns:xi="http://www.w3.org/2001/XInclude">
  <xi:include href="b.xml" parse="xml" />
</a>

b.xml

<b>Hi Mom!</b>

<b> 应该使用什么命名空间元素在包含之后foo或者没有命名空间?


使用Nokogiri使用 XInclude 处理加载第一个文档会生成一个文本表示形式,该表示形式暗示 <b>位于父命名空间中,但检查元素后发现 <b>没有命名空间:

require 'nokogiri'

d = Nokogiri.XML(IO.read('a.xml'),&:xinclude)
puts d
#=> <?xml version="1.0"?>
#=> <a xmlns="foo" xmlns:xi="http://www.w3.org/2001/XInclude">
#=>     <b>Hi Mom!</b>
#=> </a>

p d.root.namespace
#=> #<Nokogiri::XML::Namespace:0x3fd40c517de8 href="foo">

p d.root.elements.first.namespace
#=> nil

最佳答案

What namespace should the <b> element be in after inclusion, foo or no namespace?

没有命名空间。 XInclude 在信息集级别运行并保留所包含内容的信息集属性。 infoset specification明确指出这一点

An information set corresponding to a real document will necessarily be consistent in various ways; for example the [in-scope namespaces] property of an element will be consistent with the [namespace attributes] properties of the element and its ancestors. This may not be true of an information set constructed by other means; in such a case there will be no XML document corresponding to the information set, and to serialize it will require resolution of the inconsistencies (for example, by outputting namespace declarations that correspond to the namespaces in scope).

(我的粗体)- <b>元素不在命名空间中,但其范围内命名空间信息集属性不再与解析 XInclude 所产生的“虚拟”文档中的命名空间声明一致。

关于ruby - XInclude 包含元素的命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14542030/

相关文章:

javascript - 在 NativeScript 中访问元素高度

ruby - Nokogiri::XML::Builder:需要使用字符串 "send"作为元素名称

mysql - 更改 Rails CSV 导入到 MYSQL 上的日期格式

ruby - 有没有一种 ruby​​ 方法可以使日期和时间更具可读性?

ruby - 等待使用 EventMachine 和 Ruby 纤程的异步调用

ruby - 使用 Nokogiri 抓取内容时如何排除嵌套元素?

ruby - Nokogiri 和 concat()

ruby-on-rails - Rails 在 html 文件中渲染带有空格的字符串

c# - 不使用 XmlInclude 进行序列化

html - 如何修复不兼容的 HTML 以便 Expat 解析它(htmltidy 不工作)