Share on Google+Share on Google+

Creating a DTD

In this tutorial you will learn how to create a document type description (.DTD) file of an XML document.

Creating a DTD

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:


<!ELEMENT articles (article)+>
<!ELEMENT article (title,url,img)>
<!ELEMENT title (#PCDATA)>
<!ATTLIST articles type CDATA #REQUIRED>

1. The Document Element

Declare first Element as given below:

<!ELEMENT articles (article)+>

  • The DTD tag starts with <! followed by ELEMENT tag.
  • Name of the element "articles" that is being defined comes after the ELEMENT tag.
  • In parentheses, one or more items that defines the valid contents for that element. Above line indicates that a "articles" tag can contain one or more "article" elements.

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)>



3. Choice of Elements

Elements can also be declared to have any one of the element from the list of elements.
For example:

<!ELEMENT E (E1 | E2)>

Element E consists of either element E1 or element E2.

4. Empty Elements

Empty element does not contain any sub elements or text. It is declared empty by specifying the element as an EMPTY element.


5.  Mixed Content
Elements can be declared to have elements and text together.
For example:

<!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?>

  1. AttributeType declares type of data contained by attribute value like CDATA and ID.

  2. State can be replaced by any one of the values: #REQUIRED, #FIXED (set value), and #IMPLIED (optional).

  3. 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.


Posted on: November 11, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.


Advertisement null