当前位置: 首页 > 网络学院 > XML相关教程 > Schema (XSD) > XML Schema restriction 元素
The restriction element defines restrictions on a simpleType, simpleContent, or complexContent definition.
restriction元素的作用是:为simpleType[简单类型]、simpleContent[简单内容]或complexContent[复合内容]定义一个限制属性。
<restriction id=ID base=QName any attributes > Content for simpleType: (annotation?,(simpleType?,(minExclusive|minInclusive| maxExclusive|maxInclusive|totalDigits|fractionDigits| length|minLength|maxLength|enumeration|whiteSpace|pattern)*)) Content for simpleContent: (annotation?,(simpleType?,(minExclusive |minInclusive| maxExclusive|maxInclusive|totalDigits|fractionDigits| length|minLength|maxLength|enumeration|whiteSpace|pattern)*)?, ((attribute|attributeGroup)*,anyAttribute?)) Content for complexContent: (annotation?,(group|all|choice|sequence)?, ((attribute|attributeGroup)*,anyAttribute?)) </restriction> |
(The ? sign declares that the element can occur zero or one time inside the restriction element)
“?”符号用于声明元素在restriction元素中允许出现的次数(0次或1次)
属性 | 描述 |
---|---|
id | Optional. Specifies a unique ID for the element 可选参数。为元素指定一个独立的ID |
base | Required. Specifies the name of a built-in data type, simpleType element, or complexType element defined in this schema or another schema |
any attributes | Optional. Specifies any other attributes with non-schema namespace 可选参数。指定其它的属性(这些属性无schema命名空间) |
This example defines an element called "age" with a restriction. The value of age can NOT be lower than 0 or greater than 100:
该案例定义了一个名为“age”的元素(包含一个限制属性)。age值不允许小于0或大于100:
<xs:element name="age"> <xs:simpleType> <xs:restriction base="xs:integer"> <xs:minInclusive value="0"/> <xs:maxInclusive value="100"/> </xs:restriction> </xs:simpleType> </xs:element> |
This example also defines an element called "initials". The "initials" element is a simple type with a restriction. The only acceptable value is THREE of the LOWERCASE OR UPPERCASE letters from a to z:
这个案例定义了一个名为“initials”的元素。“initials”元素是一个简单类型元素(包含一个限制属性)。它仅允许使用从“a”到“z”的字母(大写和小写均可使用):
<xs:element name="initials"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="[a-zA-Z][a-zA-Z][a-zA-Z]"/> </xs:restriction> </xs:simpleType> </xs:element> |
This example defines an element called "password". The "password" element is a simple type with a restriction. The value must be minimum five characters and maximum eight characters:
这个案例定义了一个名为“password”的元素。“password”元素是一个简单类型元素(包含一个限制属性)。它仅允许使用的字符串长度为大于等于5,小于等于8:
<xs:element name="password"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:minLength value="5"/> <xs:maxLength value="8"/> </xs:restriction> </xs:simpleType> </xs:element> |
This example shows a complex type definition using restriction. The complex type "Norwegian_customer" is derived from a general customer complex type and its country element is fixed to "Norway":
这个案例展示了一个使用限制属性的复合类型元素。复合类型“Norwegian_customer”来自于一个普通的customer[客户]复合类型和它所对应的country[国家]元素(该案例中的country属性为固定值“Norway”):
<xs:complexType name="customer"> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> <xs:element name="country" type="xs:string"/> </xs:sequence> </xs:complexType> <xs:complexType name="Norwegian_customer"> <xs:complexContent> <xs:restriction base="customer"> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> <xs:element name="country" type="xs:string" fixed="Norway"/> </xs:sequence> </xs:restriction> </xs:complexContent> </xs:complexType> |