美文网首页大刘的 iOS 自学笔记
xmlns、xmlns:xsi、xsi:schemaLocati

xmlns、xmlns:xsi、xsi:schemaLocati

作者: 大刘 | 来源:发表于2018-09-28 09:18 被阅读17次

    参考链接:
    摘抄自
    https://yq.aliyun.com/articles/40353

    xmlns: 即xml namespace

    示例:
    xmlns:context="http://www.springframework.org/schema/context
    此句代码定义了一个命名空间:http://www.springframework.org/schema/context并且前缀context和它关联。

    <context:component-scan base-package="xxx.xxx.controller" />
    这里的<component-scan/>元素就来自别名为context的XML Namespace,也就是在http://www.springframework.org/schema/context中定义的。

    比如有两个叫table的标签,就可以使用命名空间区分开来:

    <!-- 这里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/schema/beans。对于默认的Namespace中的元素,可以不使用前缀:

    <beans>
          <bean name="/hello" class="cn.weizhen.HelloController" />
          <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
          <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
          <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" />
    </beans>
    

    这里的beans和bean就没有使用前缀,直接使用默认命名空间。

    而xmlns:xsi只是表示使用xsi作为前缀的Namespace,当然前缀xsi需要在文档中声明。

    <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"
           ...>
        <context:component-scan base-package="cn.weizhen" />
        ...
    </beans>
    

    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)文档的位置的关系。它的值由一个或多个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">
    

    相关文章

      网友评论

        本文标题:xmlns、xmlns:xsi、xsi:schemaLocati

        本文链接:https://www.haomeiwen.com/subject/qacyoftx.html