置信很多人和我一样,在编写Spring或者Maven或者其余须要用到XML文档的程序时,通常都是将这些XML文档头拷贝过去,并没有了解其中元素(比方xmlns,xmlns:xsi,xsi:schemaLocation)的真正含意,不晓得哪些元素是多余的,也不晓得为什么要加那些元素。这样当有时候网上Copy的XML头有错的时候本人却不晓得怎么下手。我也是这样的,于是明天花了点工夫好好的了解了一下这些元素及其用法,现整顿与此,在此谢谢各位前辈的教训,如有总结的不对或者不好的中央,欢送留言提出各位的宝贵意见。
话不多说,先来一段Spring的XML样本,置信大家都很眼生:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="xxx.xxx.controller" /> <context:annotation-config/> <mvc:default-servlet-handler/> <mvc:annotation-driven/> <mvc:resources mapping="/images/**" location="/images/" /> <bean id="xxx" class="xxx.xxx.xxx.Xxx"> <property name="xxx" value="xxxx"/> </bean></beans>
这个文档中,根元素<beans/>就不用说了,接下来是xmlns。那么什么是xmlns呢?xmlns其实是XML Namespace的缩写,可译为“XML命名空间”,但集体感觉,翻译后的名字反而不好了解,所以咱们就叫它为XML Namespace吧。
为什么须要xmlns?
思考这样两个XML文档:示意HTML表格元素的<table/>:
<table> <tr> <td>Apples</td> <td>Bananas</td> </tr></table>
和形容一张桌子的<table/>:
<table> <name>African Coffee Table</name> <width>80</width> <length>120</length></table>
如果这两个 XML 文档被一起应用,因为两个文档都蕴含带有不同内容和定义的 <table> 元素,就会产生命名抵触。XML 解析器是无奈确定如何解决这类抵触。为了解决上述问题,xmlns就产生了。
如何应用xmlns?
很简略,应用语法: xmlns:namespace-prefix="namespaceURI"。其中namespace-prefix为自定义前缀,只有在这个XML文档中保障前缀不反复即可;namespaceURI是这个前缀对应的XML Namespace的定义。例如,
xmlns:context="http://www.springframework.org/schema/context"
这一句定义了一个http://www.springframwork.org...(这和Java类中的包的申明很类似),并将其和前缀context绑定。所以下面的Spring XML文档中会有这么一句:
<context:component-scan base-package="xxx.xxx.controller" />
这里的<component-scan/>元素就来自别名为context的XML Namespace,也就是在http://www.springframework.or...。
咱们还能够将前缀定义为abc:
xmlns:abc="namespaceURI"
这样再应用这个namespaceURI中的元素时,须要以abc为前缀,例如:<abc:xxx/>。再拿下面的例子解释怎么应用xmlns:
<!-- 这里xmlns:h="url1"示意这个table是用h作为标记,table的写法在url1中定义 --><h:table xmlns:h="url1"> <h:tr> <h:td>Apples</h:td> <h:td>Bananas</h:td> </h:tr></h:table>
和:
<!-- 这里xmlns:f="url2"示意这个table是用f作为标记,table的写法在url2中定义 --><f:table xmlns:f="url2"> <f:name>African Coffee Table</f:name> <f:width>80</f:width> <f:length>120</f:length></f:table>
后者与前者仅仅应用不同前缀,咱们为 <table> 标签增加了一个 xmlns 属性,这样就为前缀赋予了一个与某个命名空间相关联的限定名称。此时再把它们放在一起,XML解析器就不会报错了。
留神:当xmlns被定义在元素的开始标签中(如这里的<f:table/>)时,所有带有雷同前缀的子元素都会与同一个Namespace相关联(即<f:table/>外面的<f:name/>和<f:width/>也会应用url2定义的写法)。
xmlns和xmlns:xsi有什么不同?
xmlns示意默认的Namespace。例如Spring XML文档中的
xmlns="http://www.springframework.org/schema/beans"
这一句示意该文档默认的XML Namespace为http://www.springframwork.org...。对于默认的Namespace中的元素,能够不应用前缀。例如Spring XML文档中的
<bean id="xxx" class="xxx.xxx.xxx.Xxx"> <property name="xxx" value="xxxx"/></bean>
xmlns:xsi示意应用xsi作为前缀的Namespace,当然前缀xsi须要在文档中申明。
*xsi:schemaLocation有何作用?*
xsi:schemaLocation属性其实是Namespace为http://www.w3.org/2001/XMLSch...,正是因为咱们一开始申明了
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
这里才写作xsi:schemaLocation(当然个别都应用这个前缀)。它定义了XML Namespace和对应的XSD(Xml Schema Definition)文档的地位的关系。它的值由一个或多个URI援用对组成(能够简略了解为Key-Value对),两个URI之间以空白符分隔(空格和换行均可)。第一个URI(即Key-Value对中的Key)是定义的XML Namespace的值,第二个URI(即Value)给出Schema文档的地位,Schema处理器将从这个地位读取Schema文档,该文档的targetNamespace必须与第一个URI相匹配。例如:
xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
这里示意Namespace为http://www.springframework.or...http://www.springframework.org/schema/context/spring-context.xsd。这里咱们能够关上这个Schema的地位,上面是这个文档的开始局部:
<xsd:schema xmlns="http://www.springframework.org/schema/context" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:tool="http://www.springframework.org/schema/tool" <!-- 这里的targetNamespace和上方xsi:schemaLocation中的第一个URI匹配 --> targetNamespace="http://www.springframework.org/schema/context" elementFormDefault="qualified" attributeFormDefault="unqualified">
能够发现这个文档中定义的targetNamespace和上方xsi:schemaLocation中的第一个URI匹配,都是http://www.springframework.or...。有了下面的阐明后,再去了解开始的Spring XML文档,肯定会有不一样的感觉!
最初再次感激各位前辈的贵重教训。
参考资料:
http://www.springframework.org/schema/context/spring-context.xsd
http://bbs.csdn.net/topics/390751819
http://blog.csdn.net/zhengyeqing520/article/details/6091656
http://www.360doc.com/content/11/0323/13/4619459_103829478.shtml
http://baike.baidu.com/view/1853567.htm