ตัวบอกเลขประเภทของ XSD

ผ่านหลักข่าย สามารถควบคุมวิธีใช้ส่วนประกอบในเอกสารได้

หลักข่าย

มีหกหลักข่าย

มีที่มานั้นชี้ว่า ลำดับ

  • ทั้งหมด
  • เลือก
  • ลำดับ

มีที่มานั้นชี้ว่า

  • maxOccurs
  • minOccurs

Group มีนิสัยบุคคล:

  • Group name
  • attributeGroup name

มีนิสัยบุคคล

มีนิสัยบุคคล ใช้เพื่อกำหนดลำดับขององค์ประกอบ

มีนิสัยบุคคล

<all> มีนิสัยบุคคล กำหนดว่าองค์ประกอบสามารถปรากฏขึ้นในลำดับที่ไม่มีกำหนด และตัวเล่นย่อยแต่ละตัวต้องปรากฏเพียงครั้งเดียว

<xs:element name="person">
  <xs:complexType>
    <xs:all>
      <xs:element name="firstname" type="xs:string"/>
      <xs:element name="lastname" type="xs:string"/>
    </xs:all>
  </xs:complexType>
</xs:element>

หมายเหตุ:เมื่อใช้ <all> มีนิสัยบุคคล คุณสามารถตั้งค่า <minOccurs> ให้เป็น 0 หรือ 1 และสามารถตั้งค่า <maxOccurs> ให้เป็น 1 (ที่จะเรียนเกี่ยวกับ <minOccurs> และ <maxOccurs> ในตอนต่อไป)

มีนิสัยบุคคล

<choice> มีนิสัยบุคคล กำหนดว่าองค์ประกอบสามารถปรากฏเป็นตัวเล่นย่อยหนึ่งหรืออาจปรากฏเป็นตัวเล่นย่อยอื่น (ไม่มีแบบเดียวกันกัน)

<xs:element name="person">
  <xs:complexType>
    <xs:choice>
      <xs:element name="employee" type="employee"/>
      <xs:element name="member" type="member"/>
    </xs:choice>
  </xs:complexType>
</xs:element>

คำเตือน:}เพื่อตั้งค่าให้ตัวเล่นย่อยปรากฏได้เป็นจำนวนที่ไม่จำกัด (unbounded) สามารถตั้งค่า <maxOccurs> (ที่จะเรียนเกี่ยวกับในตอนต่อไป) ให้เป็น unbounded (อภิเษก)。

มีนิสัยบุคคล

<sequence> กำหนดว่าตัวเล่นย่อยต้องปรากฏตามลำดับที่กำหนด:

<xs:element name="person">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="firstname" type="xs:string"/>
      <xs:element name="lastname" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

มีนิสัยบุคคล

มีนิสัยบุคคล

หมายเหตุ:สำหรับองค์ประกอบ "Order" และ "Group" ทั้งหมด (any, all, choice, sequence, group name และ group reference) ค่ามาตรฐานของ maxOccurs และ minOccurs คือ 1。

มีนิสัยบุคคล

<maxOccurs> แสดงว่าองค์ประกอบสามารถปรากฏขึ้นอย่างน้อยจำนวนเท่าไหร่:

<xs:element name="person">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="full_name" type="xs:string"/>
      <xs:element name="child_name" type="xs:string"> maxOccurs="10"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

ตัวอย่างที่อยู่เหนือนี้แสดงว่า ตัวเล่นย่อย "child_name" สามารถปรากฏในองค์ประกอบ "person" อย่างน้อย 1 ครั้ง (จากที่มีมาตรฐาน คือ minOccurs คือ 1) สูงสุด 10 ครั้ง。

มีนิสัยบุคคล

<minOccurs> แสดงว่าองค์ประกอบสามารถปรากฏขึ้นอย่างน้อยจำนวนเท่าไหร่:

<xs:element name="person">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="full_name" type="xs:string"/>
      <xs:element name="child_name" type="xs:string">
      maxOccurs="10" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

ตัวอย่างที่อยู่เหนือนี้แสดงว่า ตัวเล่นย่อย "child_name" สามารถปรากฏในองค์ประกอบ "person" อย่างน้อย 0 ครั้ง สูงสุด 10 ครั้ง。

คำเตือน:}ถ้าต้องการให้องค์ประกอบที่มีจำนวนที่ไม่จำกัด ใช้การประกาศ maxOccurs="unbounded"

