Declaring Document Declarations in XML

By Daniel Imbellino
Feb 28, 2013

A document type definition is used in XML to declare elements, rules, and properties for your markup language. Schemas do the same job and better, but here we are going to look at DTD’s specifically.

If You’ve worked with XHTML or HTML you’ve most likely used a document type definition in your web pages. In an XML document a document type definition is called a document type declaration, since we are declaring the use of one in our document.

The document type declaration in XHTML 1.0 Strict:

<!DOCTYPE html PUBLIC “-W3C//DTD XHTML 1.0 Transitional//EN” “”>

We can declare an internal DTD or point to an external one from our XML document like so:
To declare an internal DTD:
<?xml version=”1.0”?>
<!DOCTYPE root_element [
Define elements, rules, and properties here

Here we specified the use of a DTD followed by the root element of our XML language, which could be any element we defined. Notice the closing “]>”, all of our rules for our language will go between the opening DOCTYPE and its closing characters.
You can point to an external DTD the same way you do with XHTML like this:
<!DOCTYPE root_element Public “-//Name_Of_organization_or_creator//DTD Root_Element//EN//” “http://location-of-DTD/subfolder/root_element.dtd”>

Hers an example using a fictitious XBOX markup language:

<!DOCTYPE xbox Public “-//Microsoft-xbml//DTD xbox//EN//” “”>

We started with an XML declaration using the “standalone=”no” attribute, that specifies this XML document relies on external data, in this case our DTD. We then declared a DOCTYPE using “<!DOCTYPE” followed by our “root element”, which was “xbox”. We then specified our DTD as being for public use using the “Public” identifier. We could use “Standalone” instead of “Public” if our language was created specifically for use by one entity or organization. We then declared the creator of the DTD, in this case it was Microsoft (“-//Microsoft-xbml//DTD xbox//EN//”). The minus symbol was used to specify that our DTD was not associated or endorsed by a standards body, such as the ISO, etc. Notice we also stated the language of our DTD, using “EN” for English in our example.

The last part of the DTD defines the URL to locate our DTD so parsers can access it:

Here we simply abbreviated our xbox markup language as “XBML” for short, followed by the actual DTD file itself. DTD’s must end with the “.dtd” file extension.