Document Type Definition (DTD), defined to some extent differently within the XML and SGMLspecifications, is one of several SGML and XML schema languages, and is the term used to describe a document or portion thereof that is authored in the DTD language. A DTD is primarily used for the expression of a schema via a set of declarations that conform to a particular markup syntax and that describe a class, or type, of SGML or XML documents, in terms of constraints on the structure of those documents. |
A Document Type Definition (DTD) defines the legal building blocks of an XML document. It defines the document structure with a list of legal elements and attributes. A DTD can be declared inline inside an XML document, or as an external reference. |
Types of DTD |
Internal DTD |
This is an XML document with a Document Type Definition: |
<?xml version=”1.0″ encoding=”ISO-8859-1″?> <!DOCTYPE email [ <!ELEMENT email (sender,recipient,subject,message)> <!ELEMENT sender (sender_name,sender_email_id)> <!ELEMENT recipient (recipient_name,recipient_email_id)> <!ELEMENT subject (#PCDATA)> <!ELEMENT message (message_header,message_body_text,message_footer)> ]> |
<email> <sender> <sender_name>XYZ Kumar</sender_name> <sender_email_id>[email protected]</sender_email_id> </sender> <recipient> <recipient_name>ABC</recipient_name> <recipient_email_id>[email protected]</recipient_email_id> </recipient> <subject>Hello! how r U</subject> <message> <message_header>Hello dear</message_header> <message_body_text>Hello this is xyz, how are you dear</message_body_text> <message_footer>bye!, take care</message_footer> </message> </email> |
Click here to view the XML file. |
The DTD is interpreted like this: |
!ELEMENT email (in line 2) defines the element “email” as having four elements: ” sender, recipient, subject, message” and so on. |
External DTD |
This is the same XML document with an external DTD: |
<?xml version=”1.0″ encoding=”ISO-8859-1″?> <!DOCTYPE email SYSTEM “emailStructure.dtd”> <email> <sender> <sender_name>XYZ Kumar</sender_name> <sender_email_id>[email protected]</sender_email_id> </sender> <recipient> <recipient_name>ABC</recipient_name> <recipient_email_id>[email protected]</recipient_email_id> </recipient> <subject>Hello! how r U</subject> <message> <message_header>Hello dear</message_header> <message_body_text>Hello this is xyz, how are you dear</message_body_text> <message_footer>bye!, take care</message_footer> </message> </email> |
Click here to view the XML file. |
Content of the file “emailStructure.dtd” containing the Document Type Definition: |
<?xml version=”1.0″ encoding=”ISO-8859-1″?> <!ELEMENT email (sender,recipient,subject,message)> <!ELEMENT sender (sender_name,sender_email_id)> <!ELEMENT recipient (recipient_name,recipient_email_id)> <!ELEMENT subject (#PCDATA)> <!ELEMENT message (message_header,message_body_text,message_footer)> |