ตัวชี้วัด XSD
เราควบคุมได้ว่าจะใช้องค์ประกอบอย่างไรในเอกสารที่มีตัวบ่งชี้
ตัวชี้วัด
มีเจ็ดตัวชี้วัด:
ตัวบ่งชี้การสั่งซื้อ:
- ทั้งหมด
- ทางเลือก
- ลำดับ
ตัวบ่งชี้การเกิดขึ้น:
- maxOccurs
- minOccurs
ตัวชี้วัดกลุ่ม:
- ชื่อกลุ่ม
- แอตทริบิวต์ชื่อกลุ่ม
ตัวชี้วัดการสั่งซื้อ
ตัวบ่งชี้คำสั่งซื้อใช้เพื่อกำหนดลำดับขององค์ประกอบ
ตัวบ่งชี้ทั้งหมด
ตัวบ่งชี้ <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>
ตัวบ่งชี้ลำดับ
ตัวบ่งชี้ <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>
ตัวชี้วัดเหตุการณ์
ตัวบ่งชี้การเกิดขึ้นใช้เพื่อกำหนดความถี่ที่องค์ประกอบสามารถเกิดขึ้นได้
หมายเหตุ:สำหรับตัวบ่งชี้ "คำสั่งซื้อ" และ "กลุ่ม" ทั้งหมด (ใดๆ ทั้งหมด ตัวเลือก ลำดับ ชื่อกลุ่ม และการอ้างอิงกลุ่ม) ค่าเริ่มต้นสำหรับ maxOccurs และ minOccurs คือ 1
maxOccurs ดัชนี
ตัวบ่งชี้ <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" สามารถเกิดขึ้นได้อย่างน้อยหนึ่งครั้ง (ค่าเริ่มต้นสำหรับ minOccurs คือ 1) และสูงสุดสิบครั้งในองค์ประกอบ "บุคคล"
minOccurs ตัวบ่งชี้
ตัวบ่งชี้ <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" สามารถเกิดขึ้นได้อย่างน้อยศูนย์ครั้งและสูงสุดสิบครั้งในองค์ประกอบ "บุคคล"
เคล็ดลับ:หากต้องการให้องค์ประกอบปรากฏได้ไม่จำกัดจำนวนครั้ง ให้ใช้คำสั่ง maxOccurs="unbounded":
ตัวอย่างการทำงาน:
ไฟล์ XML ชื่อ "Myfamily.xml":
<?xml version="1.0" encoding="UTF-8"?>
<persons xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="family.xsd">
<person>
<full_name>Hege Refsnes</full_name>
<child_name>Cecilie</child_name>
</person>
<person>
<full_name>Tove Refsnes</full_name>
<child_name>Hege</child_name>
<child_name>Stale</child_name>
<child_name>Jim</child_name>
<child_name>Borge</child_name>
</person>
<person>
<full_name>Stale Refsnes</full_name>
</person>
</persons>
ไฟล์ XML ด้านบนมีองค์ประกอบรูทชื่อ "บุคคล" ภายในองค์ประกอบรูทนี้ เราได้กำหนดองค์ประกอบ "บุคคล" ไว้สามองค์ประกอบ องค์ประกอบ "บุคคล" แต่ละรายการต้องมีองค์ประกอบ "full_name" และสามารถมีองค์ประกอบ "child_name" ได้ไม่เกินห้าองค์ประกอบ
นี่คือไฟล์สคีมา "family.xsd":
<?xml version="1.0" encoding="UTF-8"?>
<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>
ตัวชี้วัดกลุ่ม
ตัวบ่งชี้กลุ่มใช้เพื่อกำหนดชุดองค์ประกอบที่เกี่ยวข้อง
กลุ่มองค์ประกอบ
กลุ่มองค์ประกอบถูกกำหนดด้วยการประกาศกลุ่มดังนี้:
<xs:group name="groupname">
...
</xs: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: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="groupname">
...
</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>