DTD - Overview
XML Document Type Declaration, commonly known as DTD, is a way to describe precisely the XML language. DTDs check the validity of structure and vocabulary of an XML document against the grammatical rules of the appropriate XML language.
An XML document can be defined as −
Well-formed − If the XML document adheres to all the general XML rules such as tags must be properly nested, opening and closing tags must be balanced, and empty tags must end with '/>', then it is called as well-formed.
Valid − An XML document said to be valid when it is not only well-formed, but it also conforms to available DTD that specifies which tags it uses, what attributes those tags can contain, and which tags can occur inside other tags, among other properties.
The following diagram represents that a DTD is used to structure the XML document −
DTD can be classified on its declaration basis in the XML document, such as −
When a DTD is declared within the file it is called Internal DTD and if it is declared in a separate file it is called External DTD.
We will learn more about these in the chapter DTD Syntax
Following are some important points that a DTD describes −
the elements that can appear in an XML document.
the order in which they can appear.
optional and mandatory elements.
element attributes and whether they are optional or mandatory.
whether attributes can have default values.
Advantages of using DTD
Documentation − You can define your own format for the XML files. Looking at this document a user/developer can understand the structure of the data.
Validation − It gives a way to check the validity of XML files by checking whether the elements appear in the right order, mandatory elements and attributes are in place, the elements and attributes have not been inserted in an incorrect way, and so on.
Disadvantages of using DTD
It does not support the namespaces. Namespace is a mechanism by which element and attribute names can be assigned to groups. However, in a DTD namespaces have to be defined within the DTD, which violates the purpose of using namespaces.
It supports only the text string data type.
It is not object oriented. Hence, the concept of inheritance cannot be applied on the DTDs.
Limited possibilities to express the cardinality for elements.