Go to previous pageGo to next page

1.4.1. XML DTD

A Document Type Definition defines the legal building blocks of an XML document with a list of legal elements.
A DTD includes the following XML-structure and -syntax elements:

  • list of valid elements,
  • list of valid attributes,
  • list of entities,
  • definition on how elements can be nested,
  • definition whether an element or attribute is required, recommended or optional,
  • definition on how often elements may be used (0, 1 or more),
  • definition of default values.
remark

A DTD can be declared inline in your XML document, or as an external reference.

Internal DOCTYPE declaration

If the DTD is included in your XML source file, it should be wrapped in a DOCTYPE definition with the following syntax:

Internal DOCTYPE declarationInternal DOCTYPE declaration

The DTD above is interpreted like this:

  • !DOCTYPE person (in line 2) defines that this is a document of the type person.
  • !ELEMENT person (in line 3) defines the person element is having three elements: "age, gender, name".
  • !ELEMENT age (in line 4) defines the age element to be of type "term#CDATA".
  • !ELEMENT gender (in line 5) defines the gender element to be of type "term#PCDATA".
  • And so on...

External DOCTYPE declaration

If the DTD is external to your XML source file, it should be wrapped in a DOCTYPE definition with the following syntax:

External DTD DeclarationExternal DTD Declaration

The same XML document as above, but with an external DTD looks like this:

XML      file with external DTDXML file with external DTD

And a copy of the file "person.dtd" containing the DTD looks like this:

Content      of the external DTD file "person.dtd"Content of the external DTD file "person.dtd"

Perhaps you are asking why do we have to use a DTD? Here are the answers:

  • With DTD, each of your XML files can carry a description of its own format with it.
  • With a DTD, independent groups of people can agree to use a common 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.

If you are interested in more information about DTD (e.g. how to define attributes in a DTD document) consult the W3C Tutorial "Learn DTD".
In this chapter we dealt with the "DTD Introduction"-part of this tutorial. Therefore you might continue with the chapter "DTD Building Blocks".



Go to previous page
Go to next page