zoukankan      html  css  js  c++  java
  • 【转载】关于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!

  • 相关阅读:
    BZOJ-1625 宝石手镯 01背包(傻逼题)
    BZOJ-2929 洞穴攀岩 最大流Dinic(傻逼题)
    BZOJ3252: 攻略 可并堆
    二逼平衡树 Tyvj 1730 BZOJ3196 Loj#106
    [Noi2016]区间 BZOJ4653 洛谷P1712 Loj#2086
    [NOIP2014]飞扬的小鸟 D1 T3 loj2500 洛谷P1941
    BZOJ4554: [Tjoi2016&Heoi2016]游戏 luoguP2825 loj2057
    BZOJ 2599: [IOI2011]Race 点分治
    POJ1038 Bugs Integrated, Inc 状压DP+优化
    JLOI2015 城池攻占
  • 原文地址:https://www.cnblogs.com/charlesblc/p/5978764.html
Copyright © 2011-2022 走看看