Schema

前端之家收集整理的这篇文章主要介绍了Schema前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Schema

XML Schema是基于XML的DTD替代者,用于描述XML文档的结构。XMLSchema支持命名空间,基本数据类型和复杂数据类型,比DTD更具优势。

1、 命名空间

一个XML文件可以包含多个XMLSchema文件,不同的schema文件难免会出现重名的元素。为了解决该问题,Schema提供了命名空间,只要在使用元素时加上命名空间,就可以避免不同schema元素重名的问题,如下

<!—sop.xsd -->@H_404_10@

<?@H_404_10@xml@H_404_10@ version@H_404_10@="1.0"@H_404_10@ encoding@H_404_10@="UTF-8"@H_404_10@?>@H_404_10@

<@H_404_10@schema@H_404_10@ xmlns@H_404_10@="http://www.w3.org/2001/XMLSchema"@H_404_10@

targetNamespace@H_404_10@="http://www.ssl.org/aop"@H_404_10@

xmlns:tns@H_404_10@="http://www.ssl.org/aop"@H_404_10@

elementFormDefault@H_404_10@="qualified"@H_404_10@>@H_404_10@

<@H_404_10@element@H_404_10@ name@H_404_10@="bean"@H_404_10@>@H_404_10@

<@H_404_10@complexType@H_404_10@>@H_404_10@

<@H_404_10@sequence@H_404_10@>@H_404_10@

<@H_404_10@element@H_404_10@ name@H_404_10@="class"@H_404_10@type@H_404_10@="string"@H_404_10@/>@H_404_10@

</@H_404_10@sequence@H_404_10@>@H_404_10@

</@H_404_10@complexType@H_404_10@>@H_404_10@

</@H_404_10@element@H_404_10@>@H_404_10@

</@H_404_10@schema@H_404_10@>@H_404_10@

<!—spring.xsd -->@H_404_10@

<?@H_404_10@xml@H_404_10@ version@H_404_10@="1.0"@H_404_10@ encoding@H_404_10@="UTF-8"@H_404_10@?>@H_404_10@

<@H_404_10@schema@H_404_10@ xmlns@H_404_10@="http://www.w3.org/2001/XMLSchema"@H_404_10@

targetNamespace@H_404_10@="http://www.ssl.com/spring"@H_404_10@

xmlns:tns@H_404_10@="http://www.ssl.com/spring"@H_404_10@

elementFormDefault@H_404_10@="qualified"@H_404_10@>@H_404_10@

<@H_404_10@element@H_404_10@ name@H_404_10@="bean"@H_404_10@>@H_404_10@

<@H_404_10@complexType@H_404_10@>@H_404_10@

<@H_404_10@sequence@H_404_10@>@H_404_10@

<@H_404_10@element@H_404_10@ name@H_404_10@="class"@H_404_10@type@H_404_10@="string"@H_404_10@/>@H_404_10@

</@H_404_10@sequence@H_404_10@>@H_404_10@

</@H_404_10@complexType@H_404_10@>@H_404_10@

</@H_404_10@element@H_404_10@>@H_404_10@

</@H_404_10@schema@H_404_10@>@H_404_10@

上面的aop.xsd、spring.xsd都定义了bean元素,若在同一个XML文件中使用上面两个schema定义的bean元素,此时需要在引入schema时添加命名空间,如下

<?xml version="1.0" encoding="UTF-8"?>@H_404_10@

<beans xmlns:spring=@H_404_10@"http://www.ssl.com/spring"@H_404_10@

xmlns:aop=@H_404_10@"http://www.ssl.com/aop"@H_404_10@

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

xsi:schemaLocation=@H_404_10@"http://www.ssl.com/spring spring.xsd@H_404_10@

http://www.ssl.com/aop aop.xsd">@H_404_10@

@H_404_10@

<spring:bean>@H_404_10@com.ssl.User</spring:bean>@H_404_10@

<aop:bean>@H_404_10@com.ssl.Admin</aop:bean>@H_404_10@

</beans>@H_404_10@

可以在根元素的属性字段中,通过xmlns来引入schema文件,在引入schema文件时,可以通过xmlns:prefixName来指定命名空间的前缀,在使用该命名空间内的元素时,需要在元素前添加前缀。

在引入多个@H_404_10@schema@H_404_10@文件时,只有一个命名空间可以不添加前缀,该命名空间为默认命名空间@H_404_10@。

