Introduction to XML | Defining XML Structures with DTD | What Is a DTD?

XML lets you define tags freely, but defining a shared document structure is useful when multiple systems exchange data. DTD is a language for defining that structure.

For example, consider inventory data collected from multiple branches. If each branch uses its own tags, the system that processes the documents must handle every variation. A common DTD makes each branch follow the same rules.

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE inventory[
    <!ELEMENT inventory (product)>
    <!ELEMENT product (name, quantity)>
    <!ELEMENT name (#PCDATA)>
    <!ELEMENT quantity (#PCDATA)>
]>

<inventory>
    <product>
        <name>Bicycle</name>
        <quantity>20</quantity>
    </product>
</inventory>

Because the document follows common rules, the meaning of each value is clear and programs can process the known tags more easily.

A DTD defines the structure of an XML document. You can write it directly in the XML document or store it in an external file and load it.