XML的xmlns:xsi和 xsi:schemaLocation

xmlns作用

在 XML 中,元素名称是由开发者自己定义的,当两个不同的文档使用相同的元素名时,就会发生命名冲突。

这个 XML 文档携带着某个表格中的信息:

<table>
   <tr>
   <td>Apples</td>
   <td>Bananas</td>
   </tr>
</table>

这个 XML 文档携带有关桌子的信息(一件家具):

<table>
   <name>African Coffee Table</name>
   <width>80</width>
   <length>120</length>
</table>

假如这两个 XML 文档被一起使用,由于两个文档都包含带有不同内容和定义的

元素,就会发生命名冲突。

XML 解析器无法确定如何处理这类冲突。

使用命名空间(Namespaces)

这个 XML 文档携带着某个表格中的信息:

<h:table xmlns:h="http://www.w3.org/TR/html4/">
   <h:tr>
   <h:td>Apples</h:td>
   <h:td>Bananas</h:td>
   </h:tr>
</h:table>

此 XML 文档携带着有关一件家具的信息:

<f:table xmlns:f="http://www.w3school.com.cn/furniture">
   <f:name>African Coffee Table</f:name>
   <f:width>80</f:width>
   <f:length>120</f:length>
</f:table>

与仅仅使用前缀不同,我们为

标签添加了一个 xmlns 属性,这样就为前缀赋予了一个与某个命名空间相关联的限定名称。

XML Namespace (xmlns) 属性

XML 命名空间属性被放置于元素的开始标签之中,并使用以下的语法:

xmlns:namespace-prefix="namespaceURI"

当命名空间被定义在元素的开始标签中时,所有带有相同前缀的子元素都会与同一个命名空间相关联。

xsi:schemaLocation 是什么?

schemaLocation就是 xsi 命名空间的一个属性,
如果把xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 的别名改成
xmlns:test="http://www.w3.org/2001/XMLSchema-instance"
这里就变成 test:schemaLocation,

属性schemaLocation作用

这个属性的值由一个或多个URI引用对组成,两个URI之间以空白符分隔(空格和换行均可)
它定义了XML Namespace和对应的 XSD(Xml Schema Definition)文档的位置的关系,意思就是 这个命名空间对应的具体模板是哪个。
例如打开 http://www.springframework.org/schema/mvc/ 这个 命名空间,可以看到有很多选择

总结 xmlns:xsixsi:schemaLocation

基本上每一个xml文件的根标签都会有这两个属性。

xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"

个人理解:

  • `两个属性是xml默认的,当使用命名空间时(无论使用默认命名空间还是其他命名空间)这两个属性必须设置;
  • xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance - xml模板实例(XMLSchema-instance)命名空间;
  • xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" - 表明此xml文档所有的 Namespace和对应的 XSD(Xml Schema Definition)文档的位置。

一个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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:util="http://www.springframework.org/schema/util"
	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/aop 
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/util 
        http://www.springframework.org/schema/util/spring-util.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">

</beans>
posted on 2021-12-23 18:06  哑吧  阅读(595)  评论(0)    收藏  举报