Python DTD (Document Type Definition) & Entities
A DTD (Document Type Definition) defines the legal building blocks of an XML document. It specifies the list of permitted elements, child ordering, attribute types, and custom text entities. An XML document validated against its DTD is termed Valid XML.
"A DTD is like a building permit inspector checklist: it lists exactly what rooms (elements), doors (attributes), and standard fixtures (entities) your house is legally allowed to possess."
Deep Dive: How It Works
<!DOCTYPE root [...]>: Encloses internal DTD rules or points to an external .dtd file.
<!ELEMENT name (child1, child2)>: Defines element hierarchy. (#PCDATA stands for Parsed Character Data).
<!ATTLIST element attr type default>: Declares attribute types (#REQUIRED, #IMPLIED, #FIXED).
<!ENTITY name "value">: Declares custom shortcut aliases expanded by XML parsers.
Syntax Blueprint
<!DOCTYPE note [ <!ELEMENT note (to,from,body)> <!ELEMENT to (#PCDATA)> ]>
Specifies that <note> must contain exactly one <to>, <from>, and <body> in sequence.
Core Rules to Remember



Common Beginner Traps & How to Fix Them
Putting elements in the wrong order when DTD defines a strict sequence.Why it happens: <!ELEMENT note (to,from)> fails validation if <from> appears before <to>.
How to fix: Ensure elements match the exact sequence declared in the DTD definition.
Live Interactive Example
Hit Run Code to see it liveYour Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Write a DTD element declaration
Declare <!ELEMENT user (name, email)> for a user containing parsed character data name and email.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.