XSD 複合類型指示器
ผ่านคำแจ้ง เราสามารถควบคุมวิธีการใช้สมาชิกในเอกสาร
คำแจ้ง
มีคำแจ้งเจ็ดราย
คำแจ้ง Order
- All
- Choice
- Sequence
คำแจ้ง Occurrence
- maxOccurs
- minOccurs
Group คำแจ้ง
- Group name
- attributeGroup name
หมายเหตุ Order
หมายเหตุ Order ใช้เพื่อกำหนดลำดับของชาติภาพ:
หมายเหตุ All
<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> หมายเหตุเพียงครั้งเดียวเท่านั้น (จะมีการรายละเอียดเกี่ยวกับ <minOccurs> และ <maxOccurs> ในหลัง)
หมายเหตุ Choice
<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>
คำเตือน:ถ้าต้องการที่จะตั้งให้ชาติภาพลูกปรากฏได้ทุกครั้ง สามารถตั้ง <maxOccurs> (จะมีการรายละเอียดในหลัง) ให้เป็น unbounded (ไม่จำกัดครั้ง)
หมายเหตุ Sequence
<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>
หมายเหตุ Occurrence
หมายเหตุ Occurrence ใช้เพื่อกำหนดความเร็วที่ชาติภาพปรากฏขึ้น:
หมายเหตุ:สำหรับหมายเหตุ "Order" และ "Group" ทั้งหมด (any, all, choice, sequence, ชื่อ group และอ้างอิง group) ค่ามาตรฐานของ 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" สามารถปรากฏในชาติภาพ "person" ได้น้อยที่สุด 1 ครั้ง (โดยค่ามาตรฐานของ minOccurs คือ 1) และสูงสุด 10 ครั้ง。
หมายเหตุ 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" สามารถปรากฏในชาติภาพ "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 จำนวนเฉพาะสำหรับกำหนดองค์ประกอบที่เกี่ยวข้องของหลายชุดตัวแปร。
กลุ่มองค์ประกอบ
กลุ่มองค์ประกอบจัดการโดย elementGroup การกำหนด:
<xs:group name="组名称"> ... </xs:group>
คุณต้องจัดการกลุ่ม elementGroup ภายในการกำหนด all, choice หรือ sequence อย่างน้อยหนึ่งตัว ตัวอย่างด้านล่างนี้กำหนดกลุ่ม elementGroup ที่มีชื่อ "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>
หลังจากที่คุณได้จัดการกลุ่ม elementGroup ให้เสร็จสมบูรณ์แล้ว คุณสามารถอ้างอิงมันในการกำหนดอื่นๆ:
<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>
หลังจากที่คุณได้จัดการกลุ่มคุณภาพแบบ 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>