XML 命名空间冲突

XML namespaces conflicts

我想知道为什么我们必须使用对我们的组织来说是唯一的 URI 来定义我们的名称空间(就像 Java 中的包一样)。这显然是有道理的,让我们避免名称冲突。但是,this page 表示:

It is also possible (though not recommended) for the same prefix refer to different namespaces, depending on their context

所以基本上如果我有自己的 XML 文档,我自己的命名空间前缀 myNamespace 定义为 http://hisdomain.com/test,那么有人可以重新定义 'myNamespace' 前缀以指向他的own namespace http://hisdomain.com/test, even by mistake (我认为如果我定义了一个已经在文档中定义的命名空间前缀就没有警告).

即使有 URI,其他人也需要确保他们不使用我的前缀!

然后使用 URI 并必须向命名空间提供定义变得毫无用处,如果我们仅使用前缀而不必使用 xlns 属性提供定义,命名空间系统将同样有效。


如果我没理解错的话,这是不允许的:

<root xmlns:abc="mydomain.com/test" xmlns:abc="yourdomain.com/test">
...
</root>

但是这个没问题 - 您可以用您的域重新定义 abc 前缀。

<root xmlns:abc="mydomain.com/test">
    <body xmlns:abc="yourdomain.com/test">
    ...//to use my namespace here, I'd need to redefine it again to mydomain.com/test

    </body>
</root>

Even with URIs, other people need to make sure they don't use my prefix!

正确,在 XML 文档中,名称空间前缀声明不应冲突。

然而,关键是命名空间前缀本身并不重要。它是完整名称空间名称的语法糖。符合规范的 XML 处理器不会关心您或其他方使用什么名称空间前缀。命名空间前缀只是 shorthand 的便利;命名空间名称很重要。

Then using URIs and having to provide definitions to namespaces becomes useless, the namespace system would work equally well if we simply used the prefixes only without having to provide definitions using xlns attribute.

不正确。

如果组件名称必须足够长以支持命名空间名称的唯一性要求,那么命名空间系统将无法同样有效地工作。我们不喜欢额外的冗长。


更新问题的新内容:

正确,这是不允许的(类似于给定元素的两个属性不能具有相同名称的方式):

<root xmlns:abc="mydomain.com/test" xmlns:abc="yourdomain.com/test">

但这是允许的(但最好避免):

<root xmlns:abc="mydomain.com/test">
    <body xmlns:abc="yourdomain.com/test">
    ...//to use my namespace here, I'd need to redefine it again to mydomain.com/test

    </body>
</root>