📄 xml namespace.htm
字号:
<html>
<head><title>XML中的Namespaces</title><link rel="stylesheet" href="../../Global.css" tppabs="http://www.xml.org.cn:8188/Global.css"></head>
<body LINK="#0000ff">
<table width="24%" border="0" height="30" cellpadding="0" cellspacing="3">
<tr>
<td width="50%">
<p align="left"><a href="../resource.htm" tppabs="http://www.xml.org.cn:8188/resource/resource.htm" target="_parent">返回资源 </a></p>
</td>
<td width="50%">
<p align="left"><a href="../../default.htm" tppabs="http://www.xml.org.cn:8188/default.htm" target="_parent">返回首页</a> </p>
</td>
</tr>
</table>
<table width="35%" border="0" height="69" cellpadding="0" cellspacing="3" align="center">
<tr>
<td width="50%" height="62">
<h2 align="center">XML中的Namespaces</h2>
<p align="center">作者:<a href="mailto:xYuHua@hotmail.com">郁桦</a><font face="宋体" lang="ZH-CN" size=5>
</font></p>
<b></b></td>
</tr>
</table>
<h4 ALIGN="CENTER"> </h4>
<table width="85%" border="0" align="center">
<tr>
<td height="447"><span class="normal105">在XML中,用户可以自己定义tag和元素。但我们可以很容易想到,如果把多个XML文件合并为一个时,就很可能出现冲突。Namespaces(名域)就是为了解决这个问题的。对XML
namespace严格的定义是:namespace是用URI加以区别的,在XML文件的元素和属性中出现的所有名称的集合。有了namespace,用户就可以保证在他的文件中使用的名称是独一无二的。
在没有namespace的XML 1.0文件中,元素和属性中出现的名称无异于一族没有结构的字符。我们称它们为local names(当地名称)。当地名称在网络上是极不合适的。可以想象,网上会有成千上万的人使用同一个名称,而它们却代表了不同的意义。
<br>
Namespace通过URI区别同名的标识。我们已经完全可以相信不会出现冲突了,因为URI是独立的。<br>
<br>
Namespace的声明要用到前缀xmlns 。声明的namespace位于指定的URI,同样的,它也有个名字,我们称这个名字为:namespace名。Namespace名必须独有而一致。有它修饰过的元素就莫认为它由指定URI处的namespace约束。<br>
<br>
让我们有一些例子来看一下几种不同namespace的声明。<br>
例一:<br>
<x xmlns:edi='http://ecommerce.org/schema'> <br>
<!-- the "edi" prefix is bound to http://ecommerce.org/schema for the
"x" element and contents --> <br>
</x> <br>
x是元素的标识tag名称。<br>
Xmlns指出一个namespace在这个元素中被用到。如果不特殊说明,元素内的标识都要收它的约束。<br>
HTTP的地址表明了特定的schema namespace位置。Edi是在当地,这个namespace的名称。<br>
例二:<br>
<x xmlns:edi='http://ecommerce.org/schema'> <br>
<!-- the 'price' element's namespace is http://ecommerce.org/schema -->
<br>
<edi:price units='Euro'>32.18</edi:price> <br>
这个例子给我们展示了edi在元素内部的作用。<br>
例三:<br>
<?xml version="1.0"?><br>
<!-- all elements here are explicitly in the HTML namespace --><br>
<html:html xmlns:html='http://www.w3.org/TR/REC-html40'><br>
<html:head> <br>
<html:title>Frobnostication</html:title><br>
</html:head> <br>
<html:body> <br>
<html:p> Moved to <br>
<html:a href='http://frob.com'> here.</html:a><br>
</html:p> <br>
</html:body> <br>
</html:html> <br>
第三行中html:html说明这个元素的tag名是html,并且,html已在当地的namespace中说明过了。<br>
例四:<br>
<?xml version="1.0"?><br>
<!-- initially, the default namespace is "books" --> <br>
<book xmlns='urn:loc.gov:books' xmlns:isbn='urn:ISBN:0-395-36341-6'><br>
<title>Cheaper by the Dozen</title><br>
<isbn:number>1568491379</isbn:number><br>
<notes> <br>
<!-- make HTML the default namespace for some commentary --> <br>
<p xmlns='urn:w3-org-ns:HTML'> <br>
This is a <i>funny</i>book!<br>
</p> <br>
</notes><br>
</book><br>
第三行中book xmlns='urn:loc.gov:books'说明,如果没有特别说明,在book元素中的标准namespace在urn:loc.gov:books
。所有的tag标识都被默认在这个namespace中。</span><br>
<hr size="1">
<div align="center">
<p><br>
<a href="../resource.htm" tppabs="http://www.xml.org.cn:8188/resource/resource.htm" target="_parent">返回资源 </a></p>
</div>
</td>
</tr>
<tr align="center">
<td><font color="#FF0033" size="-1">本站点所刊文章版权,均归本站点所有。署名作者拥有其著作权。<br>
未经</font><font color="#FF0033"><a href="mailto:xmlteam@egroups.com"><font color="#6666FF" size="-1">允许</font></a></font><font color="#FF0033" size="-1">,不得随意转载,违者必究!!</font></td>
</tr>
<tr>
<td> </td>
</tr>
</table>
<p ALIGN="JUSTIFY"> </p>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -