让我们考虑这个 xml 数据
<root xmlns:h="http://www.w3.org/TR/html4/"
xmlns:f="http://www.w3schools.com/furniture">
<h:table>
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>
<f:table>
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>
</root>
这里是 H 的 NURI:“http://www.w3.org/TR/html4/”
f 的 NURI:“http://www.w3schools.com/furniture”
NURI(Namespace URI) 到底是什么意思?有必要吗?。它实际上包含什么?
最佳答案
在您的示例中使用 XML 命名空间是必要的,因为您有两个 table
文档中具有不同含义的元素。它们是不相同的。一个来自 XHTML,另一个来自 W3 Schools 词汇表。由于本地前缀 h
,您可以通过在 XHTML NS 中“放置”第一个表来区分它们。绑定(bind)到 http://www.w3.org/TR/html4/
NS 和第二个 table
由于本地前缀 f
在另一个 NS绑定(bind)到 http://www.w3schools.com/furniture
NS。现在,通过构建扩展名称和比较 URI,我们能够区分这两个元素。请注意,前缀是任意的。
命名空间 URI 允许我们唯一标识命名空间。它也称为命名空间名称。如果您使用 URL,您将利用域名系统 (DNS) 中域名的唯一性。
在 Namespaces in XML 1.0第 2.1 节,我们可以读到:
An XML namespace is identified by a URI reference
在第 2.3 节中:
URI references identifying namespaces are compared when determining whether a name belongs to a given namespace, and whether two names belong to the same namespace. [Definition: The two URIs are treated as strings, and they are identical if and only if the strings are identical, that is, if they are the same sequence of characters. ] The comparison is case-sensitive, and no %-escaping is done or undone.
这意味着当一个(程序)必须比较 NS URI 时,它仅作为原始字符串比较进行。 URI 不必是指向 Internet 上资源的 URL(例如,可通过 HTTP 访问的网页)。
请注意,URI 与本地名称(不带冒号的名称)一起使用以构建扩展名称,即一对
(URI, localName)
你可以写成 {URI}localName
(詹姆斯克拉克符号),并且使用该对而不是单独使用本地名称。这两种表示法都不是标准的,不能在 XML 文档中使用。它只是允许用户把事情写下来。命名空间本身并不存在,但建议解释了我们如何使用扩展名称以及我们如何使用名为 xmlns 的特殊属性来构建此类扩展名称。就是这样,不多也不少。让我建议您阅读 Namespace Myths Exploded罗纳德·伯雷特。
XML 1.1 中的命名空间将 URI 的使用概括为 IRI。
关于xml - 命名空间 URI 究竟是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18974378/