In this tutorial you will learn how to create a document type description (.DTD) file of an XML document.
In this tutorial you will learn how to create a document type description (.DTD) file of an XML document.A document type definition (DTD) is used to validate XML files that reference the DTD file against a set of rules.
DTD contains declarations that define elements, attributes etc for any XML file and defines constraints for how each element, attribute etc may be used within of any XML files that reference the DTD file.
DTDs are simple text files and you can use any basic text editor to create it. Although it looks a little difficult to understand at first but they are not so complicated once you get used to them.
articles.dtd is the sample file. See the text below:
articles.dtd
<!ELEMENT articles (article)+>
<!ELEMENT article (title,url,img)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT url (#PCDATA)>
<!ELEMENT img (#PCDATA)>
<!ATTLIST articles type CDATA #REQUIRED>
1. The Document Element
Declare first Element as given below:
<!ELEMENT articles (article)+>
There are many qualifiers which can be used for different purposes as given below:
Qualifier | Meaning |
* | Zero or more |
+ | One or more |
? | Optional (zero or one) |
2. Other Elements
As we declared document element, we can also declare other elements using <!ELEMENT> tag.
<!ELEMENT article (title,url,img)>
In above statement, each article element is declared to contain three child elements title, url and img, which must appear once and only once and in the same order.
Some elements are declared as #PCDATA (parsed character data) which indicates that it should contain only text.
For example: The title, url and img elements contain only text.
<!ELEMENT title (#PCDATA)>
<!ELEMENT url (#PCDATA)>
<!ELEMENT img (#PCDATA)>
3. Choice of Elements<!ELEMENT E (E1 | E2)>
Element E consists of either element E1 or element E2.<!ELEMENT elm EMPTY>
5. Mixed Content<!ELEMENT E (#PCDATA | E1 | E2)*>
For example, above statement declares element E to have text and any number of E1 and E2 elements.
6. Location of Modifier
Its important to place the modifier in the right location. Modifier outside of parenthesis applies to the group while modifier adjacent to the element name applies to the element only.
For example:
<!ELEMENT E (E1 , E2)*>
In above declaration, the element E can have any number of child E1 and E2 elements. But they must be in pairs and E1 element must come before E2 element.
<!ELEMENT E (E1* , E2*)>
In above declaration, the element E can have any number of child E1 elements followed by any number of child E2 elements.
<!ELEMENT E (E1 | E2)*>
In above declaration, element E can have any number of child E1 and E2 elements.
<!ELEMENT E (E1* | E2*)>
In above declaration, the element E can have any number of child E1 elements or any number of child E2 elements. But it cannot have both E1 and E2 elements.
7. Using Parentheses for Complex Declarations
For complex declarations, we can use parenthesis to group elements.
<!ELEMENT E (E1 | (E2, E3))>
In above declaration, element E can either contain a single E1 element or a pair of E2 and E3 elements.
8. Declaring Attributes
Attributes are declared using the <!ATTLIST > declaration.
The syntax is as given below:
<!ATTLIST NameOfElement NameOfAttribute TypeOfAttribute State DefaultValue?>
AttributeType declares type of data contained by attribute value like CDATA and ID.
State can be replaced by any one of the values: #REQUIRED, #FIXED (set value), and #IMPLIED (optional).
DefaultValue defines the default value of the attribute if not provided.
<!ATTLIST articles type CDATA #REQUIRED>
In above declaration, articles element is declared to have type attribute which is required and contains text.