XSD အစိတ် အကျဉ်းချုပ်

ဤလုံးပါ သင့်ကို XML Schema ကို ရေးသား ကြိုးပမ်းပါ၏။ သင့်ကို 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" အပေါ်အခြေခံ အီအိုင် ပါဝင်သည်၊ အရာဝတ္တု "orderid" အမည် ပါသည်။ "shiporder" အီအိုင် သည် အခြား အီအိုင် အသုံးပြုသည်: "orderperson"、"shipto" နှင့် "item" အခြား အီအိုင် အသုံးပြုသည်။ "item" အီအိုင် သည် အခြား အီအိုင် ပါဝင်သည်၊ သူ့ကို "title" နှင့် ဆိုင်သည် "note" အခြား အီအိုင် အသုံးပြုသည် ပြီး "quantity" နှင့် "price" အီအိုင် ပါဝင်သည်。

ဤအကြိမ် xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" သည် XML တခုခု ကို schema တစ်ခု အတိုင်း စစ်ဆေး ပြီး ပြောဆိုသည်။ ဤအကြိမ် xsi:noNamespaceSchemaLocation="shiporder.xsd" သည် schema အချင်းအရာ နေရာကို ကိုးကားသည် (အခြား "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" 元素。此元素拥有一个属性,其中包含其他的元素,因此我们将它认定为复合类型。"shiporder" 元素的子元素被 xs:sequence 元素包围,定义了子元素的次序:

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

然后我们需要把 "orderperson" 元素定义为简易类型(这是因为它不包含任何属性或者其他的元素)。类型 (xs:string) 的前缀是由命名空间的前缀规定的,此命名空间与指示预定义的 schema 数据类型的 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>

schema အရ၌၊ အအုပ်အချက်၏ maxOccurs အကိုးးအချုပ်အချက် နှင့် minOccurs အကိုးးအချုပ်အချက်ကို သုံးပြီး အအုပ်အချက်၏ ပေါ်ပေါက်နိုင်သော အပိုင်းအခြား အပိုင်းများ အသုံးပြုနိုင်ပါသည်။ maxOccurs အကိုးးအချုပ်အချက်သည် အအုပ်အချက်၏ ပေါ်ပေါက်နိုင်သော အပိုင်းအခြား အပိုင်းများ၏ အမြင့်အမြတ် အချက်အလက် ဖြစ်သည်၊ သို့သော် minOccurs အကိုးးအချုပ်အချက်သည် အအုပ်အချက်၏ ပေါ်ပေါက်နိုင်သော အပိုင်းအခြား အပိုင်းများ၏ အနည်းဆုံး အချက်အလက် ဖြစ်သည်။ maxOccurs နှင့် minOccurs အကိုးးအချုပ်အချက်၏ အချိန်ချိန် အချက်အလက်မှာ 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 name="inttype">
 <xs:restriction base="xs:positiveInteger"/>

<xs:simpleType name="dectype">
 <xs:restriction base="xs:decimal"/>


 
  
 

<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 အရာသည် ရှိသော သတင်းပုံစံ အမှတ်အသား အသင်းဝင် သတင်းပုံစံ အမှတ်အသား မှ စတင်ပြီး သတင်းပုံစံ အမှတ်အသား ဖြစ်သည့် သတင်းပုံစံ အမှတ်အသား ကို ပြသသည်။ အရာဝတ္တုသို့မဟုတ် အခြား အရာဝတ္တု၏ အသုံးချမှု သည် အကြောင်းပြချက် အပေါ် ဖြစ်သည်။


restriction 元素常被用于向元素施加限制。请看下面这些来自以上 schema 的片段:


 
  
 

这段代码指示出,元素或属性的值必须为字符串,并且必须是连续的六个字符,同时这些字符必须是 0-9 的数字。