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)>
]>
<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.