How to create DTD for book details in a bookstore?

DTD(Document Type Declaration) is mainly used to describe xml.  The purpose of DTD is  to check vocabulary of the xml. It also checks the validity of the structure of XML documents.

It has the syntax given below.

Syntax:

<!DOCTYPE element DTD identifier

[

  Declaration 1

  Declaration 2

  ………………

  Declaration n

 ]>

 

It has two types listed as follows.

·       -> Internal DTD

·       ->External DTD

First, how the internal DTD is created and used in XML is shown below…

Internal DTD:

This type of DTD is defined inside the XML file. We can use the above syntax here. In the XML declaration, set the standalone attribute to “yes”.

The sample XML code for book store is given below.

 

<?xml version=”1.0” encoding=”UTF-8” standalone=”yes”?>

<! DOCTYPE book [

<!ELEMENT book(name,author,publisher)>

<!ELEMENT  name(#PCDATA)>

<!ELEMENT author(#PCDATA)>

<!ELEMNET publisher(#PCDATA)>

]>

 

 <book>

<name> learn programming </name>

<author> rajeeva </author>

<publisher>xyz publications </publisher>

</book>

This is a simple way to write internal DTD for a book in a book store.

Note:

 All the tags are case sensitive. opening and closing the tags should be in proper manner.


External DTD:

External DTD’s are referred from an external file. The  sample code is given below.

Here, we have to follow two things. One is set the standalone attribute in xml declaration to “no”. second is include the external DTD file.

<?xml version=”1.0” encoding=”UTF-8” standalone=”no”?>

<!DOCTYPE book SYSTEM “book.dtd”>

<book>

    <name> Learn programming </name>

    <author> Rajeeva </author>

    <publisher> abc publicaions </publisher>

</book>

 

The external DTD file “book.dtd” is as follows.

<!ELEMENT book(name,author,publisher>

<!ELEMENT name(#PCDATA)>

<!ELEMENT author(#PCDATA)>

<!ELEMENT  publisher(#PCDATA)>

 

Note:

Instead of using SYSTEM identifier ,we can use PUBLIC identifier  with the external URL .in the <!DOCTYPE …>

Eg: <!DOCTYPE book PUBLIC “URL”>

These are the different ways to create DTD’s in XML. In this xml, the DTD's are used to create a book details in a book store.

How to write your first XML program?

XML is Extensible Markup Language. It is used to create user defined tags. This is derived from SGML(Standard Generalized Markup Language).

Do you like to create your own tags?? If the answer is yes, you try XML.

A simple XML code is given below.

It is a simple birthday message for your beloved one.

<Birthday>

  <Wishes>  Happy birthday  </Wishes>

</Birthday>

  Here, the root tag is <Birthday>. <Wishes>  is the user defined tag. This basic xml program shows you to create a root tag with elements.

To create a basic program, follow the below syntax.

Syntax:

<?xml version=”1.0”?>

<root tag>

  <element1>   </element1>

                    .

                    .

  <elementn>   <elementn>

</root tag>

 Let us write your first XML program for a school student details.

Here, student is the root tag, name of the student,class, school,place are the details. Let us consider these four as our elements. 

<?xml version=”1.0”?>

<student>

<name> Ajay </name>

<class> VIII standard </name>

<School> SS public school </School>

<place>  Chennai </place>

</student>

Note:

·       Tags are case sensitive. so, use the tags in proper case. For example, opening a  <student> tag ,you shouldn’t use ending as like </Student>,</STUDENT>.

·       In the opening statement, <?xml version=”1.0”?>. xml keyword should be in lowercase.