Dtd Examples in XML
DTD Examples in XML
Here are some practical examples demonstrating how to define and use DTD to describe the structure of XML documents.
1. Simple XML with Basic DTD
XML
<?xml version="1.0"?><!DOCTYPE note SYSTEM "note.dtd"><note> <to>Alice</to> <from>Bob</from> <heading>Reminder</heading> <body>Don't forget the meeting tomorrow!</body></note>DTD (note.dtd)
<!ELEMENT note (to, from, heading, body)><!ELEMENT to (#PCDATA)><!ELEMENT from (#PCDATA)><!ELEMENT heading (#PCDATA)><!ELEMENT body (#PCDATA)>2. DTD with Attributes
XML
<?xml version="1.0"?><!DOCTYPE book SYSTEM "book.dtd"><book id="bk101" genre="fiction"> <title>XML Guide</title> <author>John Doe</author> <year>2025</year></book>DTD (book.dtd)
<!ELEMENT book (title, author, year)><!ATTLIST book id ID #REQUIRED genre CDATA #IMPLIED><!ELEMENT title (#PCDATA)><!ELEMENT author (#PCDATA)><!ELEMENT year (#PCDATA)>3. DTD with Choice and Occurrence
XML
<?xml version="1.0"?><!DOCTYPE status SYSTEM "status.dtd"><status>pending</status>DTD (status.dtd)
<!ELEMENT status (new | pending | done)>The
<status>element can only be one of:new,pending, ordone.
4. DTD with Mixed Content
XML
<?xml version="1.0"?><!DOCTYPE paragraph SYSTEM "paragraph.dtd"><paragraph>This is a <bold>bold</bold> word and this is <italic>italic</italic>.</paragraph>DTD (paragraph.dtd)
<!ELEMENT paragraph (#PCDATA | bold | italic)*><!ELEMENT bold (#PCDATA)><!ELEMENT italic (#PCDATA)>5. DTD with Entities
XML
<?xml version="1.0"?><!DOCTYPE article SYSTEM "article.dtd"><article> <title>About &company;</title> <author>Jane Smith</author></article>DTD (article.dtd)
<!ENTITY company "OpenAI"><!ELEMENT article (title, author)><!ELEMENT title (#PCDATA)><!ELEMENT author (#PCDATA)>Summary Table of Examples
| Example | Key Feature | Description |
|---|---|---|
| Example 1 | Basic elements | Simple element declaration |
| Example 2 | Attributes | Element with required and optional attributes |
| Example 3 | Choice | Element allows one from many options |
| Example 4 | Mixed content | Text combined with child elements |
| Example 5 | Entities | Reusable text snippets defined in DTD |
If you want, I can create a customized DTD/XML example based on your specific data or use case!