Use of XML Schema in XML

In this article I am going to explain about XML Schema.
  • 2257

XML Schema

An XML schema show the structure of XML Document. XML Schema is an XML based alternative to DTD. XML schema is also referred to XML schema definition. An XML schema is more powerful than Document type Definition (DTD).

The purpose of XML schema is:-

  • defines how many element appear in XML document.
  • defines how many attribute appear in XML document.
  • defines no of child element in XML document.
  • defines the number of child elements
  • defines the which element is child element.
  • defines whether an element is empty or can include text
  • defines data types of elements and attributes
  • defines default and fixed values for elements and attributes

Example

An XML Document

<?xml version="1.0" encoding="ISO-8859-1"?>

<Demo orderid="889923"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="Demo.xsd">
  <orderperson>prabhakar</orderperson>
  <detail>
    <name>prabhat</name>
    <address>Delhi</address>
    <city>4000 Delhi</city>
    <country>India</country>
  </detail>
  <item>
    <title>Employee detail</title>
    <company>MCN solution</company>
    <salary>10000</salary>
  </item>
</Demo>

 

Schema of above XML Document.

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Demo">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="orderperson" type="xs:string"/>
      <xs:element name="detail">
        <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="company" type="xs:string" minOccurs="0"/>
            <xs:element name="salary" 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>

 

Further Readings

You may also want to read these related articles: here

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

© 2020 DotNetHeaven. All rights reserved.