xmlns:(XML Namespaces的缩写)是一个属性,是XML(标准通用标记语言的子集)命名空间的声明。作用是赋予命名空间一个唯一的名称。
命名空间是什么呢?
由于xml允许你自己定义自己的标识,你定义的标识和其他人定义的标识有可能相同,但表示不同的意义。当文件交换或者共享的时候就容易产生错误。为了避免这种错误发生,XML采用名字空间声明,允许你通过一个网址指向来识别你的标识。比如下面的例子:
<!-- 这里的 table 元素描述的是一个表格-->
<table>
<tr>
<td>Apples</td>
<td>Bananas</td>
</tr>
</table>
<!-- 这里的 table 元素描述的是一个家居桌子-->
<table>
<name>African Coffee Table</name>
<width>80</width>
<length>120</length>
</table>
第一个<table>表示表格,而第二个<table>表示家居桌子。如果我需要同时处理这两个文档,就会发生名字冲突。为了解决这个问题,我们引进了Namespaces这个概念。Namespaces通过给标识名称加一个很长的可以保证全局唯一性的字符串与该元素关联起来,这样就可以避免命名冲突了。是如何保证那个较长的字符串全局唯一呢?最好的方式莫过于使用 统一资源标识符(Uniform Resource Identifier,URI) 了, 而我们最常见的 URI 就是平时经常访问的网址 URL 了,所以经常用网址(URL)定位的方法来区别这些名称相同的标识,比如:
xmlns:namespace-prefix="namespaceURI"
namespace-prefix:表示命名空间的前缀,即我们自己定义的标识。
namespaceURI:用来唯一标识一个命名空间的网址
应用到我们所举的例子中就是:
<!-- 这里的 table 元素描述的是一个表格-->
<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>
<!-- 这里的 table 元素描述的是一个家居桌子-->
<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>
实战举例解析:
<?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:batch="http://www.springframework.org/schema/batch"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch.xsd">
<!--导入文件-->
<import resource="classpath:demo1/job/demo1-jobContext.xml"/>
<!--定义名字为billJob的作业-->
<batch:job id="billJob">
<!--定义名字为billStep的作业步-->
<batch:step id="billStep">
<batch:tasklet transaction-manager="transactionManager">
<!--定义读、处理、写操作,规定每处理两条数据,进行一次写入操作,这样可以提高写的效率-->
<batch:chunk reader="csvItemReader" processor="creditBillProcessor" writer="csvItemWriter" commit-interval="2">
</batch:chunk>
</batch:tasklet>
</batch:step>
</batch:job>
</beans>
第一部分:
<beans xmlns="http://www.springframework.org/schema/beans">
......
</beans>
定义一个beans标识,采用的是默认命名空间方法,即:
<elementName xmlns="namespaceURI">
第二部分:
<xmlns:batch="http://www.springframework.org/schema/batch">
定义一个batch标识
第三部分:
<?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:batch="http://www.springframework.org/schema/batch"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch.xsd">
......
</beans>
根据之前的知识我们可以理解, xmlns:xsi 定义了一个命名空间前缀 xsi ,对应的唯一字符串 http://www.w3.org/2001/XMLSchema-instance。 但是读者会发现, 这个 xmlns:xsi 在不同的 xml 文档中似乎都会出现。 这是因为, xsi 已经成为了一个业界默认的用于 XSD((XML Schema Definition) 文件的命名空间。 而 XSD 文件(也常常称为 Schema 文件)是用来定义 xml 文档结构的。
注解: XML 解析器可以根据 一个 XSD 文件的内容来解析另一个 XML 文件, 判断该文件的结构是否和 XSD 文件中定义的一致。 XSD 文件 可以理解为 XML 文档可以自定义的语法或格式检查器。
那么, 有了上述的理解, 再来看xsi:schemaLocation这里的语法其实是:
xsi:schemaLocation = "键" “值”
"键":
用来唯一标识一个命名空间的网址
“值”:
指代 【XSD location URI】 , 这个值指示了前一个命名空间所对应的 XSD 文件的位置, xml parser 可以利用这个信息获取到 XSD 文件, 从而通过 XSD 文件对该自定义标识(即命名空间)的元素结构进行校验, 因此这个值必然是可以访问的, 且访问到的内容是一个 XSD 文件的内容。
比如:
xsi:schemaLocation="http://www.springframework.org/schema/beans(beans的键) http://www.springframework.org/schema/beans/spring-beans.xsd(beans的值)
http://www.springframework.org/schema/batch(batch的键) http://www.springframework.org/schema/batch/spring-batch.xsd(batch的值)"
参考: