命名空间 URI 究竟是什么意思? [英] What does Namespace URI exactly means?

查看:83
本文介绍了命名空间 URI 究竟是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们考虑一下这个xml数据

<h:table><h:tr><h:td>苹果</h:td><h:td>香蕉</h:td></h:tr></h:table><f:table><f:name>非洲咖啡桌</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 绑定到 http://www.w3.org/TR/html4/ NS 和另一个 NS 中的第二个 table 由于本地前缀 f 绑定到 http://www.w3schools.com/家具 NS.现在,通过构建扩展名称和比较 URI,我们能够区分这两个元素.请注意,前缀是任意的.

命名空间 URI 允许我们唯一地标识命名空间.它也称为命名空间名称.如果您使用 URL,您将利用域名系统 (DNS) 中域名的唯一性.

XML 1.0 中的命名空间 2.1 节中,我们可以读到:<块引用>

XML 命名空间由 URI 引用标识

在第 2.3 节中:

<块引用>

在确定名称是否属于给定名称空间以及两个名称是否属于同一名称空间时,会比较标识名称空间的 URI 引用.[定义:这两个URI被视为字符串,当且仅当字符串相同,即它们是相同的字符序列时,它们才是相同的.] 比较区分大小写,没有完成或撤消 % 转义.

这意味着当一个(程序)必须比较 NS URI 时,它仅作为原始字符串比较进行.URI 不必是指向 Internet 上资源的 URL(例如,可通过 HTTP 访问的网页).

请注意,URI 与本地名称(不带冒号的名称)一起使用以构建扩展名称,即一对 (URI, localName),您可以将其写为 {URI}localName(James Clark 符号),并且该对用于代替单独的本地名称.这两种表示法都不是标准的,不能在 XML 文档中使用.它只是允许使用来写下东西.命名空间本身并不存在,但建议解释了我们如何使用扩展名称以及我们如何使用名为 xmlns 的特殊属性来构建此类扩展名称.就是这样,不多也不少.

我建议您阅读 Ronald Bourret 的 Namespace Myths Exploded.

XML 1.1 中的命名空间将 URI 的使用概括为 IRI.

Let us consider this xml data

<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>

Here NURI for h : "http://www.w3.org/TR/html4/"
NURI for f : "http://www.w3schools.com/furniture"
What does the NURI(Namespace URI) exactly means?. Is it necessary?. What it actually contains?

解决方案

The use of XML namespaces in your example is necessary because you have two table elements in your document with different meaning. They are not the same. One comes from XHTML, the other from a W3 Schools vocabulary. You distinguish between them by "putting" the first table in the XHTML NS thanks to the local prefix h which is bound to the http://www.w3.org/TR/html4/ NS and the second table in the other NS thanks to the local prefix f which is bound to the http://www.w3schools.com/furniture NS. Now, by building expanded names and comparing URIs, we are able to differentiate between both elements. Note that prefixes are arbitrary.

The namespace URI is what allows us to identify uniquely the namespace. It is also called the namespace name. If you use an URL, you will take advantage of the uniqueness of domain names in the domain name system (DNS).

In Namespaces in XML 1.0 sections 2.1, we can read that:

An XML namespace is identified by a URI reference

And in section 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.

That means that when one (a program) has to compare NS URIs it does it only as raw string comparison. The URI doesn't have to be an URL pointing to a resource on the Internet (e.g. a web page accessible through HTTP).

Please note that the URI is used with a local name (name without colon) to build an expanded name i.e. a pair (URI, localName) that you may write as {URI}localName (James Clark notation) and that pair is used instead of local names alone. Both notations are not standard and cannot be used inside an XML document. It simply allows use to write things down. The namespace doesn't exist as such, but the recommendation explains how we use expanded names and how we can use a special attribute named xmlns to build such expanded names. That's it, no more, no less.

Let me suggest that your read Namespace Myths Exploded by Ronald Bourret.

Namespaces in XML 1.1 generalizes the usage of URIs to IRIs.

这篇关于命名空间 URI 究竟是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