【发布时间】:2011-12-20 12:40:33
【问题描述】:
当默认命名空间和带前缀命名空间解析为相同的命名空间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 命名空间,但没有属性的命名空间。
【问题讨论】:
标签: xml w3c xml-namespaces xml-attribute