1 Schema概述
1.1 什么是Schema
l Schema是新的XML文檔約束;DTD出現的比較早.
l Schema要比DTD強大很多;
l Schema本身也是XML文檔,但Schema文檔的擴展名為xsd,而不是xml。( XML Schemas Definition )(xmlns : XML namespace)
1.2 Schema簡介
本課程中不對Schema深入探討,我們只對Schema有個了解即可。
students.xsd
<?xml version="1.0"?> <xsd:schema xmlns="http://www.itcast.cn/xml" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.itcast.cn/xml" elementFormDefault="qualified"> 指定目標名稱空間名稱 <xsd:element name="students" type="studentsType"/> 定義students元素,類型為studentsType類型 <xsd:complexType name="studentsType"> 定義studentsType類型 <xsd:sequence> <xsd:element name="student" type="studentType" minOccurs="0" maxOccurs="unbounded"/> studentsType類型包含0~N個student元素 </xsd:sequence> </xsd:complexType> <xsd:complexType name="studentType"> 定義studentType類型 <xsd:sequence> 子元素必須按順序出現 <xsd:element name="name" type="xsd:string"/> studentType類型順序包含子元素name,類型為字符串 <xsd:element name="age" type="ageType" /> studentType類型順序包含子元素age,類型為ageType <xsd:element name="sex" type="sexType" /> studentType類型順序包含子元素sex,類型為sexType </xsd:sequence> <xsd:attribute name="number" type="numberType" use="required"/> studentType類型包含屬性number,類型為numberType,該屬性是必須的 </xsd:complexType> <xsd:simpleType name="sexType"> 定義類型sexType <xsd:restriction base="xsd:string"> 該類型是對string進行約束 <xsd:enumeration value="male"/> 指定枚舉選項 <xsd:enumeration value="female"/> 指定枚舉選項 </xsd:restriction> </xsd:simpleType> <xsd:simpleType name="ageType"> 定義ageType類型 <xsd:restriction base="xsd:integer"> 該類型對integer進行約束 <xsd:minInclusive value="0"/> <xsd:maxInclusive value="120"/> 范圍為0~120 </xsd:restriction> </xsd:simpleType> <xsd:simpleType name="numberType"> 定義numberType類型 <xsd:restriction base="xsd:string"> 對string類型進行約束 <xsd:pattern value="ITCAST_\d{4}"/> 指定正則表達式 </xsd:restriction> </xsd:simpleType> </xsd:schema>
students.xml
<?xml version="1.0"?> <students xmlns="http://www.itcast.cn/xml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.itcast.cn/xml students.xsd">指定默認名稱空間,指定Schema約束文件 <student number="ITCAST_1001"> 子元素student,其中number屬性是必須的,而且number屬性值必須匹配正則表達式ITCAST_\d{4} <name>zhangSan</name> 子元素name,內容為任意字符串 <age>20</age> 子元素age,內容必須是0~100之間的整數 <sex>male</sex> 子元素sex,內容必須是male和female其中之一。 </student> <student number="ITCAST_1002"> <name>liSi</name> <age>25</age> <sex>female</sex> </student> </students>
2 Schema名稱空間
2.1 什么是名稱空間
如果一個XML文檔中使用多個Schema文件,而這些Schema文件中定義了相同名稱的元素時就會出現名字沖突。這就像一個Java文件中使用了import java.util.*和import java.sql.*時,在使用Date類時,那么就不明確Date是哪個包下的Date了。
總之名稱空間就是用來處理元素和屬性的名稱沖突問題,與Java中的包是同一用途。如果每個元素和屬性都有自己的名稱空間,那么就不會出現名字沖突問題,就像是每個類都有自己所在的包一樣,那么類名就不會出現沖突。
2.2 目標名稱空間
在XSD文件中為定義的元素指定名稱,即指定目標名稱空間。這需要給<xsd:schema>元素添加targetNamespace屬性。
l <xsd:schema targetNamespace="http://www.itcast.cn/xml">
名稱空間可以是任意字符串,但通常我們會使用公司的域名作為名稱空間,這與Java中的包名使用域名的倒序是一樣的!千萬不要以為這個域名是真實的,它可以是不存在的域名。
如果每個公司發布的Schema都隨意指定名稱空間,如a、b之類的,那么很可能會出現名稱空間的名字沖突,所以還是使用域名比較安全,因為域名是唯一的。
當使用了targetNamespace指定目標名稱空間后,那么當前XSD文件中定義的元素和屬性就在這個名稱空間之中了。
2.3 XML指定XSD文件
在XML文件中需要指定XSD約束文件,這需要使用在根元素中使用schemaLocation屬性來指定XSD文件的路徑,以及目標名稱空間。格式為:schemaLocation=”目標名稱空間 XSD文件路徑”
l <students schemaLocation="http://www.itcast.cn/xml students.xsd">
schemaLocation是用來指定XSD文件的路徑,也就是說為當前XML文檔指定約束文件。但它不只要指定XSD文件的位置,還要指定XSD文件的目標名稱空間。
其中http://www.itcast.cn/xml為目標名稱空間,students.xsd為XSD文件的位置,它們中間使用空白符(空格或換行)分隔。
也可以指定多個XSD文件,格式為:
l schemaLocation=”目標名稱空間1 XSD文件路徑1 目標名稱空間2 XSD文件路徑2”
下面是spring配置文件的例子,它一共指定兩個XSD文件
<?xml version="1.0" encoding="UTF-8"?> <beans xsi:schemaLocation="http://www.springframework.org/schema/beans 第一個XSD文件的目標名稱空間 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 第一個xsd文件的位置,這個網址必須是真實的,不然就找不到xsd文件了. http://www.springframework.org/schema/aop 第二個xsd文件的目標名稱空間 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "> 第二個xsd文件的位置. </beans>
xsi:schemaLocation="http://www.springframework.org/schema/beans 第一個XSD文件的目標名稱空間
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 第一個XSD文件的位置,這個網址必須是真實的,不然就找不到XSD文件了。
http://www.springframework.org/schema/aop 第二個XSD文件的目標名稱空間
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 第二個XSD文件的位置
下面是JavaWeb項目的配置文件:
<?xml version="1.0" encoding="UTF-8"?> <web-app xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> </web-app>
2.4 定義名稱空間
現在我們已經知道一個XML中可以指定多個XSD文件,例如上面Spring的配置文件中就指定了多個XSD文件,那么如果我在<beans>元素中給出一個子元素<bean>,你知道它是哪個名稱空間中的么?
<beans xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <bean></bean> </beans>
如果兩個名稱空間中都<bean>元素會怎么樣呢?
所以只是使用schemaLocation指定XSD是不夠的,它只是導入了這個XSD及XSD的名稱空間而已。schemaLocation的作用就相當於Java中導入Jar包的作用!最終還是在Java文件中使用import來指定包名的。
xmlns是用來指定名稱空間前綴的,所謂前綴就是“簡稱”,例如中華人發共和國簡稱中國一樣,然后我們在每個元素前面加上前綴,就可以處理名字沖突了。
格式為:xmln:前綴=”名稱空間”
注意,使用xmlns指定的名稱空間必須是在schemaLocation中存在的名稱空間。
<beans xmlns:b="http://www.springframework.org/schema/beans" 指定b前綴 xmlns:aop="http://www.springframework.org/schema/aop" 指定aop前綴 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <b:bean></b:bean>
在使用元素時,需指定前綴,這就可以區別元素是哪個名稱空間下的了。每個元素都要指定名稱空間前綴,如果不指定那么元素就是沒有名稱空間。 <aop:scoped-proxy/> </beans>
2.5 默認名稱空間
在一個XML文件中,可以指定一個名稱空間沒有前綴,那么在當前XML文檔中沒有前綴的元素就來自默認名稱空間。
<beans xmlns="http://www.springframework.org/schema/beans"
指定默認名稱空間,即沒有前綴的名稱空間,如果元素沒有指定前綴,那么表示來自這個名稱空間 xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <bean></bean>
來自默認名稱空間,因為沒有前綴。 <aop:scoped-proxy/> </beans>
2.6 W3C的元素和屬性
如果我們的XML文件中需要使用W3C提供的元素和屬性,那么可以不在schemaLocation屬性中指定XSD文件的位置,但一定要定義名稱空間,例如:
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 定義名稱控件,這個名稱空間來自w3c所以在下面的schemaLocation中無需指定 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> </beans>
上面定義了一個名稱空間,前綴為xsi,名稱空間為http://www.w3.org/2001/XMLSchema-instance。這個名稱空間無需在schemaLocation中不存在這個名稱空間。
你可能已經發現了,schemaLocation這個屬性其實是w3c定義的屬性,與元素一定,屬性也需要指定“出處”,xsi:schemaLocation中的xsi就是名稱空間前綴。也就是說,上面我們在沒有指定xsi名稱空間時,就直接使用schemaLocation是錯誤的。