XML 命名空间

XML Namespaces provide a method to avoid element name conflicts.
XML命名空间提供了避免元素名称矛盾的方法


Name Conflicts
名称矛盾

Since element names in XML are not predefined, a name conflict will occur when two different documents use the same element names.
因为XML的元素名称没有预先定义,如果两份不同文档的元素名称相同,就会产生名称矛盾

This XML document carries information in a table:
这份XML文档含有表格里的信息:

<table>
   <tr>
   <td>Apples</td>

   <td>Bananas</td>
   </tr>
</table>

This XML document carries information about a table (a piece of furniture):
这份XML文档含有关于一张桌子的信息(一件家具):

<table>
   <name>African Coffee Table</name>

   <width>80</width>
   <length>120</length>
</table>

If these two XML documents were added together, there would be an element name conflict because both documents contain a <table> element with different content and definition.
如果两份XML文档合起来,会出现元素名称矛盾,因为两份文件都有一个<table>元素,但其所含内容和定义是不一样的。


Solving Name Conflicts Using a Prefix
用前缀法解决命名矛盾问题

This XML document carries information in a table:
这份XML文档含有表格里的信息:

<h:table>
   <h:tr>
   <h:td>Apples</h:td>
   <h:td>Bananas</h:td>

   </h:tr>
</h:table>

This XML document carries information about a piece of furniture:
这份XML文档含有关于一张桌子的信息(一件家具):

<f:table>
   <f:name>African Coffee Table</f:name>
   <f:width>80</f:width>

   <f:length>120</f:length>
</f:table>

Now there will be no name conflict because the two documents use a different name for their <table> element (<h:table> and <f:table>).
现在就不会有命名矛盾了,因为两分文档对他们的<table>元素用了不同的名称(<h:table> 和<f:table>).

By using a prefix, we have created two different types of <table> elements.
通过使用前缀,我们创建了两种不同类型的<table>元素


Using Namespaces
用命名空间

This XML document carries information in a table:
这份XML文档含有表格里的信息:

<h:table xmlns:h="http://www.w3.org/TR/html4/">
   <h:tr>

   <h:td>Apples</h:td>
   <h:td>Bananas</h:td>
   </h:tr>
</h:table>

This XML document carries information about a piece of furniture:
这份XML文档含有关于一张桌子的信息(一件家具)

<f:table xmlns:f="http://www.w3schools.com/furniture">
   <f:name>African Coffee Table</f:name>
   <f:width>80</f:width>
   <f:length>120</f:length>

</f:table>

Instead of using only prefixes, we have added an xmlns attribute to the <table> tag to give the prefix a qualified name associated with a namespace.
我们没有只使用前缀,还给<table>标签添加了xmlns属性,使得前缀有一个与命名空间相联系的合法名称。


The XML Namespace (xmlns) Attribute
XML名称空间(xmlns)属性

The XML namespace attribute is placed in the start tag of an element and has the following syntax:
XML名称空间属性安排在一个元素的始标签里,有如下的语法:

xmlns:namespace-prefix="namespaceURI"

When a namespace is defined in the start tag of an element, all child elements with the same prefix are associated with the same namespace.
当一个命名空间在一个元素的始标签里被定义,有相同前缀的所有子元素都与相同的名称空间相关联;

Note that the address used to identify the namespace is not used by the parser to look up information. The only purpose is to give the namespace a unique name. However, very often companies use the namespace as a pointer to a real Web page containing information about the namespace.
要注意的是:用于定义名称空间的地址,并不被解析器用来寻找信息。它的唯一作用是给名称空间一个特别的名称。然而,许多公司经常把名称空间用作为实际存在的网页的指示器,这些网页含有关于名称空间的信息。
尝试到达W3.org: http://www.w3.org/TR/html4/.


Uniform Resource Identifier (URI)
统一资源识别器(URI)

Uniform Resource Identifier (URI) is a string of characters which identifies an Internet Resource. The most common URI is the Uniform Resource Locator (URL) which identifies an Internet domain address. Another, not so common type of URI is the Universal Resource Name (URN). In our examples we will only use URLs.
URI是识别网络资源的一串字符。最普通的URI是Uniform Resource Locator URL,它识别英特网的域地址。另一种不太普遍的URI是Universal Resource Name (URN).在我们的例子里我们只用URL。


Default Namespaces
默认的名称空间(Namespaces)

Defining a default namespace for an element saves us from using prefixes in all the child elements. It has the following syntax:
给元素定义默认的名称空间,可以帮我们省去给所有子元素用前缀的麻烦。它有如下的语法:

xmlns="namespaceURI"

This XML document carries information in a table:
这份XML文档含有表格里的信息:

<table xmlns="http://www.w3.org/TR/html4/">

   <tr>
   <td>Apples</td>
   <td>Bananas</td>
   </tr>
</table>

This XML document carries information about a piece of furniture:
这份XML文档含有关于一张桌子的信息(一件家具)

<table xmlns="http://www.w3schools.com/furniture">
   <name>African Coffee Table</name>
   <width>80</width>

   <length>120</length>
</table>


Namespaces in Real Use
实际使用中的名称空间

When you start using XSL, you will soon see namespaces in real use. XSL style sheets are used to transform XML documents into other formats, like HTML.
一旦开始使用XSL,你会很快看到实际使用中的名称空间。XSL式样表用于把XML转换成其他形式,例如HTML。

If you take a close look at the XSL document below, you will see that most of the tags are HTML tags. The tags that are not HTML tags have the prefix xsl, identified by the namespace "http://www.w3.org/1999/XSL/Transform": 
如果你仔细看下面的例子,你会看到大部分标签都是HTML型的标签。非HTML的标签含有“xsl”的前缀,可以被名称空间识别。 "http://www.w3.org/1999/XSL/Transform": 

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
  <h2>My CD Collection</h2>
  <table border="1">

    <tr>
      <th align="left">Title</th>
      <th align="left">Artist</th>
    </tr>

    <xsl:for-each select="catalog/cd">
    <tr>
      <td><xsl:value-of select="title"/></td>
      <td><xsl:value-of select="artist"/></td>

    </tr>
    </xsl:for-each>
  </table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值