+91 90691 39140 | +1 253 214 3115 | info@hub4tech.com | hub4tech

DTD Tutorial


A Document Type Definition (DTD) defines the structure and the legal elements and attributes of an XML document. A DTD can be declared inside an XML document or in an external file.

Document Type Definitions (DTDs) offer a way of specifying further rules that help in the interpretation of documents and their structure. By using Namespaces it’s possible to create unambiguously distinct sets of elements and attributes according the purpose you have in mind. Whether or not they appear in a namespace, such special-purpose sets of terms are sometimes known as vocabularies. You can describe an XML language’s structure and vocabulary in a software-friendly fashion, which can be used to check whether XML documents meet the requirements. This checking is known as validation.

What You Should Already Know?


Before you continue you should have a basic understanding of the following:
  • HTML
  • XML

Why Use a DTD?


With a DTD, independent groups of people can agree to use a standard DTD for interchanging data. Your application can use a standard DTD to verify that the data you receive from the outside world is valid. You can also use a DTD to verify your own data.

Internal DTD Declaration


If the DTD is declared inside the XML file, it must be wrapped inside the < ! DOCTYPE > definition:

	XML document with an internal DTD
		< ? xml version= " 1.0 " ? >
		< ! DOCTYPE example [
		< ! ELEMENT example ( to,from,heading,body ) >
		< ! ELEMENT to ( # PCDATA ) >
		< ! ELEMENT from ( # PCDATA ) >
		< ! ELEMENT heading ( # PCDATA ) >
		< ! ELEMENT body ( # PCDATA ) >
		] >
		< example >
		< to >User< / to >
		< from >Hub4Tech< / from >
		< heading >Introduction< / heading >
		< body >Please visit Hub4Tech atleast once.< / body >
		< / example >


In the XML file, select "view source" to view the DTD.

The DTD above is interpreted like this:
  • ! DOCTYPE example defines that the root element of this document is example
  • ! ELEMENT example defines that the example element must contain four elements: " to,from,heading,body "
  • ! ELEMENT to defines the to element to be of type " # PCDATA "
  • ! ELEMENT from defines the from element to be of type " # PCDATA "
  • ! ELEMENT heading defines the heading element to be of type " # PCDATA "
  • ! ELEMENT body defines the body element to be of type " # PCDATA "

External DTD Declaration


If the DTD is declared in an external file, the < ! DOCTYPE > definition must contain a reference to the DTD file:

		XML document with a reference to an external DTD
		< ? xml version=" 1.0 " ? >
		< ! DOCTYPE example SYSTEM " example.dtd " >
		< example >
		< to >User< / to >
		< from >Hub4Tech< / from >
		< heading >Introduction< / heading >
		< body >Please visit Hub4Tech atleast once.< / body >
		< / example >


And here is the file "example.dtd", which contains the DTD:

		< ! ELEMENT example ( to,from,heading,body ) >
		< ! ELEMENT to ( # PCDATA ) >
		< ! ELEMENT from ( # PCDATA ) >
		< ! ELEMENT heading ( # PCDATA ) >
		< ! ELEMENT body ( # PCDATA) >


Is it Helpful?
Copyright ©2015 Hub4Tech.com, All Rights Reserved. Hub4Tech™ is registered trademark of Hub4tech Portal Services Pvt. Ltd.
All trademarks and logos appearing on this website are the property of their respective owners.
FOLLOW US