ตัวอย่าง XSD
บทนี้จะสาธิตวิธีการเขียน XML Schema คุณจะได้เรียนรู้ว่าสคีมาสามารถเขียนได้หลายวิธี
เอกสาร XML
มาดูเอกสาร XML ที่เรียกว่า "shiporder.xml" กัน:
<?xml version="1.0" encoding="UTF-8"?>
<shiporder orderid="889923"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="shiporder.xsd">
<orderperson>John Smith</orderperson>
<shipto>
<name>Ola Nordmann</name>
<address>Langgt 23</address>
<city>4000 Stavanger</city>
<country>Norway</country>
</shipto>
<item>
<title>Empire Burlesque</title>
<note>Special Edition</note>
<quantity>1</quantity>
<price>10.90</price>
</item>
<item>
<title>Hide your heart</title>
<quantity>1</quantity>
<price>9.90</price>
</item>
</shiporder>
เอกสาร XML ด้านบนประกอบด้วยองค์ประกอบราก "shiporder" ที่มีแอตทริบิวต์ที่จำเป็นที่เรียกว่า "orderid" องค์ประกอบ "ใบสั่งจัดส่ง" ประกอบด้วยองค์ประกอบย่อยสามองค์ประกอบ: "ผู้สั่งซื้อ" "จัดส่ง" และ "รายการ" องค์ประกอบ "รายการ" ปรากฏขึ้นสองครั้ง และมีองค์ประกอบ "ชื่อ" องค์ประกอบ "หมายเหตุ" ที่ไม่บังคับ องค์ประกอบ "ปริมาณ" และองค์ประกอบ "ราคา"
บรรทัดด้านบน: xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" บอก XML parser ว่าเอกสารนี้ควรได้รับการตรวจสอบเทียบกับสคีมา บรรทัด: xsi:noNamespaceSchemaLocation="shiporder.xsd" ระบุว่าสคีมาอยู่ที่ใด (ในที่นี้จะอยู่ในโฟลเดอร์เดียวกับ "shiporder.xml")
สร้าง XML Schema
ตอนนี้ เราต้องการสร้างสคีมาสำหรับเอกสาร XML ด้านบน
เราเริ่มต้นด้วยการเปิดไฟล์ใหม่ที่เรียกว่า "shiporder.xsd" ในการสร้างสคีมา เราสามารถทำตามโครงสร้างในเอกสาร XML และกำหนดแต่ละองค์ประกอบตามที่เราพบ เราจะเริ่มต้นด้วยการประกาศ XML มาตรฐาน ตามด้วยองค์ประกอบ xs:schema ที่กำหนดสคีมา:
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
...
</xs:schema>
ในสคีมาด้านบน เราใช้เนมสเปซมาตรฐาน (xs) และ URI ที่เชื่อมโยงกับเนมสเปซนี้คือคำจำกัดความภาษาสคีมา ซึ่งมีค่ามาตรฐานเป็น http://www.w3.org/2001/XMLSchema
ต่อไป เราต้องกำหนดองค์ประกอบ "ใบสั่งซื้อ" องค์ประกอบนี้มีแอตทริบิวต์และมีองค์ประกอบอื่นๆ ดังนั้นเราจึงพิจารณาว่าเป็นประเภทที่ซับซ้อน องค์ประกอบย่อยขององค์ประกอบ "ใบสั่งจัดส่ง" ล้อมรอบด้วยองค์ประกอบ xs:sequence ที่กำหนดลำดับขององค์ประกอบย่อย:
<xs:element name="shiporder">
<xs:complexType>
<xs:sequence>
...
</xs:sequence>
</xs:complexType>
</xs:element>
จากนั้นเราต้องกำหนดองค์ประกอบ "orderperson" เป็นประเภทที่เรียบง่าย (เพราะไม่มีคุณลักษณะหรือองค์ประกอบอื่น ๆ ) ประเภท (xs:string) นำหน้าด้วยคำนำหน้าเนมสเปซที่เชื่อมโยงกับ XML Schema ซึ่งระบุประเภทข้อมูลสคีมาที่กำหนดไว้ล่วงหน้า:
<xs:element name="orderperson" type="xs:string"/>
ต่อไป เราต้องกำหนดสององค์ประกอบที่เป็นประเภทที่ซับซ้อน: "shipto" และ "item" เราเริ่มต้นด้วยการกำหนดองค์ประกอบ "shipto":
<xs:element name="shipto">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
ด้วยสคีมา เราสามารถกำหนดจำนวนเหตุการณ์ที่เป็นไปได้สำหรับองค์ประกอบที่มีแอตทริบิวต์ maxOccurs และ minOccurs maxOccurs ระบุจำนวนครั้งสูงสุดสำหรับองค์ประกอบ และ minOccurs ระบุจำนวนการเกิดขึ้นขั้นต่ำสำหรับองค์ประกอบ ค่าเริ่มต้นสำหรับทั้ง maxOccurs และ minOccurs คือ 1!
ตอนนี้เราสามารถกำหนดองค์ประกอบ "รายการ" องค์ประกอบนี้สามารถปรากฏได้หลายครั้งภายในองค์ประกอบ "ใบสั่งซื้อ" สิ่งนี้ถูกระบุโดยการตั้งค่าแอตทริบิวต์ maxOccurs ขององค์ประกอบ "item" เป็น "unbounded" ซึ่งหมายความว่าองค์ประกอบ "item" สามารถเกิดขึ้นได้มากเท่าที่ผู้เขียนต้องการ โปรดสังเกตว่าองค์ประกอบ "บันทึกย่อ" เป็นทางเลือก เราได้ระบุสิ่งนี้โดยการตั้งค่าแอตทริบิวต์ minOccurs เป็นศูนย์:
<xs:element name="item" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="note" type="xs:string" minOccurs="0"/>
<xs:element name="quantity" type="xs:positiveInteger"/>
<xs:element name="price" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
</xs:element>
ขณะนี้เราสามารถประกาศแอตทริบิวต์ขององค์ประกอบ "ใบสั่งซื้อ" เนื่องจากเป็นแอตทริบิวต์ที่จำเป็น เราจึงระบุ use="required"
หมายเหตุ:การประกาศแอตทริบิวต์ต้องมาก่อนเสมอ:
<xs:attribute name="orderid" type="xs:string" use="required"/>
นี่คือรายการที่สมบูรณ์ของไฟล์สคีมาชื่อ "shiporder.xsd":
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="shiporder">
<xs:complexType>
<xs:sequence>
<xs:element name="orderperson" type="xs:string"/>
<xs:element name="shipto">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="item" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="note" type="xs:string" minOccurs="0"/>
<xs:element name="quantity" type="xs:positiveInteger"/>
<xs:element name="price" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="orderid" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>
แบ่งสคีมา
วิธีการออกแบบก่อนหน้านี้นั้นง่ายมาก แต่อาจอ่านและดูแลรักษายากเมื่อเอกสารมีความซับซ้อน
วิธีการออกแบบถัดไปจะขึ้นอยู่กับการกำหนดองค์ประกอบและคุณลักษณะทั้งหมดก่อน จากนั้นจึงอ้างอิงโดยใช้แอตทริบิวต์อ้างอิง
นี่คือการออกแบบใหม่ของไฟล์สคีมา ("shiporder.xsd"):
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- definition of simple elements -->
<xs:element name="orderperson" type="xs:string"/>
<xs:element name="name" type="xs:string"/>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
<xs:element name="title" type="xs:string"/>
<xs:element name="note" type="xs:string"/>
<xs:element name="quantity" type="xs:positiveInteger"/>
<xs:element name="price" type="xs:decimal"/>
<!-- definition of attributes -->
<xs:attribute name="orderid" type="xs:string"/>
<!-- definition of complex elements -->
<xs:element name="shipto">
<xs:complexType>
<xs:sequence>
<xs:element ref="name"/>
<xs:element ref="address"/>
<xs:element ref="city"/>
<xs:element ref="country"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="item">
<xs:complexType>
<xs:sequence>
<xs:element ref="title"/>
<xs:element ref="note" minOccurs="0"/>
<xs:element ref="quantity"/>
<xs:element ref="price"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="shiporder">
<xs:complexType>
<xs:sequence>
<xs:element ref="orderperson"/>
<xs:element ref="shipto"/>
<xs:element ref="item" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute ref="orderid" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>
การใช้ Named Types
วิธีการออกแบบที่สามกำหนดคลาสหรือประเภท ที่ช่วยให้เราสามารถนำคำจำกัดความขององค์ประกอบมาใช้ซ้ำได้ ซึ่งทำได้โดยการตั้งชื่อองค์ประกอบ simpleTypes และ complexTypes จากนั้นชี้ไปที่แอตทริบิวต์ type ขององค์ประกอบ
นี่คือการออกแบบที่สามของไฟล์สคีมา ("shiporder.xsd"):
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="stringtype">
<xs:restriction base="xs:string"/>
</xs:simpleType>
<xs:simpleType name="inttype">
<xs:restriction base="xs:positiveInteger"/>
</xs:simpleType>
<xs:simpleType name="dectype">
<xs:restriction base="xs:decimal"/>
</xs:simpleType>
<xs:simpleType name="orderidtype">
<xs:restriction base="xs:string">
<xs:pattern value="[0-9]{6}"/>
</xs:restriction>
</xs:simpleType>
<xs:complexType name="shiptotype">
<xs:sequence>
<xs:element name="name" type="stringtype"/>
<xs:element name="address" type="stringtype"/>
<xs:element name="city" type="stringtype"/>
<xs:element name="country" type="stringtype"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="itemtype">
<xs:sequence>
<xs:element name="title" type="stringtype"/>
<xs:element name="note" type="stringtype" minOccurs="0"/>
<xs:element name="quantity" type="inttype"/>
<xs:element name="price" type="dectype"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="shipordertype">
<xs:sequence>
<xs:element name="orderperson" type="stringtype"/>
<xs:element name="shipto" type="shiptotype"/>
<xs:element name="item" maxOccurs="unbounded" type="itemtype"/>
</xs:sequence>
<xs:attribute name="orderid" type="orderidtype" use="required"/>
</xs:complexType>
<xs:element name="shiporder" type="shipordertype"/>
</xs:schema>
องค์ประกอบข้อจำกัดระบุว่าประเภทข้อมูลได้มาจากประเภทข้อมูลเนมสเปซ W3C XML Schema ดังนั้น ส่วนย่อยต่อไปนี้หมายความว่าค่าขององค์ประกอบหรือแอตทริบิวต์ต้องเป็นค่าสตริง:
<xs:restriction base="xs:string">
องค์ประกอบการจำกัดมักใช้เพื่อใช้การจำกัดกับองค์ประกอบ ดูบรรทัดต่อไปนี้จากสคีมาด้านบน:
<xs:simpleType name="orderidtype">
<xs:restriction base="xs:string">
<xs:pattern value="[0-9]{6}"/>
</xs:restriction>
</xs:simpleType>
สิ่งนี้บ่งชี้ว่าค่าขององค์ประกอบหรือแอตทริบิวต์ต้องเป็นสตริง ต้องมีอักขระหกตัวในแถวพอดี และอักขระเหล่านั้นต้องเป็นตัวเลขตั้งแต่ 0 ถึง 9