Phần tử hạn chế lược đồ XML
❮ Tham chiếu lược đồ XML hoàn chỉnh
Định nghĩa và Cách sử dụng
Phần tử hạn chế xác định các hạn chế đối với định nghĩa simpleType, simpleContent hoặc complexContent.
Thông tin phần tử
- Phần tử chính: simpleType, simpleContent, complexContent
Cú pháp
<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>
(Dấu? Khai báo rằng phần tử có thể xuất hiện không hoặc một lần bên trong phần tử hạn chế)
Attribute | Description |
---|---|
id | Optional. Specifies a unique ID for the element |
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 |
ví dụ 1
Ví dụ này xác định một phần tử được gọi là "tuổi" với một hạn chế. Giá trị của tuổi KHÔNG được thấp hơn 0 hoặc lớn hơn 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>
Ví dụ 2
Ví dụ này cũng định nghĩa một phần tử được gọi là "tên viết tắt". Phần tử "tên viết tắt" là một loại đơn giản với một hạn chế. Giá trị duy nhất được chấp nhận là BA trong số các chữ cái LOWERCASE HOẶC UPPERCASE từ a đến 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>
Ví dụ 3
Ví dụ này xác định một phần tử được gọi là "mật khẩu". Phần tử "mật khẩu" là một loại đơn giản với một hạn chế. Giá trị phải có tối thiểu năm ký tự và tối đa tám ký tự:
<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>
Ví dụ 4
Ví dụ này cho thấy một định nghĩa kiểu phức tạp bằng cách sử dụng hạn chế. Loại phức hợp "Norwegian_customer" có nguồn gốc từ một loại phức hợp khách hàng chung và yếu tố quốc gia của nó được cố định thành "Na Uy":
<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>
❮ Tham chiếu lược đồ XML hoàn chỉnh