schema详解

来源:互联网 发布:单片机gpio是什么意思 编辑:程序博客网 时间:2024/04/30 09:15

一、介绍

XML Schema是W3C制定的基于XML格式的XML文档结构描述标准。作为一种文档描述语言,通常我们将其简写为XSD(XML Schema Define)。XSD作为DTD(文档类型定义)的替代者,已经广泛地应用到各种商业应用。使用XSD,我们不仅可以描述XML文档的结构以便颁布业务标准,而且可以使用支持XSD的通用化XML解析器对XML文档进行解析并自动地检查其是否满足给定的业务标准。应用XSD校验XML文档的结构后,我们不仅验证了XML文档的有效性(Well-Formed Document),还验证了XML文档的合法性,甚至验证了XML文档各域的值合法性(数据类型与编码值),而且这些验证工作不必我们编写任何代码,只需使用支持XSD的通用化XML文档解析器即可完成。这就给应用软件带来了巨大的灵活性,以前需要借助数据库或配置文件才能完成的参数化管理,现在只需按照新的业务需求发布新的XML Schema即可。

二、简单案例

1. 一个简单的schema文件以及与其对应的有效的XML文档:
schema文件:
<?xml version="1.0" encoding="UTF-8"?><schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.xxz.org/user"xmlns:tns="http://www.xxz.org/user" elementFormDefault="qualified"><element name="user"><complexType><sequence><element name="id" type="int"/><element name="username" type="string"/><element name="birthday" type="date"/></sequence></complexType></element></schema>

xml文件:
<?xml version="1.0" encoding="UTF-8"?><user xmlns="http://www.xxz.org/user"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://www.xxz.org/user"><id>1</id><username>zhangsan</username><birthday>1990-12-12</birthday></user>

三、详细分析

1. schema文件的结构:


2. 在xml文件中如何引入schema文件:
第一种方式:

在eclipse中如何增加xml的category:


第二种方式:

四、属性的分析

1. 我们再写一个books的schema如下:
<?xml version="1.0" encoding="UTF-8"?><schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.npf.org/books"xmlns:tns="http://www.npf.org/books" elementFormDefault="qualified"><element name="books"><complexType><sequence minOccurs="1" maxOccurs="unbounded"><element name="book"><complexType><sequence ><element name="title" type="string"/><element name="content" type="string"/><choice><element name="author" type="string"/><element name="authors"><complexType><sequence minOccurs="1" maxOccurs="unbounded"><element name="author" type="string"/></sequence></complexType></element></choice></sequence></complexType></element></sequence></complexType></element></schema>
说明如下:
(1). minOccurs="1" maxOccurs="unbounded", 说明这个sequence最少出现1次,最多出现的次数无限制。
(2). 一本书的author可能有多个,那么我们就使用了choice标签,它表示使用里面的其中一个。
(3). 我们使用的sequence是有顺序的。必须严格按照定义的顺序出现在xml文件中。
(4). 如果我们不想使用有顺序的,那么使用all标签来代替sequence标签。但是使用all标签的话,就限定了每个子元素都只能出现一次。
2. 我们写一个books的xml如下:
<?xml version="1.0" encoding="UTF-8"?><books xmlns="http://www.npf.org/books"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:noNamespaceSchemaLocation="books.xsd"><book><title>Java in action</title><content>very good</content><author>npf</author></book><book><title>Spring in action</title><content>very good</content><authors><author>xxz</author><author>npf</author></authors></book></books>
3. 我们写一个带有属性的schema如下:

4. 我们写一个带有属性的xml如下:
<?xml version="1.0" encoding="UTF-8"?><books xmlns="http://www.npf.org/books"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:noNamespaceSchemaLocation="books.xsd"><book id="1"><title>Java in action</title><content>very good</content><author>npf</author></book><book id="2"><title>Spring in action</title><content>very good</content><authors><author>xxz</author><author>npf</author></authors></book></books>

0 0
原创粉丝点击