XML 命名空间
2008-03-21 16:08
225 查看
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文档携带了表格中的信息:
This XML document carries information about a table (a piece of furniture):
这份XML文档携带了一张桌子的信息(一件家具):
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文档携带了表格中的信息:
This XML document carries information about a piece of furniture:
这份XML文档中携带了一件家具的信息:
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文档携带了表格中的信息:
This XML document carries information about a piece of furniture:
这份XML文档中携带了一件家具的信息:
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
The XML namespace attribute is placed in the start tag of an element and has the following syntax:
将XML命名空间属性放置在一个元素的起始标签中,语法如下:
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.
值得注意的是:解析器并不会使用这些用于定义命名空间的地址来搜索信息。这样做的目的仅是为命名空间提供一个独立的名称。然而,在大多数情况下,公司会将命名空间作为一个指示器,指向包含命名空间具体信息的真实网页。
自己看看吧: http://www.w3.org/TR/html4/.
Uniform Resource Identifier (URI)
A 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是统一资源定位器(URL),它用于对互联网的域名地址进行定位;另一种不太普遍的URI是通用资源名称 (URN)。在我们的案例中,我们只使用URL。
Default Namespaces
Defining a default namespace for an element saves us from using prefixes in all the child elements. It has the following syntax:
为元素定义一个默认的命名空间,我们就不需要为每个子元素定义前缀。语法如下:
This XML document carries information in a table:
这份XML文档携带了表格中的信息:
This XML document carries information about a piece of furniture:
这份XML文档中携带了一件家具的信息:
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 命名空间提供了避免元素名称冲突的方法。
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> |
这份XML文档携带了一张桌子的信息(一件家具):
<table> <name>African Coffee Table</name> <width>80</width> <length>120</length> </table> |
如果把两份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> |
这份XML文档中携带了一件家具的信息:
<f:table> <f:name>African Coffee Table</f:name> <f:width>80</f:width> <f:length>120</f:length> </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> |
这份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> |
在这里,我们并没有使用前缀,取而代之的是,我们给<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" |
在元素的起始标签中定义一个命名空间时,所有包含相同前缀的子元素都与相同的名称空间相匹配;
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.
值得注意的是:解析器并不会使用这些用于定义命名空间的地址来搜索信息。这样做的目的仅是为命名空间提供一个独立的名称。然而,在大多数情况下,公司会将命名空间作为一个指示器,指向包含命名空间具体信息的真实网页。
自己看看吧: http://www.w3.org/TR/html4/.
Uniform Resource Identifier (URI)
统一资源识别器(URI)
A 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是统一资源定位器(URL),它用于对互联网的域名地址进行定位;另一种不太普遍的URI是通用资源名称 (URN)。在我们的案例中,我们只使用URL。
Default 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" |
这份XML文档携带了表格中的信息:
<table xmlns="http://www.w3.org/TR/html4/"> <tr> <td>Apples</td> <td>Bananas</td> </tr> </table> |
这份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> |
相关文章推荐
- Spring4.3.x 浅析xml配置的解析过程(10)——解析aop命名空间之aspectj-autoproxy标签
- Android中XML的命名空间、自定义属性
- C#读取写入带有命名空间的XML文件
- 在XML序列化时去除默认命名空间xmlns:xsd和xmlns:xsi
- XML 命名空间与jar包版本不一,导致spring mvc搭建不起来?
- 全栈JavaScript之路( 二十三 )DOM2、DOM3, 涉及XML命名空间的扩展(一)
- 当xml中存在命名空间,dom4j解析以及写入xml文档时的乱码问题
- 去掉xml中的命名空间
- 读取带有Xmlns命名空间的XML文件
- .net关于xml文件命名空间的解析读取
- 在jquery中处理带有命名空间的XML数据
- Jaxb解析xml(带命名空间)相关联的属性 "xsi:NamespaceSchemaLocation" 的前缀 "xsi" 未绑定。
- XML 命名空间
- xml-了解 XML 命名空间
- .NET(C#):使用XPath查询带有命名空间(有xmlns)的XML
- 在XML序列化时去除默认命名空间xmlns:xsd和xmlns:xsi
- 使用SQL Server的OpenXML解析带有命名空间的XML片段
- Linq to Xml读取复杂xml(带命名空间)
- spring-security-4.2.1.RELEASE命名空间基本配置(xml)
- 资源文件xml里的命名空间