2、 Schema文件

新建Schema文件时,在根元素schema中总有如下属性:

<@H_404_10@schema@H_404_10@ xmlns@H_404_10@="http://www.w3.org/2001/XMLSchema"@H_404_10@

targetNamespace@H_404_10@="http://www.ssl.com/person"@H_404_10@

xmlns:tns@H_404_10@="http://www.ssl.com/person"@H_404_10@

elementFormDefault@H_404_10@="qualified"@H_404_10@>@H_404_10@

</@H_404_10@schema@H_404_10@>@H_404_10@

(1)其中http://www.w3.org/2001/XMLSchema@H_404_10@为标准命名空间,该命名空间内定义了定义schema文件所用的元素和数据类型;

(2)targetNamespace表明schema被xml引入的命名空间;

(3)elementForDefault指出任何实例文档所使用本schema中声明的元素必须被命名空间限定;

3、 元素类型

在schema中可以使用的基本数据类型有int、interger、decimal、boolean、string、date、time。

在schema中,可以直接使用基本数据类型来声明简单元素类型,如下

<@H_404_10@element@H_404_10@ name@H_404_10@="id"@H_404_10@ type@H_404_10@="int"@H_404_10@/>@H_404_10@

<@H_404_10@element@H_404_10@ name@H_404_10@="name"@H_404_10@type@H_404_10@="string"@H_404_10@/>@H_404_10@

<@H_404_10@element@H_404_10@ name@H_404_10@="age"@H_404_10@ type@H_404_10@="int"@H_404_10@/>@H_404_10@

也可以由简单元素组成复杂元素,任何复杂的元素类型都是由简单的元素类型组合而成的,如下

<@H_404_10@complexType@H_404_10@ name@H_404_10@="user"@H_404_10@>@H_404_10@

<@H_404_10@sequence@H_404_10@>@H_404_10@

<@H_404_10@element@H_404_10@ name@H_404_10@="id"@H_404_10@ type@H_404_10@="int"@H_404_10@/>@H_404_10@

<@H_404_10@element@H_404_10@ name@H_404_10@="name"@H_404_10@type@H_404_10@="string"@H_404_10@/>@H_404_10@

<@H_404_10@element@H_404_10@ name@H_404_10@="age"@H_404_10@ type@H_404_10@="int"@H_404_10@/>@H_404_10@

</@H_404_10@sequence@H_404_10@>@H_404_10@

</@H_404_10@complexType@H_404_10@>@H_404_10@

复杂元素是由简单元素或其他复杂元素合成的,那么组合的规则是什么呢?在声明复杂元素时可以使用all、choice、sequence来嵌套子元素,以表明组合规则,具体如下

lall

规定子元素可以按照任意顺序出现,但每个子元素只能出现一次;

lchioce

规定可出现某个子元素或者另一个子元素,非此即彼,如下

<@H_404_10@xs:element@H_404_10@ name@H_404_10@="person"@H_404_10@>@H_404_10@

<@H_404_10@xs:complexType@H_404_10@>@H_404_10@

<@H_404_10@xs:choice@H_404_10@>@H_404_10@

<@H_404_10@xs:element@H_404_10@ name@H_404_10@="employee"@H_404_10@type@H_404_10@="employee"@H_404_10@/>@H_404_10@

<@H_404_10@xs:element@H_404_10@ name@H_404_10@="member"@H_404_10@type@H_404_10@="member"@H_404_10@/>@H_404_10@

</@H_404_10@xs:choice@H_404_10@>@H_404_10@

</@H_404_10@xs:complexType@H_404_10@>@H_404_10@

</@H_404_10@xs:element@H_404_10@>@H_404_10@

lsequence

规定子元素必须按照特定的顺序出现,如下

<@H_404_10@element@H_404_10@ name@H_404_10@="user"@H_404_10@>@H_404_10@

<@H_404_10@complexType@H_404_10@>@H_404_10@

<@H_404_10@sequence@H_404_10@>@H_404_10@

<@H_404_10@element@H_404_10@ name@H_404_10@="id"@H_404_10@ type@H_404_10@="int"@H_404_10@/>@H_404_10@

<@H_404_10@element@H_404_10@ name@H_404_10@="name"@H_404_10@type@H_404_10@="string"@H_404_10@/>@H_404_10@

