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.
值得注意的是:解析器并不会使用这些用于定义命名空间的地址来搜索信息。这样做的目的仅是为命名空间提供一个独立的名称。然而,在大多数情况下,公司会将命名空间作为一个指示器,指向包含命名空间具体信息的真实网页。
自己看看吧: 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"
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>
分享到:
相关推荐
xml命名空间讲解,很详细,很不错的讲义,希望对大家有帮助
WebApi去除XML命名空间 vs2013版本及以上环境下运行
python xml命名空间 The ns0: prefix for an element name is a reference to a (XML) NameSpace.
XML的命名空间
XML 命名空间(XML Namespaces)
winform 使用XML命名空间源码! 很值得下载看看!资源免费,大家分享!!
WebApi 返回 xml 格式数据时自动去除命名空间相关标记
jquery.xmlns-1.7.0.js 是根据jquery.xmlns.js改写的适用于各种版本的jquery解析带命名空间的XML数据,里面附有实例代码,如果使用中有遇到问题,可以反馈,我会进一步改进。
删除和添加对XML命名空间未做处理。不过仿照查询和修改,很简单!将数据查询出来,并且绑定到下拉列表框,处理了数据重复的问题!实现二级联动的效果!加油哦!
主要介绍了XML的命名空间,包括命名空间的声明等XML入门学习中的基础知识,需要的朋友可以参考下
通过 利用 java 以及 dom4j 来进行XML 文件的 生成与 解析操作。代码简单易懂。
services.xml加命名空间services.xml加命名空间
WCF 返回XML去除默认命名空间实例Demo 解压后根据说明文档修改您的程序。
一本关于XML Schema的打包教程,深入浅出,系统性强,并附有相应实例。
元素的lang(无命名空间的)属性用于定义元素的内容及其他文本属性的语言种类。 它的值必须是符合BCP 47规定范围内的一个语言标签。 当该属性设置成空字符串时,...请不要在html文档中使用带有xml命名空间的lang属性。
xml的命名空间.md
怎样使用XML命名空间来避免元素命名冲突。 XML CDATA 怎样让XML解析器知道某些文字不进行解析呢。使用CDATA。 XML编码 怎样把你的XML文档编码。 XML服务器 怎样在服务器上产生XML。 XML应用 怎样...
C# 读取 带 xmlns 命名空间 XML 结点 SelectSingleNode 类 For .NET Framework 1.1x AutoPrefix(System.Xml.XmlNode xNode, System.Xml.XmlNamespaceManager xNameSpaceManager) AutoXPath(System.Xml.XmlNode ...
3.4 XML命名空间 3.5 开发工具XML Spy与XML实例 第四章 XML文档类型定义 第五章 XML Schema结构 第六章 层叠样式表 6.1 CSS简介 6.2 设置字体属性 6.3 设置色彩和背景图像属性 6.4 设置边界属性 ...