DTD Examples
Real-world DTD implementations and use cases
📚 DTD Examples
Learn DTD through practical examples covering common scenarios like books, contacts, orders, and more. These real-world examples demonstrate best practices for structuring and validating XML documents effectively.
<!-- Simple DTD structure -->
<!DOCTYPE root [
<!ELEMENT root (child+)>
<!ELEMENT child (#PCDATA)>
]>
Example Categories
Book Catalog
Library and bookstore systems
Contact List
Address book and contacts
Order System
E-commerce and shopping
News Article
Content management systems
🔹 Example 1: Book Catalog
A complete DTD for a library or bookstore catalog system with books, authors, and pricing information including validation rules.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE catalog [
<!-- Elements -->
<!ELEMENT catalog (book+)>
<!ELEMENT book (title,author+,publisher,year,price,description?)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (firstname,lastname)>
<!ELEMENT firstname (#PCDATA)>
<!ELEMENT lastname (#PCDATA)>
<!ELEMENT publisher (#PCDATA)>
<!ELEMENT year (#PCDATA)>
<!ELEMENT price (#PCDATA)>
<!ELEMENT description (#PCDATA)>
<!-- Attributes -->
<!ATTLIST book
id ID #REQUIRED
isbn CDATA #REQUIRED
category (fiction|nonfiction|science|history|technology) #REQUIRED
language CDATA "English">
<!ATTLIST price currency CDATA "USD">
<!-- Entities -->
<!ENTITY publisher "TechBooks Publishing">
]>
<catalog>
<book id="B001" isbn="978-1234567890" category="technology">
<title>Learning XML and DTD</title>
<author>
<firstname>John</firstname>
<lastname>Smith</lastname>
</author>
<author>
<firstname>Jane</firstname>
<lastname>Doe</lastname>
</author>
<publisher>&publisher;</publisher>
<year>2024</year>
<price currency="USD">49.99</price>
<description>
A comprehensive guide to XML and DTD for beginners.
</description>
</book>
<book id="B002" isbn="978-0987654321" category="science">
<title>Introduction to Physics</title>
<author>
<firstname>Albert</firstname>
<lastname>Newton</lastname>
</author>
<publisher>Science Press</publisher>
<year>2023</year>
<price currency="EUR">39.99</price>
</book>
</catalog>
🔹 Example 2: Contact List
An address book DTD with personal and professional contact information, supporting multiple phone numbers and addresses per contact.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE contacts [
<!-- Elements -->
<!ELEMENT contacts (contact+)>
<!ELEMENT contact (name,email+,phone*,address?)>
<!ELEMENT name (firstname,lastname,middlename?)>
<!ELEMENT firstname (#PCDATA)>
<!ELEMENT lastname (#PCDATA)>
<!ELEMENT middlename (#PCDATA)>
<!ELEMENT email (#PCDATA)>
<!ELEMENT phone (#PCDATA)>
<!ELEMENT address (street,city,state,zip,country)>
<!ELEMENT street (#PCDATA)>
<!ELEMENT city (#PCDATA)>
<!ELEMENT state (#PCDATA)>
<!ELEMENT zip (#PCDATA)>
<!ELEMENT country (#PCDATA)>
<!-- Attributes -->
<!ATTLIST contact
id ID #REQUIRED
type (personal|business) #REQUIRED
favorite (yes|no) "no">
<!ATTLIST phone type (mobile|home|work) #REQUIRED>
<!ATTLIST email type (personal|work) "personal">
]>
<contacts>
<contact id="C001" type="business" favorite="yes">
<name>
<firstname>John</firstname>
<middlename>Robert</middlename>
<lastname>Smith</lastname>
</name>
<email type="work">[email protected]</email>
<email type="personal">[email protected]</email>
<phone type="mobile">+1-555-0123</phone>
<phone type="work">+1-555-0124</phone>
<address>
<street>123 Business Ave</street>
<city>New York</city>
<state>NY</state>
<zip>10001</zip>
<country>USA</country>
</address>
</contact>
<contact id="C002" type="personal">
<name>
<firstname>Jane</firstname>
<lastname>Doe</lastname>
</name>
<email>[email protected]</email>
<phone type="mobile">+1-555-0125</phone>
</contact>
</contacts>
🔹 Example 3: E-commerce Order
A complete order system DTD for e-commerce applications with customer information, products, shipping, and payment details.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE order [
<!-- Elements -->
<!ELEMENT order (customer,items,shipping,payment,total)>
<!ELEMENT customer (name,email,phone)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT email (#PCDATA)>
<!ELEMENT phone (#PCDATA)>
<!ELEMENT items (item+)>
<!ELEMENT item (product,quantity,price)>
<!ELEMENT product (#PCDATA)>
<!ELEMENT quantity (#PCDATA)>
<!ELEMENT price (#PCDATA)>
<!ELEMENT shipping (address,method)>
<!ELEMENT address (#PCDATA)>
<!ELEMENT method (#PCDATA)>
<!ELEMENT payment EMPTY>
<!ELEMENT total (#PCDATA)>
<!-- Attributes -->
<!ATTLIST order
id ID #REQUIRED
date CDATA #REQUIRED
status (pending|processing|shipped|delivered|cancelled) "pending">
<!ATTLIST item
id CDATA #REQUIRED
category (electronics|books|clothing|food) #REQUIRED>
<!ATTLIST payment
method (credit|debit|paypal|cash) #REQUIRED
status (pending|completed|failed) "pending">
<!-- Entities -->
<!ENTITY currency "USD">
<!ENTITY tax "8.5%">
]>
<order id="ORD001" date="2024-01-15" status="processing">
<customer>
<name>Alice Johnson</name>
<email>[email protected]</email>
<phone>+1-555-0126</phone>
</customer>
<items>
<item id="I001" category="electronics">
<product>Laptop</product>
<quantity>1</quantity>
<price>999.00</price>
</item>
<item id="I002" category="electronics">
<product>Mouse</product>
<quantity>2</quantity>
<price>25.00</price>
</item>
</items>
<shipping>
<address>456 Home Street, Boston, MA 02101</address>
<method>Express Shipping</method>
</shipping>
<payment method="credit" status="completed"/>
<total>1049.00 ¤cy; (includes &tax; tax)</total>
</order>
🔹 Example 4: News Article
A content management system DTD for news articles with metadata, authors, categories, and rich text content including multimedia.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE article [
<!-- Elements -->
<!ELEMENT article (metadata,content,media?,tags?)>
<!ELEMENT metadata (title,author,published,modified?,category)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (name,email)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT email (#PCDATA)>
<!ELEMENT published (#PCDATA)>
<!ELEMENT modified (#PCDATA)>
<!ELEMENT category (#PCDATA)>
<!ELEMENT content (paragraph+)>
<!ELEMENT paragraph (#PCDATA|bold|italic|link)*>
<!ELEMENT bold (#PCDATA)>
<!ELEMENT italic (#PCDATA)>
<!ELEMENT link (#PCDATA)>
<!ELEMENT media (image|video)*>
<!ELEMENT image EMPTY>
<!ELEMENT video EMPTY>
<!ELEMENT tags (tag+)>
<!ELEMENT tag (#PCDATA)>
<!-- Attributes -->
<!ATTLIST article
id ID #REQUIRED
lang CDATA "en"
status (draft|published|archived) "draft">
<!ATTLIST image
src CDATA #REQUIRED
alt CDATA #REQUIRED
width CDATA #IMPLIED
height CDATA #IMPLIED>
<!ATTLIST video
src CDATA #REQUIRED
duration CDATA #IMPLIED>
<!ATTLIST link href CDATA #REQUIRED>
<!-- Entities -->
<!ENTITY siteName "Tech News Daily">
<!ENTITY copyright "Copyright © 2024">
]>
<article id="ART001" lang="en" status="published">
<metadata>
<title>Introduction to XML DTD</title>
<author>
<name>Sarah Williams</name>
<email>[email protected]</email>
</author>
<published>2024-01-15T10:30:00</published>
<modified>2024-01-16T14:20:00</modified>
<category>Technology</category>
</metadata>
<content>
<paragraph>
Welcome to &siteName;! Today we explore
<bold>XML DTD</bold> and its <italic>practical applications</italic>.
</paragraph>
<paragraph>
DTD helps validate XML documents. Learn more at
<link href="https://example.com">our tutorial</link>.
</paragraph>
<paragraph>
This technology is <bold>essential</bold> for modern
web development and data exchange systems.
</paragraph>
</content>
<media>
<image
src="dtd-diagram.jpg"
alt="DTD Structure Diagram"
width="800"
height="600"/>
<video
src="dtd-tutorial.mp4"
duration="05:30"/>
</media>
<tags>
<tag>XML</tag>
<tag>DTD</tag>
<tag>Tutorial</tag>
<tag>Web Development</tag>
</tags>
</article>
🔹 Example 5: Restaurant Menu
A simple restaurant menu DTD demonstrating categories, items, prices, and dietary information for food service applications.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE menu [
<!ELEMENT menu (restaurant,categories)>
<!ELEMENT restaurant (name,address,phone)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT address (#PCDATA)>
<!ELEMENT phone (#PCDATA)>
<!ELEMENT categories (category+)>
<!ELEMENT category (item+)>
<!ELEMENT item (name,description,price,calories?)>
<!ELEMENT description (#PCDATA)>
<!ELEMENT price (#PCDATA)>
<!ELEMENT calories (#PCDATA)>
<!ATTLIST category name CDATA #REQUIRED>
<!ATTLIST item
id ID #REQUIRED
vegetarian (yes|no) "no"
spicy (mild|medium|hot) #IMPLIED>
<!ENTITY currency "$">
]>
<menu>
<restaurant>
<name>The Gourmet Kitchen</name>
<address>789 Food Street, Chicago, IL</address>
<phone>+1-555-0127</phone>
</restaurant>
<categories>
<category name="Appetizers">
<item id="A001" vegetarian="yes">
<name>Spring Rolls</name>
<description>Fresh vegetables wrapped in rice paper</description>
<price>¤cy;8.99</price>
<calories>180</calories>
</item>
<item id="A002" spicy="hot">
<name>Buffalo Wings</name>
<description>Spicy chicken wings with blue cheese</description>
<price>¤cy;12.99</price>
<calories>450</calories>
</item>
</category>
<category name="Main Course">
<item id="M001" vegetarian="yes">
<name>Vegetable Pasta</name>
<description>Penne with seasonal vegetables</description>
<price>¤cy;15.99</price>
<calories>520</calories>
</item>
<item id="M002">
<name>Grilled Salmon</name>
<description>Fresh Atlantic salmon with herbs</description>
<price>¤cy;24.99</price>
<calories>380</calories>
</item>
</category>
</categories>
</menu>
🔹 Key Takeaways
DTD Best Practices from Examples:
- Clear Structure: Organize elements logically and hierarchically
- Appropriate Attributes: Use attributes for IDs, types, and metadata
- Entities for Reuse: Define common text and symbols as entities
- Occurrence Indicators: Use +, *, ? to control element repetition
- Enumerated Values: Restrict attributes to valid options
- Optional Elements: Use ? for optional content
- Mixed Content: Combine text and elements when needed
- Documentation: Comment your DTD for clarity