ตัวอย่าง XSD หนึ่ง

บทนี้จะแสดงให้เห็นว่าเขียน XML Schema แบบไหน คุณยังจะเรียนรู้วิธีการเขียน schema แบบต่างๆ

เอกสาร XML

จะมีการชมเอกสาร XML ชื่อว่า "shiporder.xml":

<?xml version="1.0" encoding="ISO-8859-1"?>
<shiporder orderid="889923"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="shiporder.xsd">
 <orderperson>George Bush</orderperson>
 <shipto>
  <name>John Adams</name>
  <address>Oxford Street</address>
  <city>London</city>
  <country>UK</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 ดังกล่าวมี root element ชื่อ "shiporder" ซึ่งมี attribute ที่ต้องชื่อว่า "orderid" ตัว element "shiporder" มี sub-element สามตัวคือ "orderperson"、"shipto" และ "item" ตัว element "item" ปรากฏเป็นครั้งที่สอง มันมี element "title" และ element "note" ที่เป็นออプชัน ซึ่งมี "quantity" และ element "price"

บรรทัดนี้ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ระบุ XML parser ต้องเช็คเอกสารนี้ตาม schema บางชิ้น บรรทัดนี้ xsi:noNamespaceSchemaLocation="shiporder.xsd" กำหนดตำแหน่งของ schema (ที่นี้ มันอยู่ในแฟ้มเดียวกับ "shiporder.xml")

สร้าง XML Schema

ตอนนี้ เราต้องการสร้าง schema สำหรับเอกสาร XML นี้

เราสามารถเริ่มด้วยการเปิดไฟล์ใหม่และตั้งชื่อไฟล์ว่า "shiporder.xsd" เพื่อสร้าง schema เราต้องการเพียงตามโครงสร้างของเอกสาร XML และกำหนดอิเลเมนต์ที่เราพบ

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
...
...
</xs:schema>

ใน schema ด้านบน เราใช้ชื่อพื้นที่มาตรฐาน (xs) ชื่อพื้นที่ที่เกี่ยวข้องกับชื่อพื้นที่นี้คือ Schema language definition ค่ามาตรฐานคือ http://www.w3.org/2001/XMLSchema

ต่อมา เราต้องการจะกำหนดอิเลเมนต์ "shiporder" อิเลเมนต์นี้มีอาทิตย์หนึ่งที่มีอิเลเมนต์อื่นๆ ดังนั้นเราจะมองมันว่ามันเป็นชนิดที่แยกตัว อิเลเมนต์ลูกของอิเลเมนต์ "shiporder" ถูกอิเลเมนต์ xs:sequence ล้อมรอบ ซึ่งกำหนดลำดับของอิเลเมนต์ลูก


 
  <xs:sequence>
  ...
  ...
  </xs:sequence>
  ...
 </xs:complexType>

หลังจากนั้น เราต้องการจะกำหนดอิเลเมนต์ "orderperson" ให้เป็นชนิดง่าย (นี้เพราะมันไม่มีอาทิตย์และอิเลเมนต์อื่นๆ) หน้าตาของประกาศ (xs:string) หน้าตานี้มีผลต่อชื่อพื้นที่ของ XML schema ที่มีความเกี่ยวข้องกับ XML schema ที่ระบุชนิดของ data type ที่เป็นกลางรับรอง

<xs:element name="orderperson" type="xs:string"/>

ต่อมา ฉันต้องการจะกำหนดอิเลเมนต์สองอย่างเป็นชนิดที่แยกตัว: "shipto" และ "item" พวกเราเริ่มจากการกำหนดอิเลเมนต์ "shipto"


 
  <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>

ผ่าน schema เราสามารถใช้属性 maxOccurs และ minOccurs มากำหนดจำนวนที่อิเลเมนต์อาจปรากฏขึ้น จำนวนสูงสุดที่อิเลเมนต์อาจปรากฏขึ้นกำหนดโดย maxOccurs และจำนวนต่ำสุดที่อิเลเมนต์อาจปรากฏขึ้นกำหนดโดย minOccurs ค่าปริยายของ maxOccurs และ minOccurs คือ 1!

