</>
Skip to content
XML lessons (16/29)

XML — DTD

What is DTD?

DTD (Document Type Definition) defines the structure and legal elements of an XML document.

Internal DTD

<!DOCTYPE note [
    <!ELEMENT note (to, from, heading, body)>
    <!ELEMENT to (#PCDATA)>
    <!ELEMENT from (#PCDATA)>
    <!ELEMENT heading (#PCDATA)>
    <!ELEMENT body (#PCDATA)>
]>
<note>
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Don't forget me!</body>
</note>

External DTD

<!DOCTYPE note SYSTEM "note.dtd">

Element Declarations

SyntaxDescription
<!ELEMENT name (#PCDATA)>Text only
<!ELEMENT name (child)>Child element
<!ELEMENT name (a, b)>Sequence
<!ELEMENT name (a\|b)>Choice
<!ELEMENT name EMPTY>Empty
<!ELEMENT name ANY>Anything

Cardinality

SymbolMeaning
(a)Exactly one
(a+)One or more
(a*)Zero or more
(a?)Zero or one

Attribute Declarations

<!ATTLIST book
    genre CDATA #REQUIRED
    pages CDATA #IMPLIED
    lang CDATA "en"
>

Mini Practice

  1. Create an internal DTD
  2. Create an external DTD file
  3. Define elements with cardinality
  4. Add attribute declarations

Up Next

Continue with Schema — XML Schema Definition (XSD).

Related Topics

Frequently Asked Questions about DTD

What is DTD in XML?

DTD is a fundamental concept in XML. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn DTD?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn DTD.

Why is DTD important in XML?

DTD is essential for XML development. Understanding this concept will help you write better code and solve real-world problems more effectively.