一个 XSD 实例

Ben jie hui wei ni shi xian ru he xie bing XML Schema. Ni hai jing xue hui xie schema de bu tong fang fa.

XML wen juan

Ran la wo men kan kan zhe ming wei "shiporder.xml" de XML wen juan:

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

Yan na XML wen juan bao huan gen yuan yuan yu "shiporder", zhong huan bao huan bi ming wei "orderid" de shu xing. "shiporder" yuan yu bao huan san ge bu tong de zi yuan yu: "orderperson", "shipto" yi ji "item". "item" yuan yu chuan xian liang ci, ta huan huan yi ge "title", yi ge xu xuan "note" yuan yu, yi ge "quantity" yi ji "price" yuan yu.

Yan na yan zai yan, xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance", ba yin xi hua XML jie xi qi gen jun ge schema lai yan zheng ci wén juan. Yan na hang: xsi:noNamespaceSchemaLocation="shiporder.xsd" gui ding le schema de wei zhi (zai zhe li, ta yi hui "shiporder.xml" chu zai tai yi ge wu jian).

An kafa XML Schema

Kamfanin, a na iya zama bayyana schema na yanzu na XML.

A na iya zama bayyana da sabon wuri don fara, kuma a na iya zama "shiporder.xsd". Don samar da schema, a na iya zama bayyana da jiki na XML wanda yana da wuri na na'urin, wanda ya bayyana kowane yanki na a iya samar da. A na baya, a na iya fara bayyana da jiki na na'urin na na'urin na na'urin:

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

A cikin schema na yanzu, a na iya samar da wuri na na'urin (xs), wanda yana da alhakin wuri na na'urin na sabon nau'i (Schema language definition), wanda yana da wuri na na'urin na na'urin na na'urin na na'urin: http://www.w3.org/2001/XMLSchema.

Kamfanin, a na iya zama bayyana "shiporder" yanki. Wannan yanki yana da wuri da ya ɗauki yankuna, kuma haka a na iya zama nau'i na yadda yana cikin. 'Yan yanki na "shiporder" zai iya zama da xs:sequence yanki, wanda ya bayyana 'yan yanki na yadda su za a iya zama:


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

Kamfanin, a na iya zama bayyana "orderperson" yanki don nau'i na yadda yana cikin (zai iya zama yanki na yadda ba a iya samar da ina ko yanki na dandalin). Nau'i (xs:string) na farko yana cikin hadadda da kwanan wuri na na'urin, wanda yana da alhakin wuri na sabon nau'i na XML schema da ya haɗa da XML schema na sabon nau'i na XML schema:

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

Kamfanin, a na iya zama bayyana biyu yanki don nau'i: "shipto" da "item". A na baya bayyana "shipto" yanki:

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

Guda kai, a na iya samar da sabon wuri don "schema", a na iya samar da maxOccurs da minOccurs don bayyana kuma yadda yanki zai iya zama. maxOccurs yana bayyana kuma yanki zai iya zama kaiyawa, kuma minOccurs yana bayyana kuma yanki zai iya zama kaiyawa. maxOccurs da minOccurs na iya zama 1!

Kuma yanzu, a na iya zama bayyana "item" yanki. Wannan yanki zai "shiporder" yanki yana iya fiyar jumla. Haka sannan, a na sanya ina cikakken "item" yanki maxOccurs cikakken "unbounded", kuma haka "item" yanki zai iya fiyar kaiyawa yadda kwaya yadda kwaya. Kware, "note" yanki yana iya zama yanki da a yiwa izi. A na sanya ina cikakken minOccurs cikakken 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>

xian zai, women keyi sheng ming "shiporder" yuan su de shu xing le. youyu zhe shi yi ge bi xuan shu xing, women gui ding use="required".

zhu shi:ci shu xing de sheng ming bi xu bei zhu zai zui hou:

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

zhe shi zhe ge ming wei "shiporder.xsd" de schema wen jian de wen dan qing dan:

<?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:element name="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>
   
   <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>

fen ge Schema

qianmian de she ji fang fa feichang rongyi, dan dang wen ju hen fu za zhi shi que nan yu du du he wei hui.

xiayi jieshao de she ji fang fa juyu zai xian shi dui duo ge yuan su he shu xing de ding yi, ran hou zai shi yong ref shu xing lai zhuan yin tamen.

wannan shi yong xin fang fa she ji de schema wen jian:

<?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:sequence>
   <xs:element ref="name"/>
   
   
   
  </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>

إلتماسم يظهر أن نوع البيانات يأتي من نوع بيانات اسم فضاء 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 的数字。