<@H_404_10@element@H_404_10@ name@H_404_10@="age"@H_404_10@ type@H_404_10@="int"@H_404_10@/>@H_404_10@

</@H_404_10@sequence@H_404_10@>@H_404_10@

</@H_404_10@complexType@H_404_10@>@H_404_10@

</@H_404_10@element@H_404_10@>@H_404_10@

复杂元素是由简单元素或其他复杂元素合成的,那么组合的规则是什么呢?在声明复杂元素时可以使用all、choice、sequence来嵌套子元素,以表明组合规则,具体如下

此外,有些元素只允许出现一次,而有些元素可以出现多次;可以在choice、sequence、element等子元素上添加minOccurs和maxOccurs来指明允许元素出现的次数,如下

<?@H_404_10@xml@H_404_10@ version@H_404_10@="1.0"@H_404_10@ encoding@H_404_10@="UTF-8"@H_404_10@?>@H_404_10@

<@H_404_10@schema@H_404_10@ xmlns@H_404_10@="http://www.w3.org/2001/XMLSchema"@H_404_10@

targetNamespace@H_404_10@="http://www.ssl.com/books"@H_404_10@

xmlns:tns@H_404_10@="http://www.ssl.com/books"@H_404_10@

elementFormDefault@H_404_10@="qualified"@H_404_10@>@H_404_10@

<@H_404_10@element@H_404_10@ name@H_404_10@="books"@H_404_10@>@H_404_10@

<@H_404_10@complexType@H_404_10@>@H_404_10@

<@H_404_10@sequence@H_404_10@ minOccurs@H_404_10@="1"@H_404_10@ maxOccurs@H_404_10@="unbounded"@H_404_10@>@H_404_10@@H_404_10@

<@H_404_10@element@H_404_10@ name@H_404_10@="book"@H_404_10@>@H_404_10@

<@H_404_10@complexType@H_404_10@>@H_404_10@

<@H_404_10@sequence@H_404_10@>@H_404_10@

<@H_404_10@element@H_404_10@ name@H_404_10@="id"@H_404_10@ type@H_404_10@="int"@H_404_10@/>@H_404_10@

<@H_404_10@element@H_404_10@ name@H_404_10@="name"@H_404_10@ type@H_404_10@="string"@H_404_10@/>@H_404_10@

<@H_404_10@element@H_404_10@ name@H_404_10@="author"@H_404_10@type@H_404_10@="string"@H_404_10@/>@H_404_10@

</@H_404_10@sequence@H_404_10@>@H_404_10@

</@H_404_10@complexType@H_404_10@>@H_404_10@

</@H_404_10@element@H_404_10@>@H_404_10@

</@H_404_10@sequence@H_404_10@>@H_404_10@

</@H_404_10@complexType@H_404_10@>@H_404_10@

</@H_404_10@element@H_404_10@>@H_404_10@

</@H_404_10@schema@H_404_10@>@H_404_10@

4、 元素约束

在实际应用中,往往需要添加元素约束,如性别只能为男女,年龄范围,手机号、Emailed格式等。在schema中在定义元素时,可以添加元素约束,如可以添加范围、枚举、字符串长度、正则表达式等约束。在schema中只能为简单元素添加这些约束限制,如下

<?@H_404_10@xml@H_404_10@ version@H_404_10@="1.0"@H_404_10@ encoding@H_404_10@="UTF-8"@H_404_10@?>@H_404_10@

<@H_404_10@schema@H_404_10@ xmlns@H_404_10@="http://www.w3.org/2001/XMLSchema"@H_404_10@

targetNamespace@H_404_10@="http://www.ssl.com/user"@H_404_10@

xmlns:tns@H_404_10@="http://www.ssl.com/user"@H_404_10@

elementFormDefault@H_404_10@="qualified"@H_404_10@>@H_404_10@

<@H_404_10@element@H_404_10@ name@H_404_10@="user"@H_404_10@>@H_404_10@

<@H_404_10@complexType@H_404_10@>@H_404_10@

<@H_404_10@sequence@H_404_10@>@H_404_10@

<@H_404_10@element@H_404_10@ name@H_404_10@="id"@H_404_10@ type@H_404_10@="unsignedInt"@H_404_10@/>@H_404_10@

