在 XML 中,元素名称是自定义的,当两个不同的文档使用相同的元素名或者同一份文档使用相同的元素名表示不同的意思时,就会发生命名冲突。
例如:
这个 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> 元素,就会发生命名冲突。
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.w3school.com.cn/furniture">
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>
我们为 <table> 标签增加了一个前缀,添加了一个 xmlns 属性,这样就为前缀赋予了一个与某个命名空间相关联的限定名称。
命名空间语法:
XML 命名空间属性被放置于元素的开始标签之中,并使用以下的语法:
xmlns:namespace-prefix="namespaceURI"
当命名空间被定义在元素的开始标签中时,所有带有相同前缀的子元素都会与同一个命名空间相关联。
如上例中<f:name></f:name>,<f:width></f:width>都与“http://www.w3school.com.cn/furniture"命名空间相关联
注释:用于标示命名空间的地址不会被解析器用于查找信息。其惟一的作用是赋予命名空间一个惟一的名称。不过,很多公司常常会作为指针来使用命名空间指向实际存在的网页,这个网页包含关于命名空间的信息。也就是说这个命名空间是可以任意命名的,可以写成<f:table xmlns:f="http://www.lsj.123456"也可以。这个网站只是用来标识这个命名空间。
默认的命名空间(Default Namespaces)
为元素定义默认的命名空间可以让我们省去在所有的子元素中使用前缀的工作。
请使用下面的语法:
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.w3school.com.cn/furniture">
<name>African Coffee Table</name>
<width>80</width>
<length>120</length>
</table>
是不是简洁多了,实际应用中凡有声明命名空间的 XML 文档中,都是以默认方式来声明的。还有,即使是用了显式声明命名空间语法,如果其中仍有未带前缀的标签存在,其实那个标签也是在一个默认的命名空间中,只是没有明确告诉你一个默认的 URI,如下:
- <earth:labourer xmlns:earth="http://unmi.blogjava.net/earthy"
- xmlns:net="http://unmi.blogjava.net/net">
- <name>Unmi</name>
- <net:address>fantasia@sina.com</net:address>
- <earth:address>Gaoxin Nanyi Road, NanShan District, Shenzhen</earth:address>
- </earth:labourer>
- <xsl:stylesheet xmlns:xsl=http://www.w3.org/TR/WD-xsl">
- <xsl:template match="/">
- ........
- <html>
- ..........................................
- <xsl:stylesheet xmlns:xsl=http://www.w3.org/TR/WD-xsl">
- <xsl:template match="/">
- ........
- <html>
- ..........................................
②XHTML 文件
- <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
- <table>
- .....................................
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:jee="http://www.springframework.org/schema/jee">
- <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- </bean>
- <jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/ds"/>
475

被折叠的 条评论
为什么被折叠?



