虽然经常看到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/context
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>
xmlns其实是XML NameSpace的缩写。它指示了这里使用的xml标签来自哪里的定义,如beans标签就来这xmlns后面的url中。而我们还能看到使用了context:annotation-config标签,这里是因为将context定义为了xmlns:context=”http://www.springframework.org/schema/context”,所以使用这里url中的标签要加上context前缀
为什么需要xlmns
考虑这样两个XML文档:表示HTML表格
<table>
<tr>
<td>Apples</td>
<td>Bananas</td>
</tr>
</table>
和描述一张桌子的
<table>
<name>African Coffee Table</name>
<width>80</width>
<length>120</length>
</table>
当两个XML文档一起使用,XML文档解析器就入伍解决这类冲突问题。
如何使用xmlns
使用语法:
xmlns:namespace-prefix="namespaceURI"
其中namespace-prefix是自定义前缀,在xml文档中要保证前缀不重复;namespaceURI表示这个前缀对应的XML Namespace的定义
如例子中的
xmlns:context="http://www.springframework.org/schema/context"
这句话就将http://www.springframwork.org/schema/context和context进行了绑定,当然我们在这里也可以使用其他的名称前缀。使用的使用,前缀名称写在前面后面跟上要使用的标签,如
<context:component-scan base-package="xxx.xxx.controller" />
这里的component-scan就来自别名为context的XML namespace
我们可以看到在例子中直接使用了xmlns没有使用前缀,这表示默认的namespace,如
xmlns="http://www.springframework.org/schema/beans"
在使用默认空间中的标签时,直接写标签名称就可以。如这里bean的使用。
<bean id="xxx" class="xxx.xxx.xxx.Xxx">
<property name="xxx" value="xxxx"/>
</bean>
xsi:schemaLocation有何作用?
xsi:schemaLocation属性其实是Namespace为http://www.w3.org/2001/XMLSchema-instance里的schemaLocation属性,正是因为我们一开始声明了
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
这里才写作xsi:schemaLocation(当然一般都使用这个前缀)。它定义了XML Namespace和对应的XSD(Xml Schema Definition)文档的位置的关系。XSD的作用是定义XML文档的合法构建模块。它的值由一个或多个URI引用对组成,两个URI之间以空白符分隔(空格和换行均可)。第一个URI是定义的XML Namespace的值,第二个URI给出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.org/schema/context的Schema的位置为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">
以上就是xml文档中xmlns xmlns:xsi等的分析
接下来,我们用刚刚学习到的知识来看看maven的pom.xml文件
pom.xml文件类似如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>myproject</artifactId>
<version>0.0.1-SNAPSHOT</version>
</project>
这里引用的所有标签都来自默认空间,即xmlns,除了schemaLocation属性。xmlns:xsi表示将命名空间定义为xsi的别名。xsi:schemaLocation定义了maven空间与对应的xsd的位置关系。
参考
关于XML文档的xmlns、xmlns:xsi和xsi:schemaLocation