BCA TU Web Tech - How to write XML Schema and DTD ?
Lets suppose we have a XML file containing the details of students. We know students data can have many attributes such as name, address, guardian, contact, course, roll no etc but for the sake of simplicity we will consider only four attributes of students' data. The main aim of this article is to learn the schema writing process of XML files.
Consider the following XML file which contains the name, roll, address, gender attributes of students.
<students>
<student>
<name>Shrawan</name>
<roll>21</roll>
<address>Pespiscola</address>
<gender>Male</gender>
</student>
<student>
<name>Abdhesh</name>
<roll>02</roll>
<address>Jadibuti</address>
<gender>Male</gender>
</student>
<student>
<name>Anil</name>
<roll>03</roll>
<address>Jadibuti</address>
<gender>Male</gender>
</student>
<student>
<name>Rupesh</name>
<roll>16</roll>
<address>Jadibuti</address>
<gender>Male</gender>
</student>
<student>
<name>Esha</name>
<roll>06</roll>
<address>Balkot</address>
<gender>Female</gender>
</student>
<student>
<name>Samjhana</name>
<roll>17</roll>
<address>Kalanki</address>
<gender>Female</gender>
</student>
<student>
<name>Insang</name>
<roll>12</roll>
<address>Koteshwor</address>
<gender>Male</gender>
</student>
</students>
As you can see we have only few number of student's data. Now our task is to add schema validation to this xml file. To add the schema the following line is added at the beginning:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
Now since we have one opening head element called "students" , we put the following code below the element students:
<xs:complexType>
<xs:sequence>
For each of the head tag we must declare complex type and sequence. After writing complex type for student under students we can start declaring name and type of the elememts as below:
<xs:element name="name" type="xs:string"/>
<xs:element name="roll" type="xs:int"/>
<xs:element name="address" type="xs:string"/>
<xs:element name="gender" type="xs:string"/>
Here if you see you will find that each attributes of xml should contain two things, name and type. Before puttong name and type we must write"xs:element" So the final code of schema valdation of the XML for the students data shown above will be like this:
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="students">
<xs:complexType>
<xs:sequence>
<xs:element name="student">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="roll" type="xs:int"/>
<xs:element name="address" type="xs:string"/>
<xs:element name="gender" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
And finally the file is saved with the extension of .xsd meaning xml schema document. Thus our file will be saved as students.xsd
Now for the same xml file if we need to create data DTD file then what can be done ? In case you dont know whta DTD stands for Document Type Definition. DTD is a document file defines the element types and attributes in an XML file. See the DTD of the XML Students
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE STUDENTS [
<!ELEMENT students (students+)>
<!ELEMENT student (name, roll, address, gender) >
<!ELEMENT name (#PCDATA) >
<!ELEMENT roll (#PCDATA) >
<!ELEMENT address (#PCDATA) >
<!ELEMENT gender (#PCDATA) >
]>
Finally, the Schema and DTD of the XML is shown together in the code belwo:
<?xml version="1.0" encoding="UTF-8"?>
<!-- DTD for the XML-->
<!DOCTYPE STUDENTS [
<!ELEMENT students (students+)> //Corrected by teacher
<!ELEMENT student (name, roll, address, gender) >
<!ELEMENT name (#PCDATA) >
<!ELEMENT roll (#PCDATA) >
<!ELEMENT address (#PCDATA) >
<!ELEMENT gender (#PCDATA) >
]>
<!--Schema for the XML-->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="students">
<xs:complexType>
<xs:sequence>
<xs:element name="student">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="roll" type="xs:int"/>
<xs:element name="address" type="xs:string"/>
<xs:element name="gender" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
<!--XML Document-->
<students>
<student>
<name>Shrawan</name>
<roll>21</roll>
<address>Pespiscola</address>
<gender>Male</gender>
</student>
<student>
<name>Abdhesh</name>
<roll>02</roll>
<address>Jadibuti</address>
<gender>Male</gender>
</student>
<student>
<name>Anil</name>
<roll>03</roll>
<address>Jadibuti</address>
<gender>Male</gender>
</student>
<student>
<name>Rupesh</name>
<roll>16</roll>
<address>Jadibuti</address>
<gender>Male</gender>
</student>
<student>
<name>Esha</name>
<roll>06</roll>
<address>Balkot</address>
<gender>Female</gender>
</student>
<student>
<name>Samjhana</name>
<roll>17</roll>
<address>Kalanki</address>
<gender>Female</gender>
</student>
<student>
<name>Insang</name>
<roll>12</roll>
<address>Koteshwor</address>
<gender>Male</gender>
</student>
</students>
After getting few good response from this site, we decided to extend our service and thus developed an app called BCA Guides Nepal. The app has been published on play store. This app provides features like - Downloading Notes
- Downloading Syllabus
- Downloading Question papers
- Downloading Books
- Downloading Notices
- Checking Results within the app
App Download Link : https://play.google.com/store/apps/details?id=com.skthakur.bcaguidesnepal
0 Comments