Tuesday 15 January 2019

Getting Started with XSD

XSD:

1.  An XML document that is described by an XML schema is called an instance document.
2.  the schema of XML Schema is substantially longer than the document it describes or defines.

<?xml version="1.0"?>
<Book>
    <Title>XML Schema Essentials</Title>
<Authors>
    <Author>R. Allen Wyke</Author>
    <Author>Andrew Watt</Author>
</Authors>
<Publisher>John Wiley</Publisher>
</Book>


4. Declaring Elements and Defining Types:
there is a substantive difference between declaring an element that has content that is either a simple type or complex type and is permitted to occur in an instance document
and  defining a new type, which can be either simple type or complex type.


4.1 Defining Simple Types:

XSD Schema provides three flavors of simple types:
    1. Atomic types
    2. List types
    3. Union types


------------------------------
Simple Types:

1. Atomic Simple Type:

   
   
<ShippingAddress type="EUAddress">
    <Name>
        <FirstName>Hans</FirstName>
        <LastName>Schmidt</LastName>
    </Name>
    <Address>
        <Street>123 Hallgarten</Street>
        <City>Berlin</City>
        <PostalCode>12345</PostalCode>
        <Country>DE</Country>
    </Address>
</ShippingAddress>

---->

<?xml version='1.0'?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<xsd:element name="ShippingAddress">
    <xsd:complexType>
        <xsd:sequence>
        <xsd:element name="Name" type="NameType"/>
        <xsd:element name="Address" type="AddressType"/>
        </xsd:sequence>
        <xsd:attribute name="type" type="xsd:string"/>
    </xsd:complexType>
</xsd:element>

<xsd:complexType name="NameType">
    <xsd:sequence>
        <xsd:element name="FirstName" type="xsd:string"/>
        <xsd:element name="LastName" type="xsd:string"/>
    </xsd:sequence>
</xsd:complexType>

<xsd:complexType name="AddressType">
    <xsd:sequence>
        <xsd:element name="Street" type="xsd:string"/>
        <xsd:element name="City" type="xsd:string"/>
        <xsd:element name="PostalCode" type="PostalCodeType"/>
        <xsd:element name="Country" type="CountryType"/>
    </xsd:sequence>
</xsd:complexType>

<xsd:simpleType name="PostalCodeType">
    <xsd:restriction base="xsd:integer">
    <xsd:length value="5" />
    </xsd:restriction>
</xsd:simpleType>

<xsd:simpleType name="CountryType">
    <xsd:restriction base="xsd:string">
    <xsd:length value="2"/>
    </xsd:restriction>
</xsd:simpleType>

</xsd:schema>
   


<xsd:attribute name="maxOccurs">
<xsd:simpleType>
<xsd:union>
<xsd:simpleType>
<xsd:restriction base='xsd:nonNegativeInteger'/>
</xsd:simpleType>
<xsd:simpleType>
<xsd:restriction base='xsd:string'>
<xsd:enumeration value='xsd:unbounded'/>
</xsd:restriction>
</xsd:simpleType>
</xsd:union>
</xsd:simpleType>
</xsd:attribute>

   
--> Getting Started with Complex Type   
   
<?xml version='1.0'?>
<Book PubCountry="USA">
XML Schema Essentials, R. Allen Wyke, Andrew Watt, Wiley
</Book>

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" >
<xsd:element name="Book">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute name="PubCountry" type="xsd:string"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
</xsd:schema>   

-------- Anonymous Complex Type

<?xml version='1.0'?>
<SimpleInvoice>
    <Customer>
    <CustomerName>XMML.com</CustomerName>
    </Customer>
    <LineItems>
        <LineItem quantity="2">Mandrake Linux version 8</LineItem>
        <LineItem quantity="1">IBM WebSphere Studio Workbench version 4</LineItem>
    </LineItems>
</SimpleInvoice>


<?xml version='1.0'?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<!-- anonymous complex type -->
<xsd:element name="SimpleInvoice">
    <xsd:complexType>
        <xsd:sequence>
            <xsd:element name="Customer" type="CustomerType"/>
            <xsd:element name="LineItems" type="LineItemsType"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:element>

<!-- named complex type -->
<xsd:complexType name="CustomerType">
    <xsd:sequence>
        <xsd:element name="CustomerName" type="xsd:string"/>
    </xsd:sequence>
</xsd:complexType>

<xsd:complexType name="LineItemsType">
    <xsd:sequence>
        <xsd:element ref="LineItem" minOccurs="1" maxOccurs="unbounded"/>
    </xsd:sequence>
</xsd:complexType>

<xsd:element name="LineItem">
    <xsd:complexType>
        <xsd:simpleContent>
            <xsd:extension base="xsd:string">
                <xsd:attribute name="quantity" type="xsd:string"/>
            </xsd:extension>
    </xsd:simpleContent>
</xsd:complexType>

</xsd:element>
</xsd:schema>

------------------------------------

No comments:

Post a Comment