系列专题
XML Schema快速入门(一)如何使用
XML Schema快速入门(二)语法之简单类型
XML Schema快速入门(三)语法之复杂类型
XML Schema语法
本章讲解XML Schema中各种元素、数据类型定义的语法。
XML Schema中数据类型可以按照简单类型和复杂类型的维度进行划分。
简单类型(simpleType):简易元素、属性
复杂类型(complexType):复合元素(空元素、仅包含子元素的元素、仅包含文本的元素、包含文本和子元素的混合元素)
1. 简易元素
简易元素指那些只包含文本的元素。它不会包含任何其他的元素或属性。
语法:
<xs:element name=“xxx” type=“yyy”/> // type本质是对simpleType的引用
此处 xxx 指元素的名称,yyy 指元素的数据类型。XML Schema 拥有很多内建的数据类型。
最常用的类型是(这些类型都是simpleType的数据类型):
- xs:string
- xs:decimal
- xs:integer
- xs:boolean
- xs:date
- xs:time
例子:
<lastname>Smith</lastname>
<age>28</age>
<dateborn>1980-03-27</dateborn>
这是相应的简易元素定义:
<xs:element name="lastname" type="xs:string"/>
<xs:element name="age" type="xs:integer"/>
<xs:element name="dateborn" type="xs:date"/>
简易元素可拥有指定的默认值或固定值。
当没有其他的值被规定时,默认值就会自动分配给元素。
在下面的例子中,缺省值是 “red”:
<xs:element name="color" type="xs:string" default="red"/>
固定值同样会自动分配给元素,并且您无法规定另外一个值。
在下面的例子中,固定值是 “red”:
<xs:element name="color" type="xs:string" fixed="red"/>
2. 属性
属性本身总是作为简易类型被声明的。
注意
:简易元素和属性都是作为简易类型申明的,他们是平级的,所以简易元素无法拥有属性。而复合类型,它是可以包含这些简易类型,所以一个元素要包含属性,只能通过复合类型去定义,这个后面会说。
定义属性的语法是:
<xs:attribute name=“xxx” type=“yyy”/>
在此处,xxx 指属性名称,yyy 则规定属性的数据类型。和简易元素一样都作为简易类型申明的,type可以是内置基本数据类型,或者simpleType的引用。
举例:
这是带有属性的 XML 元素:
<lastname lang="EN">Smith</lastname>
这是对应的属性定义:
<xs:attribute name="lang" type="xs:string"/>
属性可拥有指定的默认值或固定值。
当没有其他的值被规定时,默认值就会自动分配给元素。
在下面的例子中,缺省值是 “EN”:
<xs:attribute name="lang" type="xs:string" default="EN"/>
固定值同样会自动分配给元素,并且您无法规定另外的值。
在下面的例子中,固定值是 “EN”:
<xs:attribute name="lang" type="xs:string" fixed="EN"/>
可选的和必需的属性
在缺省的情况下,属性是可选的。如需规定属性为必选,请使用 “use” 属性:
<xs:attribute name="lang" type="xs:string" use="required"/>
3. <xsd:simpleType> 元素
schema规范中内置了很多简单类型,除了字符串,还有日期、数值、逻辑、base64Binary、十六进制、浮点、双精度、anyURI、anyURI 以及 NOTATION。这些类型的详细用法,可以点击这里。
除了内置的简单数据类型,我们还可以通过从现有简单类型(内置数据类型和派生的简单类型)派生来定义新的简单类型。简单类型不能包含元素并且不能具有属性。可以通过下列方式之一进行派生:
- restriction
限制派生:将简单类型的值的范围限制为继承的简单类型的那些值的子集。 - list
列表派生:定义一个简单类型,包含继承的简单类型的通过空白分隔的值列表。 - union
联合派生:定义一个简单类型,包含两个或多个继承的简单类型的值的联合。
定义新的简单类型,需要使用simpleType元素,先看下对simpleType元素的定义:
定义一个简单类型,确定与具有纯文本内容的属性或元素的值有关的信息以及对它们的约束。
语法:
<simpleType
final = (#all | (list | union | restriction))
id = ID
name = NCName
{any attributes with non-schema Namespace}…>
Content: (annotation?, (restriction | list | union))
</simpleType>
属性:
属性 | 值 |
final | 派生的类型。final 属性防止该 simpleType 元素的指定派生类型。该值可以包含 #all 或者一个列表,该列表是 list、union 或 restriction 的子集。
|
id | 该元素的 ID。id 值必须属于类型 ID 并且在包含该元素的文档中是唯一的。 可选项。 |
name | 类型名称。该名称必须是在 XML 命名空间规范中定义的无冒号名称 (NCName)。 如果指定,该名称在所有 simpleType 和 complexType 元素之间必须是唯一的。 如果 simpleType 元素是 schema 元素的子元素,则为必选项,在其他时候则是不必需的。 |
元素信息:
出现次数:无限制
父元素:attribute、element、list、restriction (simpleType)、schema、union
内容:annotation、list、restriction (simpleType)、union
3.1 限制派生: <xsd:restriction> 元素 (simpleType)
定义对 simpleType 定义的约束。
语法:
<restriction
base = QName
id = ID
{any attributes with non-schema Namespace}…>
Content: (annotation?, (simpleType?, (minExclusive | minInclusive |
maxExclusive | maxInclusive | totalDigits |fractionDigits | length |
minLength | maxLength | enumeration | whiteSpace | pattern)*))
</restriction>
属性:
属性 | 值 |
---|---|
base | 在该架构(或由指定的命名空间指示的其他架构)中定义的内置数据类型 simpleType 元素的名称。包含 restriction 元素的元素是从基值所指定的类型派生的。 基值必须是限定名 (QName)。 必选项。 |
id | 该元素的 ID。id 值必须属于类型 ID 并且在包含该元素的文档中是唯一的。 可选项。 |
元素信息:
出现次数:一次
父元素:simpleType
内容:annotation、fractionDigits、enumeration、length、maxExclusive、maxInclusive、maxLength、minExclusive、minInclusive、minLength、pattern、simpleType、totalDigits、whiteSpace
Restriction机制包括指出最大最小值、正则表达式、限制字符串的长度、限制十进制数的位数等。
下面的例子定义了带有一个限定且名为 “age” 的元素。age 的值不能低于 0 或者高于 120:
<xs:element name="age">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="120"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
下面的例子定义了带有一个限定的名为 “letter” 的元素。可接受的值是 a - z 中零个或多个字母:
<xs:element name="letter">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="([a-z])*"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
更多更详细例子请走传送门,
支持的限定:
限定 | 描述 |
---|---|
enumeration | 定义可接受值的一个列表 |
fractionDigits | 定义所允许的最大的小数位数。必须大于等于0。 |
length | 定义所允许的字符或者列表项目的精确数目。必须大于或等于0。 |
maxExclusive | 定义数值的上限。所允许的值必须小于此值。 |
maxInclusive | 定义数值的上限。所允许的值必须小于或等于此值。 |
maxLength | 定义所允许的字符或者列表项目的最大数目。必须大于或等于0。 |
minExclusive | 定义数值的下限。所允许的值必需大于此值。 |
minInclusive | 定义数值的下限。所允许的值必需大于或等于此值。 |
minLength | 定义所允许的字符或者列表项目的最小数目。必须大于或等于0。 |
pattern | 定义可接受的字符的精确序列。 |
totalDigits | 定义所允许的阿拉伯数字的精确位数。必须大于0。 |
whiteSpace | 定义空白字符(换行、回车、空格以及制表符)的处理方式。 |
注意:不同的数据类型支持的限定不尽相同,例如字符串不支持maxExclusive。
不同数据类型能够支持的限定规则查看,请点击这里
深入理解restriction的base属性
base属性值是在该架构(或由指定的命名空间指示的其他架构)中定义的内置数据类型 simpleType 元素的名称。将简单类型的值的范围限制为继承的简单类型的那些值的子集。
演示例子:
<xs:simpleType name="ageType">
<xs:restriction base="xs:integer">
<xs:maxExclusive value="1000"/>
<xs:minExclusive value="100"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="smallAgeType">
<xs:restriction base="ageType">
<xs:minExclusive value="300"/>
<xs:maxExclusive value="1500"/>
</xs:restriction>
</xs:simpleType>
请问,如果有元素
<xs:element name="smallAge" type="smallAgeType"/>
那么它的值范围多少?答案是大于300小于1000
ageType范围100到1000,smallAgeType范围是300到1500,交集为300到1000
字符串举例:
<xs:simpleType name="testbasestr2">
<xs:restriction base="testbasestr">
<xs:minLength value="4"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="testbasestr">
<xs:restriction base="xs:string">
<xs:enumeration value="abcdefg"/>
<xs:enumeration value="abcd"/>
<xs:enumeration value="ab"/>
</xs:restriction>
</xs:simpleType>
testbasestr2的可选值只有abcd,abcdefg
3.2 列表派生:<xsd:list> 元素
list 元素定义单个 simpleType 定义的集合。
语法:
<list
id = ID
itemType = QName
{any attributes with non-schema Namespace}…>
Content: (annotation?, (simpleType?))
</list>
属性:
属性 | 值 |
---|---|
id | 该元素的 ID。id 值必须属于类型 ID 并且在包含该元素的文档中是唯一的。 可选项。 |
itemType | 在该架构(或由指定的命名空间指示的其他架构)中定义的内置数据类型或 simpleType 元素的名称。包含 list 元素的 simpleType 元素是从 list 值指定的简单类型派生的。list 值必须是限定名 (QName)。对 simpleType 元素子级和 itemType 属性的使用是互相排斥的。 如果内容包含 simpleType 元素,则是不必需的。否则是必选项。 |
元素信息:
出现次数:一次
父元素:simpleType
子元素:annotation、simpleType
下面的例子展示了为一列整数的简单类型:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="intvalues" type="valuelist">
<xs:simpleType name="valuelist">
<xs:list itemType="xs:integer"/>
</xs:simpleType>
</xs:schema>
文档中的 “intvalues” 元素类似这样(注意这个列表有五个列表项):
<intvalues>100 34 56 -23 1567</intvalues>
注释:空白被作为列表项的分隔符。
注意
如果数据类型是从列表数据类型派生的,则可以使用以下约束方面。
- length
- maxLength
- minLength
- 枚举
- whiteSpace
例如:
<xs:simpleType name="derivedlistOfIntegers">
<!-- derivedlistOfIntegers允许最多3个列表项 -->
<xs:restriction base="valuelist">
<xs:maxLength value="3"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="valuelist">
<xs:list itemType="xs:integer"/>
</xs:simpleType>
3.3 联合派生:<xsd:union> 元素
union 元素定义多个 simpleType 定义的集合。
语法:
<union
id = ID
memberTypes = List of QNames
{any attributes with non-schema Namespace}…>
Content: (annotation?, (simpleType*))
</union>
属性:
属性 | 值 |
---|---|
id | 该元素的 ID。id 值必须属于类型 ID 并且在包含该元素的文档中是唯一的。 可选项。 |
memberTypes | 在该架构(或由指定的命名空间指示的其他架构)中定义的内置数据类型或 simpleType 元素的名称列表。包含 union 元素的 simpleType 元素是从 memberTypes 值指定的简单类型派生的。memberTypes 中的值必须是限定名 (QNames)。 对于简单类型联合定义,简单类型列表是 memberTypes(本身是简单类型的列表)的内容和 union 元素下每一子 simpleType 元素定义的联合。请参见本主题内下文中的第二个示例。 memberTypes 属性与 list 元素的 itemType 属性相反,后者与 list 元素的 simpleType 元素子级是互相排斥的。 可选项。 |
以下示例说明是两个简单类型的 union 的简单类型。:
<xs:attribute name="fontsize">
<xs:simpleType>
<xs:union memberTypes="fontbynumber fontbystringname" />
</xs:simpleType>
</xs:attribute>
<xs:simpleType name="fontbynumber">
<xs:restriction base="xs:positiveInteger">
<xs:maxInclusive value="72"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="fontbystringname">
<xs:restriction base="xs:string">
<xs:enumeration value="small"/>
<xs:enumeration value="medium"/>
<xs:enumeration value="large"/>
</xs:restriction>
</xs:simpleType>
以下示例说明一个简单类型定义,unionType代表范围:字符数为5的字符串、数字1、大于10的数
<xs:simpleType name="stringType">
<xs:restriction base="xs:string">
<xs:length value="5"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="unionType">
<xs:union memberTypes="stringType">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:enumeration value="1"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minExclusive value="10"/>
</xs:restriction>
</xs:simpleType>
</xs:union>
</xs:simpleType>