Open In App

DTD Syntax

DTD stands for Document Type Definition. It is a set of rules which is used to define the structure and elements of an XML document. A DTD:

DTDs are used to validate and describe the structure of XML documents. In a way, they are a set of rules that confirm that the XML document follows a specified structure.



Syntax:

1. Element Declaration:

<!ELEMENT> declaration is used to define an element in a DTD.

<!ELEMENT element_name content_specification>

where: element_name is the name of the XML and content_specification defines what can appear within the element> It can be EMPTY, ANY, or can contain any content.



2. Entity Declaration:

Entities are used to define reusable strings or special characters. <!ENTITY> declaration is used to define entities in a DTD. Entities can be of two types:

a. Internal entities:

<!ENTITY entity_name "entity_value">

b. External entities:

<!ENTITY entity_name SYSTEM "file_location">

3. Attribute Declaration:

<!ATTLIST> declaration is used to define attributes for an element.

<!ATTLIST element_name
  attribute_name attribute_type default_value>

4. Comments:

IN DTDs, comments are added in the same way just like XML documents. ‘<!—-‘ and ‘—->’ are used to add comments.

<!-- This is a DTD comment -->

5. Parameter Entity Reference:

Parameter entities are used to define reusable parts of the DTD. They are denoted by %.

<![ENTITY % entity_name "entity_value">

6. Notation Declaration:

It provides a way to specify the format or encoding of external data that an XML document might reference, such as multimedia files, binary data, or other data types that are not directly represented in XML.

<!NOTATION notation_name SYSTEM "system_identifier">

Now, let’s take an example of a DTD document which defines details of the book- its title, publisher, author, publishing year and ISBN.




document.dtd File
< !DOCTYPE library [
 <! ELEMENT library (book+)>
 <! ELEMENT book (title, author, pub_year, ISBN)>
 <! ELEMENT title (#PCDATA)
 <! ELEMENT author (#PCDATA)
 <! ELEMENT publication_year (#PCDATA)>
 <! ELEMENT ISBN (#PCDATA)>
  
 <!ATTLIST book genre CDATA #IMPLIED>
 <!ENTITY company_name "XYZ Publisher">
 <!NOTATION image_png PUBLIC "PNG Image">
]>

The above code comprises details of the book. We have to link the above dtd document to an .xml file to run an executable code as shown below:




<?xml version="l.0" encoding="UTF-8"?>
< !DOCTYPE library SYSTEM "document.dtd">
<library>
    <book genre="Science Fiction">
        <tit1e>The Hitchhiker's Guide to the Galaxy</title>
            <author>Doug1as Adams</author>
            <pub1ication_year>1979</pub1ication_year>
            <ISBN>0345391802</ISBN>
    </book>
    <book genre="Mystery">
        <tit1e>Sher10ck Holmes: The Hound of the Baskervilles< /title>
                <author>Arthur Conan Doyle</author>
                <pub1ication_year>1902</pub1ication_year>
                <ISBN>9780140437867</ISBN>
    </book>
</library>

Advantages of DTDs:

Disadvantages of DTDs:

Document Type Definitions (DTDs) are a fundamental component of XML, providing a means to define and validate the structure of XML documents.


Article Tags :