How to use Schema in XML

This article tell you how to define XML Schema in XML
  • 2181

How to use XML Schema

You may already learn about DTDs. DTDs are and XML Schema both are similar to each. In that they are used to create classes of XML documents. The work of DTDs and Xml Schema to define the structure of XML document. But DTDs is the previous it has the some flow. Although DTDs are still common, XML Schema is a much more powerful language.

 Means of understanding the power of XML Schema first see the limitation of DTDs

  • DTDs have not built-in data types.

  • It is also not support user-derived data type

  • In DTDs does not support multiple declarations in parenthesis.

To overcome these flow use XML Schema

First create simple XML document

<?xml version="1.0"?>
<note>
  <to>pulkit</to>
  <from>vijay</from>
  <heading>Remember me</heading>
  <body>Don't forget me this year</body>
</note>

Note: This is a simple XML document

Simple DTDs declaration 

<!ELEMENT note (to, from, heading, body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>

Note: This is a simple DTDs declaration

XML Schema file

Example

<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="note">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="to" />
        <xs:element ref="from" />
        <xs:element ref="heading" />
        <xs:element ref="body" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="to" type="xs:string" />
  <xs:element name="from" type="xs:string" />
  <xs:element name="heading" type="xs:string" />
  <xs:element name="body" type="xs:string" />
</xs:schema> 

Note: This is the Example of XML Schema

A Reference to a DTD

<?xml version="1.0"?>
<!DOCTYPE note SYSTEM
"D:\satya prakash\dtd note.txt">
<note>
  <to>pulkit</to>
  <from>vijay</from>
  <heading>Remember me</heading>
  <body>Don't forget me this  year</body>
</note>

Note: This is reference type of DTD means we make DTD in system directory and used it in the XML document.

Further Readings

You may also want to read these related articles :

Ask Your Question 

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

Programming Answers here

© 2020 DotNetHeaven. All rights reserved.