Quackit Logo
HTML
CSS
Scripting
Database
Hosting
Design
XML

Print Version

DTD <!DOCTYPE>

If you've had the opportunity to view some XML documents, you may have noticed a line starting with <!DOCTYPE appearing near the top of the document. For example, if you've viewed the source code of a (valid) XHTML file, you may have seen a line like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

The purpose of this line is to declare the Document Type Definition (DTD). Actually, we even used the DOCTYPE declaration in a previous lesson to define an entity.

As mentioned in the previous lesson, a DTD specifies the rules regarding the elements within your XML document.

DOCTYPE Syntax

To use a DTD within your XML document, you need to declare it. The DTD can either be internal (written into the same document that it's being used in), or external (located in another document).

You declare a DTD at the top of your XML document (in the prolog) using the <!DOCTYPE declaration. The basic syntax is:

<!DOCTYPE rootname [DTD]>

...where, rootname is the root element, and [DTD] is the actual definition.

Actually, there are slight variations depending on whether your DTD is internal or external (or both), public or private. Here they are:

DOCTYPE VariationExampleDescription
<!DOCTYPE rootname [DTD]>
<!DOCTYPE tutorials [
<!ELEMENT tutorials (tutorial)+>
<!ELEMENT tutorial (name,url)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT url (#PCDATA)>
<!ATTLIST tutorials type CDATA #REQUIRED>
]>
  • This is an internal DTD (the DTD is defined between the square brackets within the XML document).
<!DOCTYPE rootname SYSTEM URL> <!DOCTYPE tutorials SYSTEM "tutorials.dtd">
  • The keyword SYSTEM indicates that it's a private DTD (not for public distribution).
  • The presence of URL indicates that this is an external DTD (the DTD is defined in a document located at the URL).
<!DOCTYPE rootname SYSTEM URL [DTD]>
<!DOCTYPE tutorials SYSTEM "tutorials.dtd" [
<!ELEMENT tutorial (summary)>
<!ELEMENT summary (#PCDATA)>
]>
  • The keyword SYSTEM indicates that it's a private DTD (not for public distribution).
  • The presence of URL and [DTD] together indicates that this is both an external and internal DTD (part of the DTD is defined in a document located at the URL, the other part is defined within the XML document).
<!DOCTYPE rootname PUBLIC identifier URL> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  • The keyword PUBLIC indicates that it's a public DTD (for public distribution).
  • The presence of URL indicates that this is an external DTD (the DTD is defined in a document located at the URL).
  • The identifier indicates the formal public identifier and is required when using a public DTD.
<!DOCTYPE rootname PUBLIC identifier URL [DTD]> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" [
<!ELEMENT tutorials (tutorial)+>
<!ELEMENT tutorial (name,url)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT url (#PCDATA)>
<!ATTLIST tutorials type CDATA #REQUIRED>
]>
  • The keyword PUBLIC indicates that it's a public DTD (for public distribution).
  • The presence of URL and [DTD] together indicates that this is both an external and internal DTD (part of the DTD is defined in a document located at the URL, the other part is defined within the XML document).
  • The identifier indicates the formal public identifier and is required when using a public DTD.

The next few lessons demonstrate some of the different methods of declaring your DTD, depending on whether you're using an internal, external, or combined DTD.

Enjoy this website?

  • Share
  • Add this page to your Favorites
  • Link to this page (copy/paste into your own website or blog):
  • Link to Quackit using one of these banner ads.
  • Help support Quackit by making a donation

Oh, and thank you for supporting Quackit!

© Copyright 2000 - 2010 Quackit.com