တစ်ခုရှိသော XSD အကိုင်း

ဤလုံးစံတွဲမှာ သင့်တော်မည့်အတွက် 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>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 စာရင်းသတင်းအရ အခြေခံအုပ်စု "shiporder" ကို အသုံးပြုထားသည်။ ဤ "shiporder" အခြေခံအုပ်စုတွင် "orderid" အမည်ရှိ အကျုံးဖြစ်သော အချက်အလက်များပါဝင်သည်။ "shiporder" အခြေခံအုပ်စုတွင် အသုံးပြုထားသော အသင်းအဖွဲ့များ သည် "orderperson"、"shipto" နှင့် "item" ဖြစ်သည်။ "item" အသင်းအဖွဲ့သည် နှစ်ကြိမ် ထွက်ပေါ်လာသည်။ ဤ "item" အသင်းအဖွဲ့တွင် "title" နှင့် အတိုင်းအတာ "note" အချက်အလက်များပါဝင်သည်။ အရင်းအမြစ် "quantity" နှင့် "price" အချက်အလက်များပါဝင်သည်。

ဤလက်ရှိမှာ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ကို သတ်မှတ်ထားသည်။ ဤလက်ရှိမှာ xsi:noNamespaceSchemaLocation="shiporder.xsd" ကို အစီအစဥ်ကို သတ်မှတ်ထားသည် (ဤအချိန်တွင် ဤ၏ အခြေခံအုပ်စုကို "shiporder.xml" နှင့် အတူ တူပြီ)。

创建一个 XML Schema

现在,我们需要为上面这个 XML 文档创建一个 schema。

我们可以通过打开一个新的文件来开始,并把这个文件命名为 "shiporder.xsd"。要创建schema,我们仅仅需要简单地遵循 XML 文档中的结构,定义我们所发现的每个元素。首先我们开始定义一个标准的 XML 声明:

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

在上面的 schema 中,我们使用了标准的命名空间 (xs),与此命名空间相关联的 URI 是 Schema 的语言定义(Schema language definition),其标准值是 http://www.w3.org/2001/XMLSchema。

နောက်ပိုင်းတွင် ကျမ်းသားက "shiporder" အအုပ်အချက်ကို အသုံးပြုရန် ဖို့ ရသည်။ ဒါအအုပ်အချက်က အချက်အလက်တစ်ခု နှင့် အခြား အအုပ်အချက်များ ပါဝင်သည်။ အရေးကြီးသော်လည်း ဒါအအုပ်အချက်က ပေါင်းစားအဖြစ် အသုံးပြုပြီး ဒါ၏ အသင်းအုပ်အချက်များ က xs:sequence အအုပ်အချက်မှ ပိုင်းခြားပြီး အသင်းအုပ်အချက်များ၏ အစီအစဉ်ကို အစိုးရပြီး ဖြစ်သည်:

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

နောက်ပိုင်းတွင် ကျမ်းသားက "orderperson" အအုပ်အချက်ကို အင်္ဂါအက်ဖ် အအုပ်အချက် (xs:string) အဖြစ် အသုံးပြုရန် ဖို့ ရသည်။ ဒါဟာ အအုပ်အချက်က အချက်အလက်များ သို့မဟုတ် အခြား အအုပ်အချက်များ ကို ကွင်းပြီး ဖြစ်သည်။ အချက်အလက်များ၏ ပထမဆုံးအသား ကို အမည်အဖွဲ့ အချက်အလက်က အစိုးရပြီး ဒါဟာ အမည်အဖွဲ့ အချက်အလက်က ဖြစ်သည်:

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

schema အပေါ်မှ,ကျမ်းသားက maxOccurs အချက်အလက် နှင့် minOccurs အချက်အလက်က အအုပ်အချက်များ ပေါ်ပေါက်နိုင်သော အပေါင်းစားကို အသုံးပြုနိုင်သည်။ maxOccurs အချက်အလက်က အအုပ်အချက်က ပေါ်ပေါက်နိုင်သော အပေါင်းစား၏ အမြင့်အရောက်ကို အစိုးရပြီး အမှတ်အသား အချက်အလက်က အအုပ်အချက်က ပေါ်ပေါက်နိုင်သော အပေါင်းစား၏ အနည်းဆုံးအပေါင်းစားကို အစိုးရပြီး ဒါဟာ အချက်အလက်များ၏ အချက်အလက်က အစိုးရပြီး 1 ဖြစ်သည်။

