ssm框架的xml文档的schema约束的学习

1、XML Schema的作用

(1)先说说XML DTD是什么?

DTD 的目的是定义 XML 文档的结构。如下:带有 DTD 的 XML 文档实例,DTD描述了这个XML文档,可以用来验证XML的语法。

 

(2)XML Schema又是什么?

一种基于 XML 的 DTD 代替者,它名为 XML Schema;

(3)一览究竟

一个简单的XML文档:

<?xml version="1.0"?>
<note>
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
</note>

DTD 文件,对上面的XML文档如何定义:

<!ELEMENT note (to, from, heading, body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>


翻译:

第 1 行定义 note 元素有四个子元素:"to, from, heading, body"。

第 2-5 行定义了 to, from, heading, body 元素的类型是 "#PCDATA"。

 

XML Schema文件,又如何定义上面的XML文档:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3school.com.cn"
xmlns="http://www.w3school.com.cn"
elementFormDefault="qualified">

<xs:element name="note">
    <xs:complexType>
      <xs:sequence>
	<xs:element name="to" type="xs:string"/>
	<xs:element name="from" type="xs:string"/>
	<xs:element name="heading" type="xs:string"/>
	<xs:element name="body" type="xs:string"/>
      </xs:sequence>
    </xs:complexType>
</xs:element>

</xs:schema>


解释:
    xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    显示 schema 中用到的元素和数据类型来自命名空间 "http://www.w3.org/2001/XMLSchema"。
    同时它还规定了来自命名空间 "http://www.w3.org/2001/XMLSchema" 的元素和数据类型应该使用前缀 xs:
    
    targetNamespace="http://www.w3school.com.cn"
    显示被此 schema 定义的元素 (note, to, from, heading, body) 来自命名空间: "http://www.w3school.com.cn"。

    
    xmlns="http://www.w3school.com.cn"
    指出默认的命名空间是 "http://www.w3school.com.cn"。


    elementFormDefault="qualified"
    指出任何 XML 实例文档所使用的且在此 schema 中声明过的元素必须被命名空间限定。


(4)在 XML 文档中引用 Schema

<?xml version="1.0"?>

<note xmlns="http://www.w3school.com.cn"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3school.com.cn note.xsd">

<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
</note>


解释:
    xmlns="http://www.w3school.com.cn"
    规定了默认命名空间的声明。此声明会告知 schema 验证器,
    在此 XML 文档中使用的所有元素都被声明于 "http://www.w3school.com.cn" 这个命名空间。

    
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    XML Schema 实例命名空间 当有了这个实例命名空间

    您就可以使用 schemaLocation 属性了。此属性有两个值。第一个值是需要使用的命名空间。
    第二个值是供命名空间使用的 XML schema 的位置:
    xsi:schemaLocation="http://www.w3school.com.cn note.xsd
    

2、以spring.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:context="http://www.springframework.org/schema/context"
	

    //先声明命名空间,然后空格 然后指定配置实例
    xsi:schemaLocation="
    				http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
         	        http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-4.3.xsd
                       " >
	 <!-- 自动扫面service层下的所有类,自动实例化 -->
	 <context:component-scan base-package="service"></context:component-scan>            
</beans>

(1)先简化上面的配置进行分析


<beans>
	 <context:component-scan base-package="service"></context:component-scan>            
</beans>

(2)发现有两种标签

<beans>没有前缀;<context:component-scan>有前缀;

(3)命名空间

XML 中标签名称是由开发者定义的,当两个标签使用相同的名称时,就会命名冲突;

XML 中使用前缀来避免命名冲突;

比如:<context:component-scan> 或者 <f:table>

命名空间的作用也是避免元素命名冲突;就是使用前缀的升级版

xmlns全称就是XML name space;

如:<f:table xmlns:f="http://www.w3school.com.cn/furniture">

与仅仅使用前缀不同,我们为 <table> 标签添加了一个 xmlns 属性,这样就为前缀赋

予了一个与某个命名空间相关联的限定名称。

(4)默认的命名空间

前面的命名空间是避免命名冲突,但是在写子元素时,要加前缀,比如:

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

默认命名空间,子元素就不用写前缀了,比如:

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

那么在(2)中的<beans>使用的就是默认命名空间,<context:component-scan>使用的是命名空间;

(5)逐句击破

xmlns="http://www.springframework.org/schema/beans"

xmlns:context="http://www.springframework.org/schema/context"

表示beans在默认命名空间中,而context:作为前缀的元素在默认命名空间下的context命名空间中

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

xsi已经成为了一个业界默认的用于 XSD((XML Schema Definition) 文件的命名空间。 而 XSD 文件(也常常称为 Schema 文件)是用来定义 xml 文档结构的。

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

xsi命名空间下 schemaLocation 元素的值为一个由空格分开的键值对

猜你喜欢

转载自blog.csdn.net/Carl_changxin/article/details/81943244
今日推荐