ตอนนี้ เราสามารถกำหนด "item" อิเลเมนต์แล้ว อิเลเมนต์นี้สามารถปรากฏขึ้นในในอิเลเมนต์ "shiporder" หลายครั้ง นี่เป็นไปด้วยการตั้งค่าค่าของ属性 maxOccurs ของอิเลเมนต์ "item" ให้เป็น "unbounded" ซึ่งทำให้อิเลเมนต์ "item" สามารถปรากฏขึ้นเท่าที่ผู้สร้างต้องการหลายครั้ง โปรดจำได้ว่า อิเลเมนต์ "note" คืออิเลเมนต์ที่ไม่จำเป็นต้องมี พวกเราได้ตั้งค่าค่าของ属性 minOccurs ของอิเลเมนต์นี้เป็น 0 แล้ว

<xs:element name="item" maxOccurs="unbounded">
 
  <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>

ตอนนี้เราสามารถประกาศคุณสมบัติขององค์ประกอบ "shiporder" ได้ ด้วยเหตุผลที่เรากำหนด use="required"

หมายเหตุ:การประกาศของนิยามนี้ต้องถูกจัดให้มีตำแหน่งสุดท้าย:

<xs:attribute name="orderid" type="xs:string" use="required"/>

นี่คือบันทึกเอกสารของไฟล์ schema ที่มีชื่อ "shiporder.xsd":

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

 
  <xs:sequence>
   <xs:element name="orderperson" type="xs:string"/>
   
    
     <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 name="item" maxOccurs="unbounded">
    
     <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:sequence>
  <xs:attribute name="orderid" type="xs:string" use="required"/>
 </xs:complexType>

</xs:schema>

แบ่งแยก Schema

วิธีออกแบบก่อนหน้านี้ง่ายแต่เมื่อเอกสารมีความซับซ้อนมาก จึงยากที่จะอ่านและประกอบ

วิธีออกแบบที่มานี้มีลักษณะง่ายแต่เมื่อเอกสารมีความซับซ้อนมาก จึงยากที่จะอ่านและประกอบ

นี่คือแบบจำลองแบบไฟล์ schema ที่ออกแบบด้วยวิธีใหม่:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- ง่ายต่อการกำหนด -->
<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"/>




 
  <xs:sequence>
   
   
   
   
  </xs:sequence>
 </xs:complexType>


 
  <xs:sequence>
   
   
   
   
  </xs:sequence>
 </xs:complexType>


 
  <xs:sequence>
   
   
   
  </xs:sequence>
  
 </xs:complexType>

</xs:schema>

使用指定的类型(Named Types)

第三种设计方法定义了类或者类型,这样使我们有能力重复使用元素的定义。具体的方式是:首先对简易元素和复合元素进行命名,然后通过元素的 type 属性来指向它们。

这是利用第三种方法设计的 schema 文件 ("shiporder.xsd"):

<?xml version="1.0" encoding="ISO-8859-1" ?>
<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>

element ฝาก ระบุว่าปริมาณข้อมูลมาจากชื่อสเชมาของ W3C XML Schema จึงมีปริมาณข้อมูลที่ต่อไปนี้ต้องเป็นปริมาณของแบบที่เป็นข้อความ

<xs:restriction base="xs:string">

องค์ประกอบ restriction มักถูกใช้เพื่อกำหนดข้อจำกัดต่อองค์ประกอบ โปรดดูชิ้นส่วนดังต่อไปนี้จาก schema ข้างต้น

<xs:simpleType name="orderidtype">
 <xs:restriction base="xs:string">
  <xs:pattern value="[0-9]{6}"/>
 </xs:restriction>
</xs:simpleType>

รหัสโค้ดนี้บอกว่าค่าขององค์ประกอบหรือแอตทริบิวต์ต้องเป็นข้อความ และต้องเป็นหกตัวอักษรต่อเนื่อง และตัวอักษรดังกล่าวต้องเป็นตัวเลข 0-9