ယခုတွင်,ကျမ်းသား "item" အအုပ်အချက်ကို ကိုးကာ ရသည်။ ဒါဟာ "shiporder" အအုပ်အချက်အတွင်း အပိုသို့မဟုတ် အခြား အအုပ်အချက်များ ကို ပြန်လည်ဖော်ပြခြင်း ဖြစ်သည်။ ဒါဟာ "item" အအုပ်အချက်၏ maxOccurs အချက်အလက်၏ အကျိုးအေးချမ်းချက်ကို "unbounded" ဟု အစိုးရပြီး ဒါဟာ "item" အအုပ်အချက်က ကိုးကာ ကိုးကာ ရတဲ့ အခြား အအုပ်အချက်များ ကို ပြန်လည်ဖော်ပြခြင်း ဖြစ်သည်။ အမှန်တကယ် အကြင် "note" အအုပ်အချက်က အခွင့်အရေး အအုပ်အချက် ဖြစ်သည်။ ကျမ်းသားက ဒါအအုပ်အချက်၏ 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" ဖြစ်သော schema ဖော်ပြစာ၏ ဖော်ပြချက်

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

Schema ကိုလုပ်ကျင်းပါ

အခြားနည်းလမ်းသည် လွယ်ကူသည်များသာလျှင် နောက်ဆုံးတွင် အတန်ပျိုးရှိသည်များသာ လက်တွေ့ကြုံနိုင်ပြီး အတန်ပျိုးရှိသည်များသာ ကြိုးစားနိုင်ပြီး

အခြားနည်းလမ်းကိုလည်း အစားထိုးပြီး အချက်အလက်များအား အချက်အလက်အသုံးပြုပြီး နောက်ပြီးတွင် ref အချက်အလက်ကိုကူညီပြီး

အသစ်သုံးစွဲသောနည်းလမ်းဖြင့်ဖွဲ့စည်းထားသော 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:attribute name="orderid" type="xs:string"/>
ပေါင်းစပ်ပုံအချက်အလက်ကြောင်း
<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>

သတ်မှတ်ထားသော အမျိုးအစား (အမည်ပြုထားသော အမျိုးအစား) ကို အသုံးပြုခြင်း

တတိယ နည်းလမ်း ဖွဲ့စည်းထားသော ပုံစံ သို့မဟုတ် အမျိုးအစား ကို သတ်မှတ်သည်။ အကျိုးသက်ရောက်မှု မှာ အဆိုပါ အစိတ်အပိုင်း နှင့် ပေါင်းစပ်ထားသော အစိတ်အပိုင်း ကို အမည်ပြုခြင်း ဖြစ်၍၊ ပြီးတော့ အစိတ်အပိုင်း၏ 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>

restriction အသုံးပြုသည် ဝီကီစီအမည်အသုံးများတွင် သတ္တိပုံစံကို အစိတ်အစိတ်များကို ကိုက်နှိပ်လိုက်သည်။ အရာဝတ္တိကို အသုံးပြုသည် အကိရိယာများသို့မူ အရာဝတ္တိအမျိုးအစား အကျယ်အဝန်းကို သတ်မှတ်လိုက်သည်:

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

restriction အသုံးပြုလိုက်သည် အသုံးပြုသော အကိရိယာများကို ကိန်းကိုင်သည်။ အောက်ပါ အစိတ်အစိတ်များကို ကျွန်ုပ်ကိုယ်မှ ပြုပြင်ထားပါသည်:

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

အဆိုပါ ကြော်ငြာ ကို အကိုင်း သို့မဟုတ် အချက်အလက် အသုံးပြုသည် နှင့် တခုခု သာ လက်ဆင့်ခဲ့သော ချက်အရာ ခုနစ် ချက်အရာ ဖြစ်ပေသည်။ ထို ချက်အရာ သည် ၀-၉ မှ ၀-၉ အရှိသည် ဖြစ်ပေသည်။