ตัวอย่างที่แท้จริงหนึ่ง:

ไฟล์ XML ชื่อ "Myfamily.xml" นี้:

<?xml version="1.0" encoding="ISO-8859-1"?>
<persons xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
xsi:noNamespaceSchemaLocation="family.xsd">
<person>
<full_name>Tony Smith</full_name>
<child_name>Cecilie</child_name>
</person>
<person>
<full_name>David Smith</full_name>
<child_name>Jogn</child_name>
<child_name>mike</child_name>
<child_name>kyle</child_name>
<child_name>mary</child_name>
</person>
<person>
<full_name>Michael Smith</full_name>
</person>
</persons>

ไฟล์ XML นี้มีเอกภาพหลักที่ชื่อว่า "persons" ภายในเอกภาพหลักนี้ เราได้เฝ้าระวังสามองค์ประกอบ "person" ภายในองค์ประกอบ "person" ต้องมีองค์ประกอบ "full_name" และสามารถมีองค์ประกอบ "child_name" ถึง 5 องค์ประกอบ

คือไฟล์ schema "family.xsd" นี้:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
elementFormDefault="qualified">
<xs:element name="persons">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="person" maxOccurs="unbounded">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="full_name" type="xs:string"/>
            <xs:element name="child_name" type="xs:string">
            minOccurs="0" maxOccurs="5"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>
</xs:element>
</xs:schema>

ตัวชี้วัตถุ Group

ตัวชี้วัตถุ Group ใช้เพื่อจัดตั้งองค์ประกอบที่เกี่ยวข้อง:

กลุ่มองค์ประกอบ

กลุ่มองค์ประกอบถูกจัดตั้งโดยการประกาศ group:

<xs:group name="组名称">
  ...
</xs:group>

คุณต้องจัดตั้ง all, choice หรือ sequence ภายในประกาศ group ด้านล่างนี้ โดยตัวอย่างด้านล่างนี้จัดตั้ง group ที่มีชื่อว่า "persongroup" ซึ่งจัดตั้งกลุ่มองค์ประกอบที่ต้องปรากฏขึ้นในลำดับที่เฉพาะ:

<xs:group name="persongroup">
  <xs:sequence>
    <xs:element name="firstname" type="xs:string"/>
    <xs:element name="lastname" type="xs:string"/>
    <xs:element name="birthday" type="xs:date"/>
  </xs:sequence>
</xs:group>

หลังจากที่คุณได้จัดตั้ง group แล้ว คุณสามารถอ้างอิงมันในการจัดตั้งอื่นๆ:

<xs:group name="persongroup">
  <xs:sequence>
    <xs:element name="firstname" type="xs:string"/>
    <xs:element name="lastname" type="xs:string"/>
    <xs:element name="birthday" type="xs:date"/>
  </xs:sequence>
</xs:group>
<xs:element name="person" type="personinfo"/>
<xs:complexType name="personinfo">
  <xs:sequence>
    <xs:group ref="persongroup"/>
    <xs:element name="country" type="xs:string"/>
  </xs:sequence>
</xs:complexType>

กลุ่มคุณภาพ

กลุ่มคุณภาพถูกจัดตั้งโดยการประกาศ attributeGroup:

<xs:attributeGroup name="组名称">
  ...
</xs:attributeGroup>

ตัวอย่างด้านล่างนี้จัดตั้งกลุ่มคุณภาพที่มีชื่อว่า "personattrgroup":

<xs:attributeGroup name="personattrgroup">
  <xs:attribute name="firstname" type="xs:string"/>
  <xs:attribute name="lastname" type="xs:string"/>
  <xs:attribute name="birthday" type="xs:date"/>
</xs:attributeGroup>

หลังจากที่คุณได้จัดตั้งกลุ่มคุณภาพแล้ว คุณสามารถอ้างอิงมันในการจัดตั้งอื่นๆ ตามนี้:

<xs:attributeGroup name="personattrgroup">
  <xs:attribute name="firstname" type="xs:string"/>
  <xs:attribute name="lastname" type="xs:string"/>
  <xs:attribute name="birthday" type="xs:date"/>
</xs:attributeGroup>
<xs:element name="person">
  <xs:complexType>
    <xs:attributeGroup ref="personattrgroup"/>
  </xs:complexType>
</xs:element>