关于XML文档的xmlns、xmlns:xsi和xsi:schemaLocation
摘要: 相信很多人和我一样,在编写Spring或者Maven或者其他需要用到XML文档的程序时,通常都是将这些XML文档头拷贝过来,并没有理解其中元素 (比如xmlns,xmlns:xsi,xsi:schemaLocation)的真正含义,不知道哪些元素是多余的,也不知道为什么要加那些元素。这样 当有时候网...
相 信很多人和我一样,在编写Spring或者Maven或者其他需要用到XML文档的程序时,通常都是将这些XML文档头拷贝过来,并没有理解其中元素(比 如xmlns,xmlns:xsi,xsi:schemaLocation)的真正含义,不知道哪些元素是多余的,也不知道为什么要加那些元素。这样当有 时候网上Copy的XML头有错的时候自己却不知道怎么下手。我也是这样的,于是今天花了点时间好好的理解了一下这些元素及其用法,现整理与此,在此谢谢 各位前辈的经验,如有总结的不对或者不好的地方,欢迎留言提出各位的宝贵意见。
话不多说,先来一段Spring的XML样本,相信大家都很眼熟:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<?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?
考虑这样两个XML文档:表示HTML表格元素的<table/>:
|
1
2
3
4
5
6
|
<table> <tr> <td>Apples</td> <td>Bananas</td> </tr></table> |
和描述一张桌子的<table/>:
|
1
2
3
4
5
|
<table> <name>African Coffee Table</name> <width>80</width> <length>120</length></table> |
假如这两个 XML 文档被一起使用,由于两个文档都包含带有不同内容和定义的 <table> 元素,就会发生命名冲突。XML 解析器是无法确定如何处理这类冲突。为了解决上述问题,xmlns就产生了。
如何是用xmlns?
很简单,使用语法: xmlns:namespace-prefix="namespaceURI"。其中namespace-prefix为自定义前缀,只要在这个XML文档中保证前缀不重复即可;namespaceURI是这个前缀对应的XML Namespace的定义。例如,
|
1
|
xmlns:context="http://www.springframework.org/schema/context" |
这一句定义了一个http://www.springframwork.org/schema/context的Namespace(这和Java类中的包的声明很相似),并将其和前缀context绑定。所以上面的Spring XML文档中会有这么一句:
|
1
|
<context:component-scan base-package="xxx.xxx.controller" /> |
这里的<component-scan/>元素就来自别名为context的XML Namespace,也就是在http://www.springframework.org/schema/context中定义的。
我们还可以将前缀定义为abc:
|
1
|
xmlns:abc="namespaceURI" |
这样再使用这个namespaceURI中的元素时,需要以abc为前缀,例如:<abc:xxx/>。再拿上面的例子解释怎么使用xmlns:
|
1
2
3
4
5
6
7
|
<!-- 这里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>
|
和:
|
1
2
3
4
5
6
|
<!-- 这里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文档中的
|
1
|
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>
|
xmlns:xsi表示使用xsi作为前缀的Namespace,当然前缀xsi需要在文档中声明。
xsi:schemaLocation有何作用?
xsi:schemaLocation属性其实是Namespace为http://www.w3.org/2001/XMLSchema-instance里的schemaLocation属性,正是因为我们一开始声明了
|
1
|
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相匹配。例如:
|
1
2
|
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的位置,下面是这个文档的开始部分:
|
1
2
3
4
5
6
7
8
9
|
<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">
|
有了上面的说明后,再去理解开始的Spring XML文档,一定会有不一样的感觉!
最后再次感谢各位前辈的宝贵经验。
关于XML文档的xmlns、xmlns:xsi和xsi:schemaLocation的更多相关文章
- 操作XML文档遇到的XMLNS问题及解决方法 (C# 和 PHP)
原文:操作XML文档遇到的XMLNS问题及解决方法 (C# 和 PHP) 不管是用 PHP 还是 C#, 在操作 XML 的时候我们除了一个节点一个节点去取值之外, 还有一个非常方便的表达式, 就是 ...
- XML文档中的xmlns、xmlns:xsi和xsi:schemaLocation
文章转载自:https://yq.aliyun.com/articles/40353 相信很多人和我一样,在编写Spring或者Maven或者其他需要用到XML文档的程序时,通常都是将这些XML文档头 ...
- 【转载】关于XML文档的xmlns、xmlns:xsi和xsi:schemaLocation
原文在: https://yq.aliyun.com/articles/40353 这里有转载:http://www.cnblogs.com/zhao1949/p/5652167.html 先来一段S ...
- 关于"XML 文档(2, 2)中有错误:不应有 <xml xmlns=''>"错误
XML文件名 <?xml version="1.0" encoding="utf-8"?> <Config xmlns:xsi="h ...
- 用Castor 处理XML文档
——Castor可以完成Java和XML的相互转换 前面有介绍过json-lib这个框架,在线博文:http://www.cnblogs.com/hoojo/archive/2011/04/21/20 ...
- python+selenium自动化软件测试(第12章):Python读写XML文档
XML 即可扩展标记语言,它可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语言进 行定义的源语言.xml 有如下特征: 首先,它是有标签对组成:<aa></aa> ...
- 根据Schema写出XML文档四部曲
Schema约束文档本身就是一个XML文档,扩展名为xsd 难点:XML文档的根元素怎么写? 如下4步曲: a.首先看Schema文档,找到根元素 <?xml version="1.0 ...
- .NET(C#)使用Serialize、Deserialize序列和反序列化XML文档
本文给大家分享一下C#操作(读取.写入)XML文档的实用方法,即用.NET本身提供的Deserialize和Serialize进行反序列化和序列化XML文档.这种方法主要是对比较规范的XML文档进行操 ...
- Spring学习----- Spring配置文件xml文档的schema约束
1.配置文件示例. <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="htt ...
随机推荐
- osx 10.11 一键制作U盘傻瓜工具最新版 无需任何命令
osx 10.11 最新版U盘制作工具 无需任何命令 纯傻瓜式 !!!只要把app下载下来放在应用程序 鼠标点点就可以做了... 下载地址:http://diskmakerx.com/do ...
- 【CF526F】Pudding Monsters cdq分治
[CF526F]Pudding Monsters 题意:给你一个排列$p_i$,问你有对少个区间的值域段是连续的. $n\le 3\times 10^5$ 题解:bzoj3745 Norma 的弱化版 ...
- go 的文件处理
准备一个文件 imooc.txt hello world! 一.使用 io/ioutil 包 定义一个 check 函数 func check(err error) { if err != nil { ...
- ThinkPHP框架 AJAX方法返回 AJAX实现分页例子:
在模块控制器Controller文件夹里创建一个 FenyeController.class.php控制器 <?php namespace Admin\Controller; use Think ...
- Latest China Scam: I've Been Arrested in the Brothel Crackdown!
Latest China Scam: I've Been Arrested in the Brothel Crackdown! If the sex industry is fastest to se ...
- How to add the ApplicationPoolIdentity to a SQL Server Login
The ApplicationPoolIdentity is a virtual account in Windows that is dynamically generated when the a ...
- 启动weblogic报错:string value '2.4' is not a valid enumeration value for web-app-versionType in namespace http://java.sun.com/xml/ns/javaee
启动报错: 原因:有人改动了web.xml的头 解决方法: 在web.xml中修改抬头为: <?xml version="1.0" encoding="UTF-8& ...
- Direct Visual-Inertial Odometry with Stereo Cameras
这对于直接方法是特别有益的:众所周知直接图像对准是非凸的,并且只有在足够准确的初始估计可用时才能预期收敛.虽然在实践中像粗到精跟踪这样的技术会增加收敛半径,但是紧密的惯性积分可以更有效地解决这个问题, ...
- Windows小技巧 -- 目录内打开CMD的快捷方式
工作中常常会有需要在某个文件夹内使用cmd的情况,例如运行某脚本,下面演示几种方法. 以进入以下目录操作为例: 方式一 : 常用的cd命令 cd命令是我们平常使用比较多的方式: 1. Win+R打开c ...
- 关于tomcat服务器
如果遇到jsp代码反复运行不成功,并且不报错 而且代码也重复检查过,正确无误了 那么 就不要把精力放在代码上了 有可能是服务器的问题 重启下服务器试试 ……不要问我尽经历过什么