What XML Schema and How to Write Simple XML schema

Leave a Comment

The XML schemas are used to represnt the structure of XML document. The goal or purpose of XML schema is to define the building of an XML document. These can be used as an alternative to XML DTD. The XML schema language is called as XML Schema Definition (XSD) language. The XML schema became the World Widw Web Consortium (W3C) recommendation in 2001.

XML schema defines elements, attributes, elements having child elements and attributes.

XML schema also allows the developer to use data types.

Step 1: We will first write a simple xsd file in which the desired structure of the XML document is defined. I have named it as StudentShema.xsd.

This file contains the complex type with four child simple type elements.

XML Schema [Student Schema.xsd]
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs = "http://wwww.w3.org/2001/XMLSchema">
  <xs:element name = "Student">
    <xs:complexType>
 <xs:sequence>
    <xs:element name = "name" type = "xs:string"/>
<xs:element name = "address" type = "xs:string"/>
<xs:element name = "std" type = "xs:string"/>
<xs:element name = "marks" type = "xs:string"/>
 </xs:sequence>
</xs:complexType>
  </xs:element>
</xs:schema>

This xs is qualifier used to identify the schema elements and types. The document element of Schema is xs:schema is the root element. It takes the attribute xmlns:xs which has the value http://www.w3.org/2001/XMLSchema. This declaration indicates that document should follow the rules of XML schema. The XML schema rules defined by the W3 recommendation in year 2001.

Then comes xs:element which is used to define the xml element. In above case the element Student is of complex type who have four child elements : name, address, std and marks. All these elements are of simple type string.

Step 2: Now develop XML document in which the desired values to the XML elements can be given. I have named this file as MySchema.xml

XML Document 5.11 [MySchema.xml]
<?xml version="1.0" encoding="utf-8"?>
<Student xmlns:xsi = "http://wwww.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation = "StudentSchema.xsd">
<name>Anand</name>
<address>Pune</address>
<std>Second</std>
<marks>70 percent</marks>
</Student>

In above XML document, the first line is typical in which the version and enconding is specified. There is xmlns:xsi attribute of document which indicates that XML document is an instance of XML schema. And it has come from the namespace "http://wwww.w3.org/2001/XMLSchema-instance".

To tie this XML document with the some Schema definition we use the attribute xsi:noNamespaceSchemaLocation. The value that can be passed to this attribute is the name of xsd file "StudentSchema.xsd"

After this we can define the child elements.

Step 3: See the output in the browser window as follows -

XML Program Output Screenshot

For the sake of comparisons I am giving three XML documents.

1. Simple XML Document (SimpleXml.xml)

<?xml version="1.0" encoding="utf-8"?>
<Student>
<name>Anand</name>
<address>Pune</address>
  <std>Second</std>
  <marks>70 percent</marks>
</Student>

2. XML Document with DTD

student.dtd
<!ELEMENT student (name, address, std, marks)>
<!ELEMENT name(#PCDATA)>
<!ELEMENT address(#PCDATA)>
<!ELEMENT std(#PCDATA)>
<!ELEMENT marks(#PCDATA)>

DTDDemo.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE student SYSTEM "student.dtd">
<student>
<name>Anand</name>
<address>Pune</address>
  <std>Second</std>
  <marks>70 percent</marks>
</student>

3. XML Schema

StudentSchema.xsd
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs = "http://wwww.w3.org/2001/XMLSchema">
  <xs:element name = "Student">
    <xs:complexType>
 <xs:sequence>
    <xs:element name = "name" type = "xs:string"/>
<xs:element name = "address" type = "xs:string"/>
<xs:element name = "std" type = "xs:string"/>
<xs:element name = "marks" type = "xs:string"/>
 </xs:sequence>
</xs:complexType>
  </xs:element>
</xs:schema>

MySchena.xml
<?xml version="1.0" encoding="utf-8"?>
<Student xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation = "StudentSchema.xsd">
  <name>Anand</name>
  <address>Pune</address>
  <std>Second</std>
  <marks>70 percent</marks>
</Student>

0 comments:

Post a Comment