XML命名空间

2018-09-26  本文已影响0人  lionel880

Spring被称为配置地狱,大量的XML需要配置,各类工程的转移等常常因为命名空间出问题。
今天对XML知识进行整理,当然 重点是为了理解命名空间
参考文档:XML Schema第1部分:结构第二版

1.xml 用途

XML 应用于 Web 开发的许多方面,常用于简化数据的存储和共享,指可扩展标记语言(EXtensible Markup Language),具有自我描述性。 (虽然现在传输用json格式更为轻便,但在spring中还是有很多的xml配置)

2.XML的树结构

XML 文档形成一种树结构
XML 文档必须包含根元素。该元素是所有其他元素的父元素。
XML 文档中的元素形成了一棵文档树。这棵树从根部开始,并扩展到树的最底端。
所有的元素都可以有子元素:如

<root>
<child>
<subchild>.....</subchild>
</child>
</root>

3.XML的解析

DOM(Document Object Model 文档对象模型)定义了访问和操作文档的标准方法。

The purpose of *XML Schema: Structures* is to define the nature of XML schemas and their component parts
 provide an inventory of XML markup constructs with which to represent schemas, 
and define the application of schemas to XML documents.

4.命名空间

4.1为什么需要命名空间

是为了防止冲突,举例来说,2个table就会冲突

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

<table>
<name>African Coffee Table</name>
<width>80</width>
<length>120</length>
</table>
4.2为了解决冲突问题,就需要加前缀,而xml中前缀的命名空间必须被定义
<h:table>
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>

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

当在 XML 中使用前缀时,一个所谓的用于前缀的命名空间必须被定义。
命名空间是在元素的开始标签的 xmlns 属性中定义的。xml name space
命名空间声明的语法如下。xmlns:前缀="URI"

<root>
<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>

为了更为清晰,写在头文件中

<root xmlns:h="http://www.w3.org/TR/html4/"
xmlns:f="http://www.w3cschool.cc/furniture">

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

命名空间为元素定义默认的命名空间可以让我们省去在所有的子元素中使用前缀的工作

xmlns="namespaceURI"

4.3 xmlns:xsi

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

之前我们已经理解了xmlns:xsi,其实也只是一个命名空间,它可以为任意值,只是为了方便和规范这么写罢了

xsi:schemaLocation="http://www.springframework.org/schema/context 
                    http://www.springframework.org/schema/context/spring-context.xsd"

我们来看看官方文档

 All schema processors have appropriate attribute declarations for these attributes built in, see ...
[Attribute Declaration for the 'schemaLocation' attribute (§3.2.7)](https://www.w3.org/TR/xmlschema-1/#xsi.schemaLocation) ...

schemal processor模式处理器,对于每个注解有有定义,这里就不是随意的了

| schemaLocation'属性的属性声明 |

属性
{名称} schemaLocation
{target namespace} http://www.w3.org/2001/XMLSchema-instance
{type definition} 匿名简单类型定义,如下所示:
属性
{名称} ·缺席·
{target namespace} http://www.w3.org/2001/XMLSchema-instance
{基本类型定义} 在内置·简单的UR-类型定义·

我们可以看到在实际使用中,schemaLocation后面的uri和xsd成对出现,前者是之前定义的命名空间,后者是定义的xsd结构定义,如spring的你就可以在spring包内找到这份xsd。实际找到xsd文件如,
spring-beans-4.3.1.RELEASE.jar/jar/org/springframework/beans/factory/xml/spring-beans-4.3.xsd

4.4 什么是xsd文件

xsi:schemaLocation 的值由一个或多个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的位置

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!--xsd文件本身是xml文件,第一行是xml声明-->
<xsd:schema xmlns="http://www.springframework.org/schema/beans" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.springframework.org/schema/beans">
<!--
   xsd作为xml文件,其根元素是schema
   属性xmlns:xsd="http://www.w3.org/2001/XMLSchema"是引入文档约束的,表示在当前文档导入"http://www.w3.org/2001/XMLSchema"中所描述的规则,并且使用里面的元素要添加xsd的前缀(和xmlns:xsd相对应,也可以指定其它前缀)
   属性targetNamespace="http://www.springframework.org/schema/beans"表示当前文档定义的规则处于命名空间"http://www.springfarmework.org/schema/beans"下面,xml文档如需要导入当前文档的规则,就可以指定这个命名空间
   属性xmlns="http://www.springframework.org/schema/beans"表示在当前文档中导入"http://www.springframework.org/schema/beans"命名空间下所描述的规则(即当前文档本身描述的规则),并且无需使用前缀,也即默认命名空间,这样,在当前文档就可以直接引用所定义的元素了
-->
</xsd:schema>

xsd已经通过了targetNamespace规定了schema的命名空间的引用,所以哪怕之前说xmlns可以随便命名,在实际中也是不行的

上一篇 下一篇

猜你喜欢

热点阅读