xmlns、xmlns:xsi、xsi:schemaLocation

参考链接:
摘抄自
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的XML Namespace,也就是在http://www.springframework.org/schema/context中定义的。

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



  
    Apples
    Bananas
  




  African Coffee Table
  80
  120

我们为  标签添加了一个 xmlns 属性,这样就为前缀赋予了一个与某个命名空间相关联的限定名称。此时再把它们放在一起,XML解析器就不会报错了。

注意:当xmlns被定义在元素的开始标签中(如这里的)时,所有带有相同前缀的子元素都会与同一个Namespace相关联(即里面的也会使用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就没有使用前缀,直接使用默认命名空间。

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


    
    ...

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的位置,下面是这个文档的开始部分:

 
    targetNamespace="http://www.springframework.org/schema/context"
    elementFormDefault="qualified"
    attributeFormDefault="unqualified">

你可能感兴趣的:(xmlns、xmlns:xsi、xsi:schemaLocation)