要定义新的简单类型,只能从现有的简单类型派生。对于简单类型只有限制派生没有扩展派生,通过简单派生得到的新的简单类型是其原来类型的子集。Xml Schema推荐了标准的12个面(facet)来限制约束。要定义简单类型,使用xs:simpleType元素,要对原类型进行限制,使用xs:restriction元素。
约束类型的12个面。
类别 | 面 |
范围 | minInclusive、maxInclusive、minExclusive、maxExclusive |
长度 | length、minLength、maxLength |
精度 | totalDigits、fractionDigits |
枚举 | enumeration |
模式匹配 | pattern |
空白处理 | whiteSpace |
范围说明
在实际应用中,往往需要把某个值限定在一定的范围内,如雇员的年龄在18到60之间,分数在0到100之间等。minInclusive(最小包含,相当于>=)、maxInclusive(最大包含,相当于<=)、minExclusive(最小不包含,相当于>)、maxExclusive(最大不包含,相当于<)。这4个范围面只能适用于整数、日期、时间类型。
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="score" type="scoreType"/>
<xs:simpleType name="scoreType">
<xs:restriction base="xs:integer">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="100"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
长度说明
限制值的长度,比如需要限制密码的长度为8位,用户名的长度为6到20位。length:内容长度、minLength:最小长度、maxLength:最大长度。主要使用的类型为字符型、QName型、anyURI型。不可以适用于日期、时间、数字和boolean。length面不能与minLength、maxLength面一起使用。
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="username">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="6"/>
<xs:maxLength value="18"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="password" type="pwType"/>
<xs:simpleType name="pwType">
<xs:restriction base="xs:string">
<xs:length value="8"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
精度说明
在某些情况下,需要限定某个数的数字位数,比如支票上最大的单位是亿,最小的单位是分。totalDigits:总数字、fractionDigits:分数数字(小数点后面的)。
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="check" type="checkType"/>
<xs:simpleType name="CheckType">
<xs:restriction base="xs:decimal">
<xs:totalDigits value="11"/>
<xs:fractionDigits value="2"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
枚举说明
经常将某个值限定在一组可选的值之中,比如:性别要在“男”和“女”之间选择。enumeration:枚举。enumeration面可以在除boolean以后的其他类型中使用。
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="sex" type="sexType"/>
<xs:simpleType name="sexType">
<xs:restriction base="xs:string">
<xs:enumeration value="Male"/>
<xs:enumeration value="FeMale"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
模式匹配
有时需要复杂的格式约束,比如需要对邮件地址的格式进行检查。可以使用pattern面进行正则表达式的限制。
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="zipCode">
<xs:simpleType>
<xs:restriction base="xs:token">
<xs:pattern vlues="\d{6}"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:schema>
空白处理
whitespace的有效值为:preserve、replace、collapse。
preserve:所有的空白都被保留
replace:制表符、换行、回车都用一个空格进行替换,但连续的空格不被压缩为一个空格。
collapse:制表符、换行、回车被空格替换,连续的空格被压缩为一个空格。
固定面
除了enumeration和pattern面外,其余的面都有一个fixed属性,默认为false。如果该值为true,那么该面的值将被固定下来,其派生类型不能改变该面的值。
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="check" type="checkType"/>
<xs:simpleType name="CheckType">
<xs:restriction base="xs:decimal">
<xs:totalDigits value="7" fixed="true"/>
<xs:fractionDigits value="2" fixed="true"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>