<@H_404_10@element@H_404_10@ name@H_404_10@="name"@H_404_10@ type@H_404_10@="tns:name"@H_404_10@/>@H_404_10@

<@H_404_10@element@H_404_10@ name@H_404_10@="age"@H_404_10@ type@H_404_10@="tns:age"@H_404_10@/>@H_404_10@

<@H_404_10@element@H_404_10@ name@H_404_10@="gender" @H_404_10@ type@H_404_10@="tns:gender"@H_404_10@/>@H_404_10@

</@H_404_10@sequence@H_404_10@>@H_404_10@

</@H_404_10@complexType@H_404_10@>@H_404_10@

</@H_404_10@element@H_404_10@>@H_404_10@

<@H_404_10@simpleType@H_404_10@ name@H_404_10@="age"@H_404_10@>@H_404_10@

<@H_404_10@restriction@H_404_10@ base@H_404_10@="int"@H_404_10@>@H_404_10@

<@H_404_10@minInclusive@H_404_10@ value@H_404_10@="16"@H_404_10@/>@H_404_10@

<@H_404_10@maxExclusive@H_404_10@ value@H_404_10@="150"@H_404_10@/>@H_404_10@

</@H_404_10@restriction@H_404_10@>@H_404_10@

</@H_404_10@simpleType@H_404_10@>@H_404_10@

<@H_404_10@simpleType@H_404_10@ name@H_404_10@="name"@H_404_10@>@H_404_10@

<@H_404_10@restriction@H_404_10@ base@H_404_10@="string"@H_404_10@>@H_404_10@

<@H_404_10@minLength@H_404_10@ value@H_404_10@="6"@H_404_10@/>@H_404_10@

<@H_404_10@maxLength@H_404_10@ value@H_404_10@="18"@H_404_10@/>@H_404_10@

</@H_404_10@restriction@H_404_10@>@H_404_10@

</@H_404_10@simpleType@H_404_10@>@H_404_10@

<@H_404_10@simpleType@H_404_10@ name@H_404_10@="gender"@H_404_10@>@H_404_10@

<@H_404_10@restriction@H_404_10@ base@H_404_10@="string"@H_404_10@>@H_404_10@

<@H_404_10@enumeration@H_404_10@ value@H_404_10@="@H_404_10@男@H_404_10@"@H_404_10@/>@H_404_10@

<@H_404_10@enumeration@H_404_10@ value@H_404_10@="@H_404_10@女@H_404_10@"@H_404_10@/>@H_404_10@

</@H_404_10@restriction@H_404_10@>@H_404_10@

</@H_404_10@simpleType@H_404_10@>@H_404_10@

</@H_404_10@schema@H_404_10@>@H_404_10@

5、 元素重用

在schema中定义元素,一般有3中方式,如下

l嵌套型

嵌套型,只有一个根元素,所有子元素都嵌套在根元素内,层次清楚;但同一个schema中可能会有大量重复元素,而不能很好的重用,如下

<@H_404_10@schema@H_404_10@ xmlns@H_404_10@="http://www.w3.org/2001/XMLSchema"@H_404_10@

targetNamespace@H_404_10@="http://www.ssl.com/books"@H_404_10@

xmlns:tns@H_404_10@="http://www.ssl.com/books"@H_404_10@

elementFormDefault@H_404_10@="qualified"@H_404_10@>@H_404_10@

<@H_404_10@element@H_404_10@ name@H_404_10@="books"@H_404_10@>@H_404_10@

<@H_404_10@complexType@H_404_10@>@H_404_10@

<@H_404_10@sequence@H_404_10@ minOccurs@H_404_10@="1"@H_404_10@maxOccurs@H_404_10@="unbounded"@H_404_10@>@H_404_10@

<@H_404_10@element@H_404_10@ name@H_404_10@="book"@H_404_10@>@H_404_10@

<@H_404_10@complexType@H_404_10@>@H_404_10@

<@H_404_10@sequence@H_404_10@>@H_404_10@

<@H_404_10@element@H_404_10@ name@H_404_10@="id"@H_404_10@ type@H_404_10@="int"@H_404_10@/>@H_404_10@

<@H_404_10@element@H_404_10@ name@H_404_10@="name"@H_404_10@ type@H_404_10@="string"@H_404_10@/>@H_404_10@

<@H_404_10@element@H_404_10@ name@H_404_10@="author"@H_404_10@type@H_404_10@="string"@H_404_10@/>@H_404_10@

