【转载】关于XML文档的xmlns、xmlns:xsi和xsi:schemaLocation

原文在:

https://yq.aliyun.com/articles/40353

这里有转载:http://www.cnblogs.com/zhao1949/p/5652167.html

 

先来一段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,避免重名

 

如何使用xmlns

xmlns:context="http://www.springframework.org/schema/context"

<context:component-scan base-package="xxx.xxx.controller" />
 xmlns和xmlns:xsi有什么不同?

    xmlns表示默认的Namespace。例如Spring XML文档中的

xmlns="http://www.springframework.org/schema/beans"

这一句表示该文档默认的XML Namespace为http://www.springframwork.org/schema/beans。对于默认的Namespace中的元素,可以不使用前缀。例如Spring XML文档中的

1
2
3
<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"
第一个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"
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">

Done!

posted @ 2016-10-19 21:15  blcblc  阅读(1311)  评论(0编辑  收藏  举报