XML Schema规范共推荐12种约束。
范围约束 |
minInclusice、manInclusice、minExclusive、maxExcusive |
长度约束 | length、minLength、maxLength |
精度约束 | fractionDigits、totalDigits |
枚举约束 | enumeration |
正则表达式约束 | pattern |
空白处理 | whiteSpace |
fractionDigits:用于定义十进制数小数部分的精度。
totalDigits:用于指定decimal及其派生类型的数值精度。
whiteSpace:指定字符串中空白的处理方式,preserve(保留空白)、replace(字符串中Newline、tab和carriage-return都会被替换成空白)、collapse(字符串中Newline、tab和carriage-return都会被替换成空白,并且字符串中间连续的空白会被压缩成一个,字符串前后的空白会被自动删除)。
约束的使用方式:
<xs:facet value="option" />
Table B1.a. Simple Types & Applicable Facets | ||||||
---|---|---|---|---|---|---|
Simple Types | Facets | |||||
length | minLength | maxLength | pattern | enumeration | whiteSpace | |
string | y | y | y | y | y | y |
normalizedString | y | y | y | y | y | y |
token | y | y | y | y | y | see (1) |
base64Binary | y | y | y | y | y | see (1) |
hexBinary | y | y | y | y | y | see (1) |
integer | y | y | see (1) | |||
positiveInteger | y | y | see (1) | |||
negativeInteger | y | y | see (1) | |||
nonNegativeInteger | y | y | see (1) | |||
nonPositiveInteger | y | y | see (1) | |||
long | y | y | see (1) | |||
unsignedLong | y | y | see (1) | |||
int | y | y | see (1) | |||
unsignedInt | y | y | see (1) | |||
short | y | y | see (1) | |||
unsignedShort | y | y | see (1) | |||
byte | y | y | see (1) | |||
unsignedByte | y | y | see (1) | |||
decimal | y | y | see (1) | |||
float | y | y | see (1) | |||
double | y | y | see (1) | |||
boolean | y | see (1) | ||||
duration | y | y | see (1) | |||
dateTime | y | y | see (1) | |||
date | y | y | see (1) | |||
time | y | y | see (1) | |||
gYear | y | y | see (1) | |||
gYearMonth | y | y | see (1) | |||
gMonth | y | y | see (1) | |||
gMonthDay | y | y | see (1) | |||
gDay | y | y | see (1) | |||
Name | y | y | y | y | y | see (1) |
QName | y | y | y | y | y | see (1) |
NCName | y | y | y | y | y | see (1) |
anyURI | y | y | y | y | y | see (1) |
language | y | y | y | y | y | see (1) |
ID | y | y | y | y | y | see (1) |
IDREF | y | y | y | y | y | see (1) |
IDREFS | y | y | y | y | y | see (1) |
ENTITY | y | y | y | y | y | see (1) |
ENTITIES | y | y | y | y | y | see (1) |
NOTATION | y | y | y | y | y | see (1) |
NMTOKEN | y | y | y | y | y | see (1) |
NMTOKENS | y | y | y | y | y | see (1) |
Note: (1) Although the whiteSpace facet is applicable to this type, the only value that can be specified iscollapse . |
The facets listed in Table B1.b apply only to simple types which are ordered. Not all simple types are ordered and so B1.b does not list all of the simple types.
Table B1.b. Simple Types & Applicable Facets | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Simple Types | Facets | |||||||||||||||||
|
|
|
|
|
| |||||||||||||
integer | y | y | y | y | y | see (1) | ||||||||||||
positiveInteger | y | y | y | y | y | see (1) | ||||||||||||
negativeInteger | y | y | y | y | y | see (1) | ||||||||||||
nonNegativeInteger | y | y | y | y | y | see (1) | ||||||||||||
nonPositiveInteger | y | y | y | y | y | see (1) | ||||||||||||
long | y | y | y | y | y | see (1) | ||||||||||||
unsignedLong | y | y | y | y | y | see (1) | ||||||||||||
int | y | y | y | y | y | see (1) | ||||||||||||
unsignedInt | y | y | y | y | y | see (1) | ||||||||||||
short | y | y | y | y | y | see (1) | ||||||||||||
unsignedShort | y | y | y | y | y | see (1) | ||||||||||||
byte | y | y | y | y | y | see (1) | ||||||||||||
unsignedByte | y | y | y | y | y | see (1) | ||||||||||||
decimal | y | y | y | y | y | y | ||||||||||||
float | y | y | y | y | ||||||||||||||
double | y | y | y | y | ||||||||||||||
duration | y | y | y | y | ||||||||||||||
dateTime | y | y | y | y | ||||||||||||||
date | y | y | y | y | ||||||||||||||
time | y | y | y | y | ||||||||||||||
gYear | y | y | y | y | ||||||||||||||
gYearMonth | y | y | y | y | ||||||||||||||
gMonth | y | y | y | y | ||||||||||||||
gMonthDay | y | y | y | y | ||||||||||||||
gDay | y | y | y | y | ||||||||||||||
Note: (1) Although the fractionDigits facet is applicable to this type, the only value that can be specified is zero. |
一:限制<restriction.../>
限制派生:是以某个现有的类型为基础,通过对其添加N个约束,使其成为一个新的数据类型。
基类型的指定有两种方式:
全局数据类型作为基类型格式:
<?xml version="1.0" encoding="GBK"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType id="" name="数据类型名">
<xs:restriction id="" base="基类型">
...
</xs:restriction>
</xs:simpleType>
</xs:schema>
局部数据类型作为基类型格式:
<?xml version="1.0" encoding="GBK"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType id="" name="数据类型名">
<xs:restriction id="">
<xs:simpleType>
<xs:restriction id="">
...
</xs:restriction>
</xs:simpleType>
...
</xs:restriction>
</xs:simpleType>
</xs:schema>
id是可选属性。
二:列表<list.../>
列表是由 单个数据类型(原子类型、联合类型)扩展出来的,就像DTD中IDREFS是由IDREF扩展出来的。
列表以空白作为分隔符,所以不要使用本身就可以包含空格的类型作为基类型(e.g. string、normalizedString、token等元素),whiteSpace约束的值也只能是collapse。
列表使用enumeration和pattern约束时,是对整个列表内容起作用。(使用全局数据类型可避免对整个列表起作用)
定义列类型有两种方式:
全局数据类型作为列类型:
<?xml version="1.0" encoding="GBK"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="aName">
<xs:restriction base="基类型">
...
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="aName_list">
<xs:list itemType="aName" />
</xs:simpleType>
<xs:element name="" type="aName_list />
</xs:schema>
局部数据类型作为列类型:
<?xml version="1.0" encoding="GBK"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:simpleType name="aName_list"> <xs:list> <xs:simpleType> <xs:restriction base=""> ... </xs:restriction> </xs:simpleType> </xs:list> </xs:simpleType> <xs:element name="" type="aName_list" /> </xs:schema>
三:联合<union.../>
联合类型由成员类型(原子类型、列表类型、联合类型)组成,而联合类型的值可以是任意一种成员类型的值。
联合类型使用enumeration和pattern约束时,是对整个列表内容起作用。(使用全局数据类型可避免对整个联合起作用)
定义联合类型有两种方式:
全局数据类型作为联合类型:
<?xml version="1.0" encoding="GBK"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="aName1">
<xs:restriction base="基类型">
...
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="aName2">
<xs:restriction base="基类型">
...
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="aName_union">
<xs:union memberTypes="aName1 aName2" />
</xs:simpleType>
<xs:element name="" type="aName_union" />
</xs:schema>
局部数据类型作为联合类型:
<?xml version="1.0" encoding="GBK"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="aName_union">
<xs:union>
<xs:simpleType>
<xs:restriction base="">
...
</xs:restriction>
</xs:simpleType>
<xs:simpleType>
<xs:restriction base="">
...
</xs:restriction>
</xs:simpleType>
...
</xs:union>
</xs:simpleType>
<xs:element name="" type="aName_union" />
</xs:schema>