Latest web development tutorials

เช่น XML Schema

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


เอกสาร XML

ลองดูที่นี้เรียกว่า "shiporder.xml" เอกสาร 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>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" ของ "Shiporder" องค์ประกอบที่มีสามองค์ประกอบของเด็กแตกต่างกัน: "orderperson", "shipto" และ "รายการ" "รายการ" องค์ประกอบที่ปรากฏขึ้นสองครั้งและมันมี "ชื่อ" ตัวเลือก "โน้ต" องค์ประกอบเป็น "ปริมาณ" และ "ราคา" องค์ประกอบ

xmlns บรรทัดด้านบน: xsi = "http://www.w3.org/2001/XMLSchema-instance" บอกสคีซึ่งเป็นไปตาม parser XML เพื่อตรวจสอบเอกสารนี้ บรรทัด: xsi: noNamespaceSchemaLocation = "shiporder.xsd" โครงสร้างกำหนดตำแหน่ง (ในกรณีนี้มัน "shiporder.xml" ในโฟลเดอร์เดียวกัน)


สร้าง XML Schema

ตอนนี้เราจำเป็นต้องสร้างสคีสำหรับเอกสาร XML ข้างต้น

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

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

ในสคีมาข้างต้นเราใช้ namespace มาตรฐาน (XS) พื้นที่ URI เกี่ยวข้องกับชื่อนี้เป็นภาษานิยามของ Schema (ความคมชัดภาษา Schema) ค่ามาตรฐานคือ http://www.w3.org/ 2001 / XMLSchema

ต่อไปเราจะต้องกำหนด "shiporder" องค์ประกอบ องค์ประกอบนี้มีคุณสมบัติที่มีองค์ประกอบอื่น ๆ ดังนั้นเราจึงระบุว่าเป็นชนิดที่มีความซับซ้อน องค์ประกอบย่อย "shiporder" องค์ประกอบ XS: ลำดับองค์ประกอบล้อมรอบกำหนดคำสั่งขององค์ประกอบย่อยนี้:

<xs:element name="shiporder">
<xs:complexType>
<xs:sequence>
...
</xs:sequence>
</xs:complexType>
</xs:element>

แล้วเราต้อง "orderperson" องค์ประกอบที่ถูกกำหนดให้เป็นประเภทที่เรียบง่าย (เพราะมันไม่ได้มีคุณลักษณะหรือองค์ประกอบอื่น ๆ ) ประเภท (XS: สตริง) จะนำหน้าด้วยบทบัญญัติ namespace คำนำหน้า namespace นี้และคำแนะนำที่กำหนดไว้ล่วงหน้าคีมาคีมาประเภทข้อมูล XML ที่เกี่ยวข้องกับ:

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

ต่อไปผมจะต้องมีสององค์ประกอบที่กำหนดไว้เป็นชนิดที่ซับซ้อน "shipto" และ "รายการ" เราเริ่มต้นด้วยการกำหนด "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!

ตอนนี้เราสามารถกำหนด "รายการ" องค์ประกอบของ องค์ประกอบนี้สามารถปรากฏหลายครั้งภายใน "shiporder" องค์ประกอบ นี้จะกระทำโดย "รายการ" maxOccurs องค์ประกอบค่าแอตทริบิวต์ถูกกำหนดเป็น "มากมาย" เพื่อให้บรรลุเพื่อที่ว่า "รายการ" องค์ประกอบที่สามารถเป็นผู้สร้างปรากฏหลายครั้งตามที่ต้องการ โปรดทราบ "โน้ต" องค์ประกอบเป็นตัวเลือก เรามี minOccurs แอตทริบิวต์ขององค์ประกอบนี้มีค่าเป็น 0 เมื่อ:

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

ตอนนี้เราสามารถประกาศแอตทริบิวต์ "shiporder" ขององค์ประกอบ เช่นนี้เป็นคุณลักษณะที่จำเป็นเราระบุการใช้ = "ต้อง"

หมายเหตุ: คุณสมบัตินี้จะต้องประกาศในวันสุดท้าย:

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

นี่คือรายการของเอกสารที่เรียกว่า "shiporder.xsd" แฟ้มสคีมา:

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


Split Schema

ในด้านหน้าของวิธีการออกแบบเป็นเรื่องง่ายมาก แต่มันก็เป็นเรื่องยากที่จะอ่านและเก็บรักษาเอกสารมี Shique ซับซ้อน

ถัดไปขึ้นอยู่กับวิธีการออกแบบที่อธิบายไว้ในความหมายแรกขององค์ประกอบทั้งหมดและคุณลักษณะและจากนั้นใช้โทษแอตทริบิวต์หมายถึงพวกเขา

วิธีการใหม่นี้ได้รับการออกแบบแฟ้มสคี ( "shiporder.xsd"):

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


ใช้ชนิดที่กำหนด (ชื่อประเภท)

วิธีการออกแบบที่สามกำหนดระดับหรือประเภทเพื่อให้เรามีความสามารถในการกำหนดองค์ประกอบนำมาใช้ใหม่ วิธีการเฉพาะคือองค์ประกอบแรกของชื่อองค์ประกอบที่ง่ายและซับซ้อนและจากนั้นพิมพ์แอตทริบิวต์ขององค์ประกอบที่จะชี้ให้กับพวกเขา

วิธีที่สามนี้คือการใช้ไฟล์คีมาออกแบบ ( "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>

องค์ประกอบข้อ จำกัด การแสดงชนิดของข้อมูลที่ได้มาจาก namespace ประเภทข้อมูล 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