当默认命名空间和带前缀的命名空间解析为相同的命名空间 URI 时,为什么没有前缀的属性不等于带前缀的属性,当两者具有相同的本地名称时?
“XML 中的命名空间”规范只是说是这样,但它的原因很短。有谁知道为什么会这样?
摘自http://www.w3.org/TR/xml-names11/#uniqAttrs的“6.3 属性的唯一性”部分:
例如,每个错误的空元素标签在以下情况下都是非法的:
<!-- http://www.w3.org is bound to n1 and n2 --> <x xmlns:n1="http://www.w3.org" xmlns:n2="http://www.w3.org" > <bad a="1" a="2" /> <bad n1:a="1" n2:a="2" /> </x>
但是,以下每一项都是合法的,第二项是因为默认命名空间不适用于属性名称:
<!-- http://www.w3.org is bound to n1 and is the default --> <x xmlns:n1="http://www.w3.org" xmlns="http://www.w3.org" > <good a="1" b="2" /> <good a="1" n1:a="2" /> </x>
我认为这只会使解析命名空间 XML 变得更加困难,因为解析器必须检查两个属性的存在并选择一个。
就我而言,我喜欢将 Atom 链接添加到我的 XML 文档中,如下所示:
<root xmlns="..." xmlns:atom="...">
<atom:link rel="self" type=".." href=".." />
</root>
我认为 atom:link 上的属性将继承元素名称空间。在 Java 中使用 DOM 解析 XML 报告了元素的 Atom 命名空间,但没有属性的命名空间。