एक एक्सएसडी उदाहरण
यह अध्याय प्रदर्शित करेगा कि XML स्कीमा कैसे लिखना है। आप यह भी सीखेंगे कि एक स्कीमा को विभिन्न तरीकों से लिखा जा सकता है।
एक एक्सएमएल दस्तावेज़
आइए इस XML दस्तावेज़ पर एक नज़र डालते हैं जिसे "shiporder.xml" कहा जाता है:
<?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>
उपरोक्त एक्सएमएल दस्तावेज़ में एक मूल तत्व, "शिपऑर्डर" होता है, जिसमें "ऑर्डरिड" नामक एक आवश्यक विशेषता होती है। "शिपऑर्डर" तत्व में तीन अलग-अलग बाल तत्व होते हैं: "ऑर्डरपर्सन", "शिप्टो" और "आइटम"। "आइटम" तत्व दो बार प्रकट होता है, और इसमें एक "शीर्षक", एक वैकल्पिक "नोट" तत्व, एक "मात्रा" और एक "मूल्य" तत्व होता है।
उपरोक्त पंक्ति: xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" XML पार्सर को बताती है कि इस दस्तावेज़ को एक स्कीमा के विरुद्ध मान्य किया जाना चाहिए। पंक्ति: xsi:noNamespaceSchemaLocation="shiporder.xsd" निर्दिष्ट करती है कि स्कीमा कहाँ स्थित है (यहाँ यह "shiporder.xml" के समान फ़ोल्डर में है)।
एक एक्सएमएल स्कीमा बनाएं
अब हम उपरोक्त XML दस्तावेज़ के लिए एक स्कीमा बनाना चाहते हैं।
हम एक नई फाइल खोलकर शुरू करते हैं जिसे हम "shiporder.xsd" कहेंगे। स्कीमा बनाने के लिए हम एक्सएमएल दस्तावेज़ में संरचना का पालन कर सकते हैं और प्रत्येक तत्व को परिभाषित कर सकते हैं जैसा हम पाते हैं। हम मानक XML घोषणा के साथ शुरू करेंगे, उसके बाद xs:schema तत्व जो एक स्कीमा को परिभाषित करता है:
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
...
</xs:schema>
ऊपर दिए गए स्कीमा में हम मानक नाम स्थान (xs) का उपयोग करते हैं, और इस नाम स्थान से संबद्ध URI स्कीमा भाषा परिभाषा है, जिसका मानक मान http://www.w3.org/2001/XMLSchema है।
अगला, हमें "शिपऑर्डर" तत्व को परिभाषित करना होगा। इस तत्व में एक विशेषता होती है और इसमें अन्य तत्व होते हैं, इसलिए हम इसे एक जटिल प्रकार मानते हैं। "शिपऑर्डर" तत्व के बाल तत्व एक xs: अनुक्रम तत्व से घिरे होते हैं जो उप तत्वों के क्रमबद्ध अनुक्रम को परिभाषित करता है:
<xs:element name="shiporder">
<xs:complexType>
<xs:sequence>
...
</xs:sequence>
</xs:complexType>
</xs:element>
फिर हमें "ऑर्डरपर्सन" तत्व को एक साधारण प्रकार के रूप में परिभाषित करना होगा (क्योंकि इसमें कोई विशेषता या अन्य तत्व नहीं होते हैं)। प्रकार (xs:string) XML स्कीमा से जुड़े नामस्थान उपसर्ग के साथ उपसर्ग है जो एक पूर्वनिर्धारित स्कीमा डेटा प्रकार को इंगित करता है:
<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>
स्कीमा के साथ हम maxOccurs और minOccurs विशेषताओं वाले तत्व के लिए संभावित घटनाओं की संख्या को परिभाषित कर सकते हैं। maxOccurs किसी तत्व के लिए घटनाओं की अधिकतम संख्या निर्दिष्ट करता है और minOccurs किसी तत्व के लिए न्यूनतम संख्या को निर्दिष्ट करता है। maxOccurs और minOccurs दोनों के लिए डिफ़ॉल्ट मान 1 है!
अब हम "आइटम" तत्व को परिभाषित कर सकते हैं। यह तत्व "शिपऑर्डर" तत्व के अंदर कई बार प्रकट हो सकता है। यह "आइटम" तत्व की maxOccurs विशेषता को "अनबाउंड" पर सेट करके निर्दिष्ट किया जाता है जिसका अर्थ है कि लेखक की इच्छा के अनुसार "आइटम" तत्व की कई घटनाएं हो सकती हैं। ध्यान दें कि "नोट" तत्व वैकल्पिक है। हमने minOccurs विशेषता को शून्य पर सेट करके इसे निर्दिष्ट किया है:
<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:attribute name="orderid" type="xs:string" use="required"/>
यहाँ "shiporder.xsd" नामक स्कीमा फ़ाइल की पूरी सूची है:
<?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>
स्कीमा को विभाजित करें
पिछली डिज़ाइन विधि बहुत सरल है, लेकिन दस्तावेज़ जटिल होने पर इसे पढ़ना और बनाए रखना मुश्किल हो सकता है।
अगली डिज़ाइन विधि पहले सभी तत्वों और विशेषताओं को परिभाषित करने पर आधारित है, और फिर रेफरी विशेषता का उपयोग करके उन्हें संदर्भित करती है।
यहाँ स्कीमा फ़ाइल का नया डिज़ाइन है ("shiporder.xsd"):
<?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>
नामांकित प्रकारों का उपयोग करना
तीसरी डिज़ाइन विधि कक्षाओं या प्रकारों को परिभाषित करती है, जो हमें तत्व परिभाषाओं का पुन: उपयोग करने में सक्षम बनाती है। यह सरल प्रकार और जटिल प्रकार तत्वों का नामकरण करके किया जाता है, और फिर तत्व के प्रकार विशेषता के माध्यम से उन्हें इंगित करता है।
यहाँ स्कीमा फ़ाइल का तीसरा डिज़ाइन है ("shiporder.xsd"):
<?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>
प्रतिबंध तत्व इंगित करता है कि डेटाटाइप W3C XML स्कीमा नेमस्पेस डेटाटाइप से लिया गया है। तो, निम्नलिखित खंड का अर्थ है कि तत्व या विशेषता का मान एक स्ट्रिंग मान होना चाहिए:
<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 तक की संख्या होनी चाहिए।