Document Type Definition ( DTD ) 

Section 3: Document Type Definition (DTD)

In this section, we'll explore the world of Document Type Definition (DTD), a powerful tool for defining the structure of XML documents. We'll cover the basics of DTD, including its introduction, internal and external DTD declarations, and the usage of entities within DTD.

Introduction to DTD

Defining the Structure of an XML Document using DTD

Document Type Definition (DTD) is a markup language used to specify the structure and legal elements of an XML document. DTD defines:

Example of DTD Declaration:


<!DOCTYPE library SYSTEM "library.dtd">

<library>

    <book>

        <title>DTD Basics</title>

        <author>Emma Johnson</author>

    </book>

</library>

Internal and External DTD Declarations

Internal DTD Declaration:


<!DOCTYPE library [

    <!ELEMENT library (book+)>

    <!ELEMENT book (title, author)>

    <!ELEMENT title (#PCDATA)>

    <!ELEMENT author (#PCDATA)>

]>


External DTD Declaration (library.dtd):


<!ELEMENT library (book+)>

<!ELEMENT book (title, author)>

<!ELEMENT title (#PCDATA)>

<!ELEMENT author (#PCDATA)>

Entities in DTD

Defining and Using Entities in DTD

Entities in DTD provide a way to define reusable content. They can be internal or external.


Internal Entity:


<!DOCTYPE sample [

    <!ENTITY greeting "Hello, World!">

]>

<root>

    &greeting;

</root>

External Entity (sample.dtd):


<!ENTITY greeting "Hello, World!">

Using External Entity in XML:


<!DOCTYPE sample SYSTEM "sample.dtd">

<root>

    &greeting;

</root>

Understanding DTD is essential for creating robust and well-structured XML documents. Stay tuned for more lessons on advanced XML topics!