XML文档中的xmlnsxmlns:xsi和xsi:schemaLocation

Posted WSYW126

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了XML文档中的xmlnsxmlns:xsi和xsi:schemaLocation相关的知识,希望对你有一定的参考价值。

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       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-2.5.xsd"
       default-autowire="byName">
    <!-- Activates annotation-based bean configuration -->
    <context:annotation-config/>


    <context:component-scan base-package="cc.wsyw126.web.prod"/>


    <bean id="messageSource"
          class="org.springframework.context.support.ReloadableResourceBundleMessageSource"
          p:basenames="file:$web_templates/home/message/errors"
          p:defaultEncoding="GBK"
          p:cacheSeconds="60"/>
</beans>

一直对上面的不太理解,今天看了下相关的资料,记录一下。

xmlns

是XML Namespace的缩写,可译为“XML命名空间”。

为什么使用xmlns?

其实看到名词解释就明白,命名空间。解决引用冲突,保证标签引用的唯一性。和C++的命名空间是类似的概念。

xmlns:xsi

语法: xmlns:namespace-prefix=”namespaceURI”。
1. namespace-prefix为自定义前缀,只要在当前的XML文档中保证前缀不重复,就可以保证引用的时候不冲突;
2. namespaceURI是前缀对应的XML Namespace的定义。

举例说明:

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

context是namespace-prefix。http://www.springframework.org/schema/context是namespaceURI。
因为绑定namespace,所以我们可以通过以下方式进行引用:

<context:annotation-config/>
<context:component-scan base-package="cc.wsyw126.web.consume"/>

如果没有进行namespace的绑定,在使用的时候就需要在每次使用的时候,进行指定(绑定):

<context:annotation-config xmlns:context="http://www.springframework.org/schema/context"/>
<context:component-scan base-package="cc.wsyw126.web.consume" xmlns:context="http://www.springframework.org/schema/context"/>

解释下面两种方式引用的区别

xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns= 表示当前文档默认的命名空间为http://www.springframwork.org/schema/beans。对于默认的Namespace中的元素,可以不使用前缀。举例(bean 没有使用前缀):

<bean id="messageSource"
      class="org.springframework.context.support.ReloadableResourceBundleMessageSource"
      p:basenames="file:$web_templates/home/message/errors"
      p:defaultEncoding="GBK"
      p:cacheSeconds="60"/>

在上面可以使用p:代替property的,是因为配置了xmlns:p="http://www.springframework.org/schema/p"

<bean id="messageSource"
      class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="file:$web_templates/home/message/errors"/>
    <property name="defaultEncoding" value="GBK"/>
    <property name="cacheSeconds" value="60"/>
</bean>

xsi:schemaLocation

xsi:schemaLocation,也是标签,也是一种引用。其实是Namespace为http://www.w3.org/2001/XMLSchema-instance里的schemaLocation属性。因为我们一开始声明了
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance",所以可以使用xsi这个前缀。

作用:定义了XML Namespace和对应的XSD(Xml Schema Definition)文档的位置的关系。
语法:它的值由一个或多个URI引用对组成,引用对内的两个URI之间以空白符分隔(空格和换行均可)。第一个URI是定义的XML Namespace的值,第二个URI给出Schema文档的位置(更具体一点是版本号)。Schema处理器将从这个位置读取对应版本的Schema文档。


参考资料:
https://www.w3cschool.cn/xml/xml-namespaces.html
备注:
转载请注明出处:http://blog.csdn.net/wsyw126/article/details/79212159
作者:WSYW126

以上是关于XML文档中的xmlnsxmlns:xsi和xsi:schemaLocation的主要内容,如果未能解决你的问题,请参考以下文章

通过 LINQ 创建 XML 文档,添加 xmlns,xmlns:xsi

详解 xml 文件头部的 xmlns:xsi

xml 中的xsi 是啥意思?

xml schema xmlns xmlns:xsi xsi:schemaLocation targetnamespace

xml 中的xsi 是啥意思?

xml命名空间