Dtd Introduction in XML
DTD Introduction in XML
What is DTD?
DTD stands for Document Type Definition.
It is a set of rules that defines the structure and the legal elements and attributes of an XML document.
DTD ensures that the XML document is valid according to a defined format.
Purpose of DTD
To define the allowed elements and their hierarchy (which elements can appear inside which).
To specify the attributes associated with elements and their types.
To set rules on the order and number of child elements.
To help validate XML documents for correctness beyond just well-formedness.
Types of DTD
Internal DTD
Declared inside the XML document itself.<!DOCTYPE root-element [ <!-- DTD declarations go here -->]>External DTD
Stored in a separate file and referenced in the XML.<!DOCTYPE root-element SYSTEM "filename.dtd">
Basic Syntax of DTD
Element Declaration
<!ELEMENT element-name content-model>Attribute Declaration
<!ATTLIST element-name attribute-name attribute-type default-declaration>Entity Declaration
<!ENTITY entity-name "replacement-text">
Example of Internal DTD
<?xml version="1.0"?><!DOCTYPE note [ <!ELEMENT note (to, from, heading, body)> <!ELEMENT to (#PCDATA)> <!ELEMENT from (#PCDATA)> <!ELEMENT heading (#PCDATA)> <!ELEMENT body (#PCDATA)>]><note> <to>Alice</to> <from>Bob</from> <heading>Reminder</heading> <body>Meeting at 10 AM</body></note>Summary
| Feature | Description |
|---|---|
| Purpose | Define the valid structure of XML |
| Validation | Ensures XML meets the declared format |
| Declaration Types | Internal (inside XML) or External (separate file) |
| Components | Elements, Attributes, Entities |
If you'd like, I can help you write a DTD for your XML document or explain how to validate XML using DTD!