XSD misoli


Ushbu bobda XML sxemasini qanday yozish ko‘rsatiladi. Shuningdek, sxemani turli usullarda yozish mumkinligini bilib olasiz.


ULASHISH

XML hujjat

Keling, "shiporder.xml" nomli ushbu XML hujjatni ko‘rib chiqamiz:

<?xml version="1.0" encoding="UTF-8"?> <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>

Yuqoridagi XML hujjat "orderid" nomli majburiy atributga ega "shiporder" ildiz elementidan iborat. "shiporder" elementi uchta turli bola elementni o‘z ichiga oladi: "orderperson", "shipto" va "item". "item" elementi ikki marta keladi va u "title", ixtiyoriy "note" elementi, "quantity" va "price" elementini o‘z ichiga oladi.

Yuqoridagi xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" qatori XML parserga bu hujjat sxema bo‘yicha tekshirilishi (validate) kerakligini bildiradi. xsi:noNamespaceSchemaLocation="shiporder.xsd" qatori esa sxema QAYERDA joylashganini belgilaydi (bu yerda u "shiporder.xml" bilan bir papkada).



XML sxemasini yaratish

Endi yuqoridagi XML hujjat uchun sxema yaratmoqchimiz.

Ishni "shiporder.xsd" deb ataladigan yangi faylni ochishdan boshlaymiz. Sxemani yaratish uchun shunchaki XML hujjatdagi tuzilmaga amal qilib, har bir elementni uchragan tartibda aniqlashimiz mumkin. Standart XML e’lonidan, undan keyin esa sxemani aniqlaydigan xs:schema elementidan boshlaymiz:

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

Yuqoridagi sxemada standart nomlar fazosidan (xs) foydalanamiz, bu nomlar fazosi bilan bog‘langan URI esa Schema tili ta’rifi bo‘lib, uning standart qiymati http://www.w3.org/2001/XMLSchema.

Keyin "shiporder" elementini aniqlashimiz kerak. Bu element atributga ega va boshqa elementlarni o‘z ichiga oladi, shuning uchun uni murakkab tip deb hisoblaymiz. "shiporder" elementining bola elementlari quyi elementlarning tartiblangan ketma-ketligini aniqlaydigan xs:sequence elementi ichiga olingan:

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

So‘ngra "orderperson" elementini oddiy tip sifatida aniqlashimiz kerak (chunki u hech qanday atribut yoki boshqa elementlarni o‘z ichiga olmaydi). Tip (xs:string) oldiga XML Schema bilan bog‘langan, oldindan belgilangan sxema ma’lumot tipini bildiruvchi nomlar fazosi prefiksi qo‘yilgan:

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

Keyin murakkab tipdagi ikkita elementni aniqlashimiz kerak: "shipto" va "item". "shipto" elementini aniqlashdan boshlaymiz:

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

Sxemalarda maxOccurs va minOccurs atributlari yordamida element necha marta kelishi mumkinligini aniqlashimiz mumkin. maxOccurs element kelishining maksimal sonini, minOccurs esa minimal sonini belgilaydi. maxOccurs va minOccurs’ning standart qiymati 1 ga teng!

Endi "item" elementini aniqlashimiz mumkin. Bu element "shiporder" elementi ichida bir necha marta kelishi mumkin. Bu "item" elementining maxOccurs atributini "unbounded" ga o‘rnatish orqali belgilanadi, ya’ni "item" elementi muallif xohlaganicha ko‘p marta kelishi mumkin. E’tibor bering, "note" elementi ixtiyoriy. Buni minOccurs atributini nolga o‘rnatish orqali belgiladik:

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

Endi "shiporder" elementining atributini e’lon qilishimiz mumkin. Bu majburiy atribut bo‘lgani uchun use="required" ni ko‘rsatamiz.

Eslatma: Atribut e’lonlari doimo eng oxirida kelishi kerak:

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

Mana "shiporder.xsd" nomli sxema faylining to‘liq matni:

<?xml version="1.0" encoding="UTF-8" ?> <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>

Sxemani bo‘laklarga ajratish

Oldingi loyihalash usuli juda oddiy, lekin hujjatlar murakkab bo‘lganda uni o‘qish va qo‘llab-quvvatlash qiyin bo‘lishi mumkin.

Keyingi loyihalash usuli avval barcha elementlar va atributlarni aniqlashga, so‘ngra ularga ref atributi yordamida havola qilishga asoslangan.

Mana sxema faylining ("shiporder.xsd") yangi tuzilishi:

<?xml version="1.0" encoding="UTF-8" ?> <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>

Nomlangan tiplardan foydalanish

Uchinchi loyihalash usuli element ta’riflaridan qayta foydalanish imkonini beruvchi sinflar yoki tiplarni aniqlaydi. Bu simpleTypes va complexTypes elementlariga nom berib, so‘ngra elementning type atributi orqali ularga ishora qilish yo‘li bilan amalga oshiriladi.

Mana sxema faylining ("shiporder.xsd") uchinchi tuzilishi:

<?xml version="1.0" encoding="UTF-8" ?> <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 elementi ma’lumot tipi W3C XML Schema nomlar fazosidagi ma’lumot tipidan hosil qilinganini bildiradi. Demak, quyidagi fragment element yoki atribut qiymati string qiymat bo‘lishi kerakligini bildiradi:

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

restriction elementi ko‘pincha elementlarga cheklovlar qo‘llash uchun ishlatiladi. Yuqoridagi sxemadan olingan quyidagi qatorlarga qarang:

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

Bu element yoki atribut qiymati string bo‘lishi, ketma-ket aynan oltita belgidan iborat bo‘lishi va bu belgilar 0 dan 9 gacha bo‘lgan raqamlar bo‘lishi kerakligini bildiradi.


W3Schools Pathfinder

Yutuqlaringizni kuzating – bu bepul!