Phần tử restriction XML Schema
Định nghĩa và cách sử dụng
Phần tử restriction định nghĩa các ràng buộc cho simpleType、simpleContent hoặc complexContent.
Thông tin phần tử
Xuất hiện | Lần |
Phần tử cha | complexContent |
Nội dung | group、all、choice、sequence、attribute、attributeGroup、anyAttribute |
Ngữ pháp
<restriction id=ID base=QName bất kỳ thuộc tính nào > Nội dung cho simpleType: (annotation?,(simpleType?,(minExclusive|minInclusive| maxExclusive|maxInclusive|totalDigits|fractionDigits| length|minLength|maxLength|enumeration|whiteSpace|pattern)*)) Nội dung cho simpleContent: (annotation?,(simpleType?,(minExclusive |minInclusive| maxExclusive|maxInclusive|totalDigits|fractionDigits| (length|minLength|maxLength|enumeration|whiteSpace|pattern)*)?, ((attribute|attributeGroup)*,anyAttribute?)) Nội dung cho complexContent: (annotation?,(group|all|choice|sequence)?, ((attribute|attributeGroup)*,anyAttribute?)) </restriction>
(? Biểu thức xác định ở phần restriction có thể xuất hiện 0 hoặc 1 lần。)
Thuộc tính | Mô tả |
---|---|
id | Tùy chọn. Định nghĩa ID duy nhất của phần tử này. |
cơ sở | Bắt buộc. Định nghĩa tên của các loại dữ liệu tích hợp, simpleType hoặc complexType được định nghĩa trong schéma này (hoặc các schéma khác được chỉ định bởi không gian tên). |
bất kỳ thuộc tính nào | Tùy chọn. Định nghĩa bất kỳ thuộc tính nào khác có không gian tên không theo schéma. |
Mô hình
Ví dụ 1
Ví dụ sau định nghĩa một phần tử có tên là "age" với các ràng buộc. Giá trị của age không thể nhỏ 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 định nghĩa một phần tử có tên là "initials". Phần tử "initials" là loại đơn giản có ràng buộc. Giá trị được chấp nhận là ba chữ cái in hoa hoặc in thường 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 định nghĩa một phần tử có tên là "password". Phần tử "password" là loại đơn giản có ràng buộc. Giá trị phải có ít nhất 5 ký tự và nhiều nhất 8 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 trình bày một định nghĩa loại phức tạp sử dụng ràng buộc. Loại phức tạp "Chinese_customer" được kế thừa từ loại phức tạp thông thường "customer", giá trị cố định của phần tử country là "China":
<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="Chinese_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="China"/> </xs:sequence> </xs:restriction> </xs:complexContent> </xs:complexType>