Dtd Building Blocks in XML
DTD Building Blocks in XML
A DTD (Document Type Definition) defines the structure and the legal building blocks of an XML document. Understanding the fundamental components of a DTD is essential to define and validate XML documents effectively.
Key Building Blocks of a DTD
1. Element Declaration (<!ELEMENT>)
Defines an element and what content it can contain.
Specifies allowed child elements or text content.
Syntax:
<!ELEMENT element-name content-model>Examples:
<!ELEMENT book (title, author, year)><!ELEMENT title (#PCDATA)><!ELEMENT author (#PCDATA)><!ELEMENT year (#PCDATA)>2. Attribute List Declaration (<!ATTLIST>)
Defines attributes for an element.
Specifies attribute types and default values.
Syntax:
<!ATTLIST element-name attribute-name attribute-type default-declaration>Example:
<!ATTLIST book id ID #REQUIRED genre CDATA #IMPLIED status (available | checkedout | reserved) "available">3. Entity Declaration (<!ENTITY>)
Defines reusable pieces of text or special characters.
Can be internal (defined within the DTD) or external (loaded from a file).
Syntax:
<!ENTITY entity-name "replacement-text"><!ENTITY entity-name SYSTEM "URI">Example:
<!ENTITY copyright "© 2025 My Company"><!ENTITY logo SYSTEM "http://example.com/logo.png">4. Notation Declaration (<!NOTATION>)
Used to describe the format of non-XML data (like images or multimedia).
Less commonly used.
Syntax:
<!NOTATION notation-name SYSTEM "URI">5. Comments
Comments can be added using:
<!-- This is a comment -->Content Model Symbols
In element declarations, these symbols define structure:
| Symbol | Meaning |
|---|---|
, | Sequence (elements in order) |
| ` | ` |
? | Optional (0 or 1 occurrence) |
* | Zero or more occurrences |
+ | One or more occurrences |
() | Grouping elements |
Example DTD Combining Building Blocks
<!ELEMENT library (book+)><!ELEMENT book (title, author, year)><!ATTLIST book id ID #REQUIRED genre CDATA #IMPLIED status (available | checkedout | reserved) "available"><!ELEMENT title (#PCDATA)><!ELEMENT author (#PCDATA)><!ELEMENT year (#PCDATA)><!ENTITY company "My Publishing House">Summary Table
| DTD Building Block | Purpose | Syntax Example |
|---|---|---|
| Element Declaration | Define elements and their content | <!ELEMENT book (title, author)> |
| Attribute List Declaration | Define attributes for elements | <!ATTLIST book id ID #REQUIRED> |
| Entity Declaration | Define reusable text or references | <!ENTITY company "My Company"> |
| Notation Declaration | Define non-XML data format | <!NOTATION jpeg SYSTEM "image/jpeg"> |
| Comments | Add explanations or notes | <!-- This is a comment --> |
If you want, I can create a full XML and DTD example demonstrating all these blocks together!