XML Schema restriction element
Definition and usage
Element restriction defines constraints for simpleType, simpleContent, or complexContent
Element information
Appearance times | Once |
Parent element | complexContent |
Content | group、all、choice、sequence、attribute、attributeGroup、anyAttribute |
การใช้งาน
<restriction id=ID base=QName อัตรายะอื่นๆ > 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>
(? สัญลักษณ์การปรากฏของ element นี้ ใน restriction อาจปรากฏ 0 หรือ 1 ครั้ง)
รูปแบบ | รายละเอียด |
---|---|
id | เลือกได้เลย กำหนด ID ที่เป็นเดียวของ element นี้ |
base | จำเป็น กำหนดชื่อของปริมาณข้อมูลที่กำหนดใน schema นี้ (หรือ schema อื่นที่ระบุชื่อช่อง) หรือ element ที่กำหนดใน simpleType หรือ complexType |
อัตรายะอื่นๆ | เลือกได้เลย กำหนดรูปแบบของอัตรายะที่มีชื่อช่องที่ไม่เป็น schema |
ตัวอย่าง
ตัวอย่าง 1
ตัวอย่างด้านล่างนี้กำหนดองค์ประกอบชื่อว่า "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>
ตัวอย่าง 2
ตัวอย่างนี้กำหนดองค์ประกอบชื่อว่า "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>
ตัวอย่าง 3
ตัวอย่างนี้กำหนดองค์ประกอบชื่อว่า "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>
ตัวอย่าง 4
นิยายนี้แสดงวิธีการใช้กฎกติกาเพื่อกำหนดชนิดแบบที่ซับซ้อน ชนิดแบบที่ชื่อว่า "Chinese_customer" มาจากชนิดแบบที่ชื่อว่า "customer" โดย country ขององค์ประกอบมีค่าคงที่คือ "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>