XML Schema restriction element
Definition and Usage
restriction element definition simpleType, simpleContent or complexContent defined constraints.
Element Information
- Parent element: simpleType, simpleContent, complexContent
grammar
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>
(? Sign declares that the restriction element of the element can occur zero or one time.)
属性 | 描述 |
---|---|
id | 可选。规定该元素的唯一的 ID。 |
base | 必需。规定在该 schema(或由指定的命名空间指示的其他 schema)中定义的内建数据类型、simpleType 或 complexType 元素的名称。 |
any attributes | 可选。规定带有 non-schema 命名空间的任何其他属性。 |
Example 1
The following example defines an element with a constraint and is named "age" of. The value of age can not be less than 0 or greater than 100:
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="100"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Example 2
This example defines a "initials" element named. "Initials" element is a simple type with a restriction. Acceptable values are from a to z three uppercase or lowercase letters:
<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>
Example 3
This example defines a named "password" element. "Password" element is a simple type with a restriction. The value must be a minimum of five characters and a maximum of eight characters:
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="5"/>
<xs:maxLength value="8"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Example 4
This example shows a complex type defined using constraints. Complex type "Chinese_customer" from a regular customer complex types derived from, the value of its fixed element is the country "China":
<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>
Complete XML Schema Reference