Saturday 3 December 2016

XSD

XML
XML stands for extensible markup language. XML is designed for transport and store data. XML tags are case sensitive. <Message>This is incorrect</message> but <message>This is correct</message>
HTML was designed for display data.

XML Schema
An XML schema describes the structure of an XML document. An XML schema is also called XML schema definition.
An XML schema:
·         Defines elements that can appear in a document.
·         Defines attributes that can appear in a document.
·         Defines which element are child elements.
·         Defines the order of child elements.

XML schemas are Extensible because they are written in XML:
·         Reuse your schema in other schema.
·         Create your own data types derived from the standard types.
·         Reference multiple schemas in the same document


Sample XSD:
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/Payment" xmlns:tns="http://www.example.org/Payment" elementFormDefault="qualified" xmlns:Q1="http://xmlns.telenor.mm/Schema/Common/1.0/Common.xsd">

<import schemaLocation="common.xsd" namespace="http://xmlns.telenor.mm/Schema/Common/1.0/Common.xsd">
</import>
              <element name="ReversalReq" type="string"></element>
                <element name="ReversalRes" type="string"></element>
                <complexType name="ReqType">
                                <sequence>
                                                <element ref="Q1:TransactionReference"></element>
                                                <element ref="Q1:CustomRef"></element>
                                </sequence>
                </complexType>
                <complexType name="ResType">
                <sequence>
                                                <element ref="Q1:TransactionReference"></element>
                                                <element ref="Q1:CustomRef"></element>
                                </sequence>
                </complexType>
</schema>


XSD Element
A simple element is an XML Element that contains only text. It can’t contain any other elements or attributes.
Simple XML elements
       Corresponding simple element definations
<lastname>Pramanik</lastname>         
<firstname>Sandip</ firstname >
<age>29</age>
<city>Kolkata</ city >
 <xs:element name=”lastname” type=”xs:string”/>
<xs:element name=” firstname” type=”xs:string”/>
<xs:element name=” age” type=”xs:integer”/>
<xs:element name=” city” type=”xs:string”/>



XSD Attribute
Attribute can contain other elements and mixture of elements.
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book>
  <title lang="en">Harry Potter</title>
  <price>29.99</price>
</book>
<book>
  <title lang="en">Learning XML</title>
  <price>39.95</price>
</book>
</bookstore>
                                                                                                                   lang- Attribute

                                                                                                                   price-Element


What is XML Namespace?
XML namespace provide a method to avoid element name conflicts.
<table>
        <tr>
                <td>Apple</td>
                <td>Banana</td>
        </tr>
</table>
<table>
        <tr>
                <name>Apple</td>
                <type>Fruit</td>
        </tr>
</table>
Solving the name conflict using a prefix
<h:table>
        <h:tr>
                <h:td>Apple</h:td>
                <h:td>Banana</h:td>
        </h:tr>
</h:table>
<f:table>
        <f:tr>
                <f:name>Apple</f:td>
                <f:type>Fruit</f:td>
        </f:tr>
</f:table>


Default Namespace:
Defining a default namespace for an element saves us from using prefixes in all the child elements.
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema">
</schema>


Target Namespace:
The schema will be assigned to the namespace http://www.example.org/Payment
If different teams start working on different files, then you have the possibility of name clashes, and it would not always be obvious where a definition had come from. The solution is to place the definition for each schema file within a distinct namespace.

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace=http://www.example.org/Payment
 xmlns:tns="http://www.example.org/Payment" elementFormDefault="qualified" xmlns:Q1="http://xmlns.telenor.mm/Schema/Common/1.0/Common.xsd">

In WSDL we are to call that targetNamespace.
<types>
                <schema>
                                <import namespace=”http://www.example.org/Payment” schemaLocation=”xyz.xsd”/>
                </schema>
</types>



elementFormDefault="qualified" indicates that any elements used by the XML instance document which were declared in the schema must be namespace qualified.



Simple Type VS Complex Type
<element name=”email”>
                <simpleType>
                                <restriction base=”xs:string”/>
                </simpleType>
</element>
<element name=”Preferences”>
                <complexType>
                                <sequence>
                                                <element name=”email” type=”email”/>
                                </sequence>
                </ complexType >
</element>

#Simple types can only have content directly contained between the elements opening and closing tags.
#Complex types can have attributes, can contain other elements, and can contain a mixture of elements.
#Simple types will not contain other elements, they only contain data.
#In Complex types, allow elements in their content and may carry attributes.
#In Simple types cannot have element content and cannot carry attributes.


Including brings in definitions that belong to the same target namespace as the enclosing schema element.

Importing brings in definitions that belong to a different target namespace from the enclosing schema element.

No comments:

Post a Comment