Introduction to DTD
● Seperate file from XML document
● Can be embedded within the XML File
● It allows to validate the contents of Xml document
● It contains rules that apply to xml data
● Extention : .dtd
Why do we need DTD
● Replace the use of resusable pieces of text between 2 XML documents● To verify the XML document
● To meet the constraints
Document Type Declaration
● A DOCTYPE declaration is an XML document specifies a referance to DTD file.● <!DOCTYPE rootelement “a.dtd”>
● 2 types:
– Internal DTD
– External DTD
● System DTD
● Public DTD
Internal DTD
● The contents of the DTD are inside an XML document.● Eg:-
– <?xml version=”1.0”?>
<!DOCTYPE myBook
[ <!ELEMENT book_name (#PCDATA) > ]>
<myBook>
<book_name> XML </book_name>
</myBook>
● No need to provide a seperate name for the DTD(contents are inside XML file)
External DTD
● Provide a reference to the DTD inside XML document.● Allow as to define DTD once. Refer to any number of XML documents
● It reduce the size of the XML documents.
<?xml version=”1.0”?>
<!DOCTYPE myBook SYSTEM “mybook.dtd”>
<myBook>
<book_name> XML </book_name>
</myBook>
● <!ELEMENT book_name (#PCDATA)>
● System DTD: it has existance and relevance only in given text. Available in same computer or same network.
● Public DTD: has usage beyond a single XML document. It is located on a different computer
accessible via internet as a URL.

Element Type Declaration
● If we want to associate a DTD with an XML document, we need to declare all the elements that we would add in XML document. <!ELEMENT book_name (#PCDATA)> ● book_name: element name is generic identifier ● #PCDATA : data type content specificationElement Content Models
● Sequence, Occurrences, Choice● Empty, Any, Mixed Content
Sequence
● <!ELEMENT address(street, region, postal-code)<!ELEMENT street (#PCDATA)>
<!ELEMENT region (#PCDATA)>
<!ELEMENT postal-code (#PCDATA)>
● Specified using pipe(|) character
<!ELEMENT guest(name, beverage)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT beverage tea|cofee>
Occurences
● + : The element can occure one or more times
● * : The element can occure zero or more times
● ? : The element can occure zero or one times
+Example
● <!ELEMENT book(Chapter+)>
● <!ELEMENT book (Chapter, author)+)>
* Exanple
<!ELEMENT organisation (emp*)>
<organisation>
<emp> ramesh </emp>
<emp> suresh </emp>
</organisation>
<organisation></organisation>
? Example
<!ELEMENT organisation(emp?)>
<organisation>
<emp>ramesh</emp>
</organisation>
<organisation></organisation>
Empty, any and mixed content
● Element must be empty, can contain mixed content or can contain any content.● Empty elements
● <!ELEMENT name EMPTY>
<name></name> </name>
Mixed Content
● An element can contain either some text or other sub-elements.● <!ELEMENT mybooks(book | #PCDATA) *>
<mybooks>book1 name
<book>Computer network</book>
</mybooks>
Any
● An element declared with ANY type can contain any content, including PCDATA, sub-elements, combinations of the 2 or empty elements● <!ELEMENT mybook ANY>
● <mybook>XML related tech
<book></book>
<book>XML 1 </book>
Attribute Declaration
● ATTLIST keyword is used.● <!DOCTYPE email [
<!ELEMENT email(message)>
<!ELEMENT message (#PCDATA)>
<!ATTLIST message from CDATA #condition>]>
<email>
<message from=”Mr.x” to=”Mr. Y” > hello </message>
</email>
Limitations of DTD
● Non XML syntax● One DTD per XML
● Weak data typing
● No inheritance
● Overridding a DTD : internal can override external DTD
● No DOM support
0 Comments