手工编写一个XML Schema(XSD)的实例
简单说:xml Schema就是xml的一个class,也相当于关系表的表结构模式。目前大都可以由程序根据已有的xml自动生成它的xsd,或者根据关系表生成它相应的xsd(实际就是将关系数据转换成xml数据,Vs2005中的类型化的Dataset就是这样实现的)。
对xml Schema做一些简单了解还是很有益处的。
XML Schema用于描述XML文档结构的文件。XML Schema有时又称为XML Schema Definition,所以也会称为XSD。因此Schema文件是以xsd为后缀的。
- 定义可以出现在文档中的Element。
- 定义可以出现在文档中的Attribute。
- 定义哪些Element是子元素。
- 定义Element的顺序。
- 定义子元素的数目。
- 定义一个元素是否能为空,是否能包含文本。
- 为每个Element和Attribute定义数据类型。
- 为Element和Attribute定义默认值和固定值。
对XML文档的验证有如下几个步骤:
1.定义一份XML Schema或DTD: Schema或DTD为同一类型的所有文档定义了结构标准,比如有关书籍信息的文档可共享一份标准。
2.应用Schema或DTD:一般由文档的作者指定Schema,这样接收方可以很快地根据说明对文档进行验证。但在C/S(客户机/服务器)环境中,由于服务器无法判断XML在客户端是否已经验证过,所以为了能够确保验证过程,也为了避免可能的重复验证,对文档应用Schema或DTD的操作由服务器完成。
3.运行解析器进行验证:解析器在装载文档的同时扫描结构,遇到任何错误都会终止装载过程,并做出适当的响应。
以下是一个xml与它相应的xml schema的实例。
给定xml (project_4.xml)文件如下:
<
UWO
>
< Departments >
< Department >
< Name > Economics </ Name >
< Address > Social Science Centre, UWO, London, Ontario, Canada , N6A 5C2 </ Address >
< Location > SSC 4071 </ Location >
< Phone > 519 661-3500 x83500 </ Phone >
< Facsimile > 519 661-3666 x83666 </ Facsimile >
< Email > economics@uwo.ca </ Email >
< WEB > http://economics.uwo.ca </ WEB >
</ Department >
< Department >
< Name > Political_science </ Name >
< Address > Social Science Centre, UWO, London, Ontario, Canada , N6A 5C2 </ Address >
< Location > SSC 4154 </ Location >
< Phone > 519 661-3266 x83266 </ Phone >
< Facsimile > 519 661-3904 x83904 </ Facsimile >
< Email > polisci-web@uwo.ca </ Email >
< WEB > http://politicalscience.uwo.ca </ WEB >
< SERVICES >
< Service >
< Name > Local_Government_program </ Name >
< Phone > 519 661-2111 x80501 </ Phone >
< Location > SSC 4148 </ Location >
< WEB > http://localgovernment.uwo.ca </ WEB >
</ Service >
< Service >
< Name > Politics_020E_course_Coordinator </ Name >
< Phone > 519 661-2111 x85108 </ Phone >
< Location > ssc 4149 </ Location >
</ Service >
</ SERVICES >
</ Department >
</ Departments >
</ UWO >
< Departments >
< Department >
< Name > Economics </ Name >
< Address > Social Science Centre, UWO, London, Ontario, Canada , N6A 5C2 </ Address >
< Location > SSC 4071 </ Location >
< Phone > 519 661-3500 x83500 </ Phone >
< Facsimile > 519 661-3666 x83666 </ Facsimile >
< Email > economics@uwo.ca </ Email >
< WEB > http://economics.uwo.ca </ WEB >
</ Department >
< Department >
< Name > Political_science </ Name >
< Address > Social Science Centre, UWO, London, Ontario, Canada , N6A 5C2 </ Address >
< Location > SSC 4154 </ Location >
< Phone > 519 661-3266 x83266 </ Phone >
< Facsimile > 519 661-3904 x83904 </ Facsimile >
< Email > polisci-web@uwo.ca </ Email >
< WEB > http://politicalscience.uwo.ca </ WEB >
< SERVICES >
< Service >
< Name > Local_Government_program </ Name >
< Phone > 519 661-2111 x80501 </ Phone >
< Location > SSC 4148 </ Location >
< WEB > http://localgovernment.uwo.ca </ WEB >
</ Service >
< Service >
< Name > Politics_020E_course_Coordinator </ Name >
< Phone > 519 661-2111 x85108 </ Phone >
< Location > ssc 4149 </ Location >
</ Service >
</ SERVICES >
</ Department >
</ Departments >
</ UWO >
对该xml大致分析,基本确定它的模式信息,开始编写XSD文件:
project.xsd
<?
xml version="1.0"
?>
< xsd:schema xmlns:xsd ="http://www.w3.org/2001/XMLSchema" targetNamespace ="http://bbs.xml.org.cn" xmlns ="http://bbs.xml.org.cn" elementFormDefault ="qualified" >
< xsd:element name ="UWO" >
< xsd:complexType >
< xsd:sequence >
< xsd:element name ="Departments" >
< xsd:complexType >
< xsd:sequence >
< xsd:element name ="Department" type ="departmentType" maxOccurs ="unbounded" /> <!-- 调用后面定义的departmentType类型 -->
</ xsd:sequence >
</ xsd:complexType >
</ xsd:element >
</ xsd:sequence >
</ xsd:complexType >
</ xsd:element >
< xsd:group name ="departmentBasic" > <!-- 定义一个group -->
< xsd:sequence >
< xsd:element name ="Name" type ="xsd:string" />
< xsd:element name ="Address" type ="xsd:string" />
< xsd:element name ="Location" type ="xsd:string" />
< xsd:element name ="Phone" type ="xsd:string" />
< xsd:element name ="Facsimile" type ="xsd:string" />
< xsd:element name ="Email" type ="xsd:string" />
< xsd:element name ="WEB" type ="xsd:string" />
</ xsd:sequence >
</ xsd:group >
< xsd:complexType name ="departmentType" > <!-- 定义departmentType类型 -->
< xsd:sequence >
< xsd:group ref ="departmentBasic" /> <!-- 引用前面定义的group -->
< xsd:element name ="SERVICES" maxOccurs ="1" minOccurs ="0" type ="servicesType" /> <!-- 调用后面定义的servicesType类型 -->
</ xsd:sequence >
</ xsd:complexType >
< xsd:complexType name ="servicesType" > <!-- 定义servicesType类型 -->
< xsd:sequence >
< xsd:element name ="Service" maxOccurs ="unbounded" >
< xsd:complexType >
< xsd:sequence >
< xsd:element name ="Name" type ="xsd:string" />
< xsd:element name ="Phone" type ="xsd:string" />
< xsd:element name ="Location" type ="xsd:string" />
< xsd:element name ="WEB" type ="xsd:string" minOccurs ="0" />
</ xsd:sequence >
</ xsd:complexType >
</ xsd:element >
</ xsd:sequence >
</ xsd:complexType >
</ xsd:schema >
< xsd:schema xmlns:xsd ="http://www.w3.org/2001/XMLSchema" targetNamespace ="http://bbs.xml.org.cn" xmlns ="http://bbs.xml.org.cn" elementFormDefault ="qualified" >
< xsd:element name ="UWO" >
< xsd:complexType >
< xsd:sequence >
< xsd:element name ="Departments" >
< xsd:complexType >
< xsd:sequence >
< xsd:element name ="Department" type ="departmentType" maxOccurs ="unbounded" /> <!-- 调用后面定义的departmentType类型 -->
</ xsd:sequence >
</ xsd:complexType >
</ xsd:element >
</ xsd:sequence >
</ xsd:complexType >
</ xsd:element >
< xsd:group name ="departmentBasic" > <!-- 定义一个group -->
< xsd:sequence >
< xsd:element name ="Name" type ="xsd:string" />
< xsd:element name ="Address" type ="xsd:string" />
< xsd:element name ="Location" type ="xsd:string" />
< xsd:element name ="Phone" type ="xsd:string" />
< xsd:element name ="Facsimile" type ="xsd:string" />
< xsd:element name ="Email" type ="xsd:string" />
< xsd:element name ="WEB" type ="xsd:string" />
</ xsd:sequence >
</ xsd:group >
< xsd:complexType name ="departmentType" > <!-- 定义departmentType类型 -->
< xsd:sequence >
< xsd:group ref ="departmentBasic" /> <!-- 引用前面定义的group -->
< xsd:element name ="SERVICES" maxOccurs ="1" minOccurs ="0" type ="servicesType" /> <!-- 调用后面定义的servicesType类型 -->
</ xsd:sequence >
</ xsd:complexType >
< xsd:complexType name ="servicesType" > <!-- 定义servicesType类型 -->
< xsd:sequence >
< xsd:element name ="Service" maxOccurs ="unbounded" >
< xsd:complexType >
< xsd:sequence >
< xsd:element name ="Name" type ="xsd:string" />
< xsd:element name ="Phone" type ="xsd:string" />
< xsd:element name ="Location" type ="xsd:string" />
< xsd:element name ="WEB" type ="xsd:string" minOccurs ="0" />
</ xsd:sequence >
</ xsd:complexType >
</ xsd:element >
</ xsd:sequence >
</ xsd:complexType >
</ xsd:schema >
如果使用该XSD验证以上xml文件,并且这两个文件在同一目录下,则只要如下修改xml文件:
<?
xml version="1.0"
?>
< UWO xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation ="http://bbs.xml.org.cn project.xsd" xmlns ="http://bbs.xml.org.cn" >
< UWO xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation ="http://bbs.xml.org.cn project.xsd" xmlns ="http://bbs.xml.org.cn" >
以上大号字体则是验证模式文件的路径。
参考: http://www.w3school.com.cn/schema/index.asp