问题描述
当默认命名空间和带前缀的命名空间解析为相同的命名空间 URI 时,如果两者具有相同的本地名称,为什么没有前缀的属性不等于带前缀的属性?
When the default namespace and a prefixed namespace resolves to the same namespace URI, why is an attribute with no prefix not equal to a prefixed attribute, when both have the same local name?
XML 中的命名空间"规范只是说是这样,但对于原因却很短.有人知道为什么会这样吗?
The "Namespaces in XML" specification just says it's so, but it's very short on why. Anyone knows why it's like this?
摘自 http://www.w3 上的6.3 属性的唯一性"部分.org/TR/xml-names11/#uniqAttrs :
例如,以下每个坏的空元素标签都是非法的:
<!-- 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>
但是,以下每个都是合法的,第二个是因为默认命名空间不适用于属性名称:
However, each of the following is legal, the second because the default namespace does not apply to attribute names:
<!-- 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 变得更加困难,因为解析器必须检查两个属性是否存在并选择一个.
I think this just makes it harder to parse namespaced XML, since the parser has to check the presence of both the attributes and pick one.
就我而言,我喜欢将 Atom 链接添加到我的 XML 文档中,如下所示:
For my case, I like to add Atom links to my XML documents like this:
<root xmlns="..." xmlns:atom="...">
<atom:link rel="self" type=".." href=".." />
</root>
我认为 atom:link 上的属性会继承元素命名空间.在 Java 中使用 DOM 解析 XML 报告元素的 Atom 命名空间,但没有属性的命名空间.
I would think that the attributes on atom:link would inherit the elements namespace. Parsing the XML with DOM in Java reported the Atom namespace for the element, but no namespace for the attributes.
推荐答案
简短回答:无前缀的属性总是在空的命名空间中,即它们没有命名空间.
Short answer: unprefixed attributes are always in the empty name space, i.e. they have no name space.
在示例中:
<good a="1" n1:a="2" />
第一个 a 将扩展为
the first a would expand to
{}a
而第二个将扩展为:
{http://www.w3.org}a
在您的原子示例中,所有属性都在空名称空间中.
In your atom example, all attributes are in the empty name space.
这篇关于为什么没有 XMLNS 前缀的 XML 属性不等于具有相同本地名称的前缀属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!