DTD

Document Type Definition (DTD) is declared internally or externally using <!DOCTYPE ...>. It is the only schema language that allows entities to be defined.

The name of an external DTD file ends with ".dtd".

Internal DTD

The following example shows an internal DTD.

<?xml version="1.0" ?>
<!DOCTYPE class [
   <!ELEMENT class (student*) >
   <!ELEMENT student (name,DOB,(top|last)?,description)>
   <!ELEMENT name (#PCDATA)>
   <!ELEMENT DOB (#PCDATA)>
   <!ELEMENT top EMPTY>
   <!ELEMENT last EMPTY>
   <!ELEMENT description ANY>
   <!ATTLIST student
                    id  CDATA #IMPLIED
                    sex (M|F) #REQUIRED>
   <!ENTITY school "Hwa Chung Junior College">
]>
<class>
   <student id="H901234" sex="M">
      <name>Alex Lee</name>
      <DOB>23-03-1990</DOB>
      <top/>
      <description>studying at &school;.</description>
   </student>
   <student id="H904321" sex="F">
      <name>Mary Chan</name>
      <DOB>25-06-1990</DOB>
      <description>studying at &school;.</description>
   </student>
</class>
A DTD declares all elements, attributes, and entities used.

',' denotes a sequence: child elements separated by a comma must appear in the specified order. '|' denotes a choice.

+ means one or more.
* means zero or more.
? means zero or one.

The valid attribute types are: The valid attribute defaults are:

External DTD and Parameter Entities

The following example uses an external DTD together with parameter entities. Parameter entities, prefixed with %, cause substitutions within a DTD and must be declared in an external DTD.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE p SYSTEM "p.dtd">
<p>
   'ANY' allows you to mix <b>tags</b>
   with <b>parsed character data</b>.
</p>

<!ENTITY % x.y "p">
<!ENTITY % x.z "ANY">
<!ELEMENT %x.y; %x.z;>
<!ELEMENT b (#PCDATA)>

Public DTD

A public DTD is commonly used and widely shared. The XML processor may replace the public ID with a built-in URI. The second string after PUBLIC is a system identifier (URI), used if the public ID is not recognized.

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC
      "-//W3C//DTD XHTML 1.0 Transitional//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
</html>
(Courtesy of http://en.wikipedia.org/wiki/Document_type_definition#Examples, modified)

NMTOKEN

NMTOKEN, a valid XML name, is generally made up of alphanumeric characters, -, _, etc.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE p [
   <!ELEMENT p ANY>
   <!ELEMENT b (#PCDATA)>
   <!ATTLIST b
                   att NMTOKEN #FIXED "1">
]>
<p>
   <b att="1">!1, @1, #1, $1...</b> are valid CDATA
   but not valid NMTOKEN.
</p>

ID, IDREF, and IDREFS

The following example shows how to use attributes declared as ID, IDREF, and IDREFS.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE p [
   <!ELEMENT p ANY>
   <!ELEMENT b (#PCDATA)>
   <!ATTLIST p
                   ir IDREFS #IMPLIED>
   <!ATTLIST b
                   id ID #IMPLIED>
]>
<p ir="b1 b2">
   The value of an attribute declared as
   <b id="b1">ID</b> must be <b id="b2">unique</b>.
</p>

External Entity Declarations

The following are examples of external entity declarations. The first two are parsed external entities: the parser retrieves the contents of the referenced files and parses them as if they were internal entities. The third is an unparsed external entity: how it is processed is left to the application, according to the notation assigned. Here, gif is a notation declared elsewhere.

<!ENTITY open-hatch SYSTEM
       "http://www.textuality.com/boilerplate/OpenHatch.xml">
<!ENTITY open-hatch PUBLIC
      "-//Textuality//TEXT Standard open-hatch boilerplate//EN"
      "http://www.textuality.com/boilerplate/OpenHatch.xml">
<!ENTITY hatch-pic SYSTEM
      "../grafix/OpenHatch.gif"
      NDATA gif >
(Courtesy of http://www.w3.org/TR/2008/REC-xml-20081126/#sec-external-ent)

ENTITY Attribute Type

The value of an attribute declared as ENTITY must be the name of an unparsed external entity.

<?xml version="1.0" standalone="no" ?>
<!DOCTYPE img [
  <!ELEMENT img EMPTY>
  <!ATTLIST img
                  src ENTITY #REQUIRED>
  <!ENTITY logo SYSTEM
                 "http://www.xmlwriter.net/logo.gif" NDATA gif>
  <!NOTATION gif PUBLIC "gif viewer">
]>
<img src="logo"/>