XML Schema restriction 元素

定义和用法

restriction 元素定义对 simpleType、simpleContent 或 complexContent 定义的约束。

元素信息

出现次数 一次
父元素 complexContent
内容 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>

(? 符号声明在 restriction 元素中该元素可出现零次或一次。)

属性 รายละเอียด
id เลือกตั้งได้ กำหนดรหัสไอดีที่เป็นเดียวที่สำหรับองค์ประกอบนี้
base วิธีที่จำเป็น กำหนดชื่อของประเภทข้อมูลรูปแบบภายใน schema หรือ schema อื่นที่มีชื่อนามสกุลหรือชื่อช่วยแสดงตามชื่อนามสกุลที่กำหนด
อาทิตย์แบบ เลือกตั้งได้ กำหนดรูปแบบขององค์ประกอบอื่นๆ ที่มีชื่อนามสกุลแบบ non-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" ซึ่งมีข้อกำหนด ค่าที่ยอมรับเป็นอักษรภาษาอังกฤษเล็กหรือใหญ่สามอักษร:

<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" ซึ่งมีข้อกำหนด ค่าต้องเป็นอักษรภาษาอังกฤษ 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>