Wednesday, 4 December 2013

XML Schema Definition with example


XSD (XML Schema Definition)
          
           A Recommendation of the World Wide Web Consortium, specifies how to formally describe the elements in an Extensible Markup Language document. This description can be used to verify that each item of content in a document adheres to the description of the element in which the content is to be placed.
          The XML Schema Definition is a reference library that provides an API for use with any code that examines, creates or modifies W3C XML Schema (standalone or as part of other artifacts, such as XForms or WSDL documents).
          XSD is a library that provides an API for manipulating the components of an XML Schema as described by the W3C XML Schema specifications, as well as an API for manipulating the DOM-accessible representation of XML Schema as a series of XML documents, and for keeping these representations in agreement as schemas are modified.
          XSD has several advantages over earlier XML schema languages, such as document type definition (DTD) or Simple Object XML (SOX). For example, it's more direct: XSD, in contrast to the earlier languages, is written in XML, which means that it doesn't require intermediary processing by a parser. Other benefits include self-documentation, automatic schema creation, and the ability to be queried through XML Transformations (XSLT). Despite the advantages of XSD, it has some detractors who claim, for example, that the language is unnecessarily complex.
e.g.
Hello.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<shiporder xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" orderid="12345" xsi:noNamespaceSchemaLocation="shiporder.xsd">
<orderperson>SBJ</orderperson>
<shipto>
<name>ABC</name>
<addr>23,Lane</addr>
<city>Kadegaon</city>
<country>India</country>
</shipto>
<item>
<title>ABCD</title>
<note>sed</note>
<quantity>2</quantity>
<price>10.90</price>
</item>
<item>
<title>EFGH</title>
<note>sdef</note>
<quantity>1</quantity>
<price>1.90</price>
</item>
</shiporder>

Hello.xsd
<?xml version="1.0"?>
<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="addr" 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"/>
                                                            <xs:element name="quantity" type="xs:string"/>
                                                            <xs:element name="price" type="xs:string"/>
                                                </xs:sequence>
                                                </xs:complexType>
                                    </xs:element>

ShareThis

Related Posts Plugin for WordPress, Blogger...