</@H_404_10@sequence@H_404_10@>@H_404_10@

</@H_404_10@complexType@H_404_10@>@H_404_10@

</@H_404_10@element@H_404_10@>@H_404_10@

</@H_404_10@sequence@H_404_10@>@H_404_10@

</@H_404_10@complexType@H_404_10@>@H_404_10@

</@H_404_10@element@H_404_10@>@H_404_10@

</@H_404_10@schema@H_404_10@>@H_404_10@

l嵌套型+直线型

为了尽量使schema文件层次清楚和重复使用元素,应先定义可能会被重用的数据类型,如下

<@H_404_10@schema@H_404_10@ xmlns@H_404_10@="http://www.w3.org/2001/XMLSchema"@H_404_10@

targetNamespace@H_404_10@="http://www.ssl.com/books"@H_404_10@

xmlns:tns@H_404_10@="http://www.ssl.com/books"@H_404_10@

elementFormDefault@H_404_10@="qualified"@H_404_10@>@H_404_10@

<@H_404_10@element@H_404_10@ name@H_404_10@="books"@H_404_10@>@H_404_10@

<@H_404_10@complexType@H_404_10@>@H_404_10@

<@H_404_10@sequence@H_404_10@ minOccurs@H_404_10@="1"@H_404_10@maxOccurs@H_404_10@="unbounded"@H_404_10@>@H_404_10@

<@H_404_10@element@H_404_10@ name@H_404_10@="book"@H_404_10@type@H_404_10@="tns:book"@H_404_10@/>@H_404_10@

</@H_404_10@sequence@H_404_10@>@H_404_10@

</@H_404_10@complexType@H_404_10@>@H_404_10@

</@H_404_10@element@H_404_10@>@H_404_10@

<@H_404_10@complexType@H_404_10@ name@H_404_10@="id"@H_404_10@>@H_404_10@

<@H_404_10@sequence@H_404_10@>@H_404_10@

<@H_404_10@element@H_404_10@ name@H_404_10@="id"@H_404_10@type@H_404_10@="unsignedInt"@H_404_10@/>@H_404_10@

</@H_404_10@sequence@H_404_10@>@H_404_10@

</@H_404_10@complexType@H_404_10@>@H_404_10@

<@H_404_10@complexType@H_404_10@ name@H_404_10@="name"@H_404_10@>@H_404_10@

<@H_404_10@sequence@H_404_10@>@H_404_10@

<@H_404_10@element@H_404_10@ name@H_404_10@="name"@H_404_10@type@H_404_10@="string"@H_404_10@/>@H_404_10@

</@H_404_10@sequence@H_404_10@>@H_404_10@

</@H_404_10@complexType@H_404_10@>@H_404_10@

<@H_404_10@complexType@H_404_10@ name@H_404_10@="author"@H_404_10@>@H_404_10@

<@H_404_10@sequence@H_404_10@>@H_404_10@

<@H_404_10@element@H_404_10@ name@H_404_10@="firstName"@H_404_10@type@H_404_10@="string"@H_404_10@/>@H_404_10@

<@H_404_10@element@H_404_10@ name@H_404_10@="lastName"@H_404_10@type@H_404_10@="string"@H_404_10@/>@H_404_10@

</@H_404_10@sequence@H_404_10@>@H_404_10@

</@H_404_10@complexType@H_404_10@>@H_404_10@

<@H_404_10@complexType@H_404_10@ name@H_404_10@="book"@H_404_10@>@H_404_10@

<@H_404_10@sequence@H_404_10@>@H_404_10@

<@H_404_10@element@H_404_10@ name@H_404_10@="id"@H_404_10@type@H_404_10@="tns:id"@H_404_10@/>@H_404_10@

<@H_404_10@element@H_404_10@ name@H_404_10@="name"@H_404_10@type@H_404_10@="tns:name"@H_404_10@/>@H_404_10@

<@H_404_10@element@H_404_10@ name@H_404_10@="author"@H_404_10@type@H_404_10@="tns:author"@H_404_10@/>@H_404_10@

</@H_404_10@sequence@H_404_10@>@H_404_10@

</@H_404_10@complexType@H_404_10@>@H_404_10@

</@H_404_10@schema@H_404_10@>@H_404_10@

猜你在找的XML相关文章