Open In App

DTD Syntax

Last Updated : 03 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  • Specifies which elements are allowed in the document
  • Order in which they should appear
  • Data types for attribute values

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>
  • element_name is the name of the element the attribute belongs to.
  • attribute_name is the name of the attribute.
  • attribute_type specifies the data type of the attribute.
  • default_value is the default value for the attribute.

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.

XML




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




<?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:

  • Validation: DTDs allow for the validation of XML documents, ensuring they adhere to a specific structure and set of rules.
  • Modularity: Parameter entity references enable modular DTD design, making it easier to maintain and reuse definitions.
  • Interoperability: DTDs help establish data exchange standards.
  • Documentation: DTDs serve as documentation, providing a clear understanding of the expected XML document structure.

Disadvantages of DTDs:

  • Limited Data Types: DTDs offer only basic data type support compared to more modern schema languages like XML Schema (XSD).
  • Complex Validation Rules: DTDs are less suitable for defining complex validation rules and constraints.
  • No Support for Namespace: DTDs do not support XML namespaces.

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads