The DTD of a document can be internal to the document or external. It must always be placed between XML declaration and root element. DTD begins with keyword !DOCTYPE between start and end tag.

 Internal DTD

The rules defined internally are for the elements of that document only. The DTD is specified between square brackets of DOCTYPE declaration.

Syntax of internal DTD in XML

<!DOCTYPE root_element[

<elements> or <attributes> or <entities> or <notations> or <PI> or <comments>

]>

Example with internal DTD in XML

<?xml version="1.0" standalone="yes"?>

<!DOCTYPE GAME [

<!ELEMENT GAME (team1, team2, score)>

<!ELEMENT team1 (#PCDATA)>

<!ELEMENT team2 (#PCDATA)>

<!ELEMENT score (#PCDATA)>

]>

<GAME>

      <team1>SA</team1>

      <team2>UK</team2>

      <score>2-0</score>

</GAME>

External DTD

The DTD can be applied to multiple documents. Any change in DTD gets reflected in all the XML documents sharing the DTD. The keyword standalone="no" should be added to the XML declaration at the beginning of the document indicating it is dependent on other files. The declaration for external DTD subset is specified after ‘SYSTEM’ or ‘PUBLIC’ keywords. External DTD are in two types:

Private (system) specification in XML

The DTD may be private if users or a single person are a group of authors. The keyword SYSTEM in the declaration of the DTD indicates the same.

Syntax of external private DTD in XML

<!DOCTYPE root_element SYSTEM “DTD_Location”>

Example with external private DTD in XML

<?xml version="1.0" standalone="no"?>

<!DOCTYPE GAME SYSTEM "Game.dtd">

<GAME>

      <team1>SA</team1>

      <team2>UK</team2>

      <score>2-0</score>

</GAME>

With the Game.dtd file being:

<!ELEMENT GAME (team1, team2, score)>

<!ELEMENT team1 (#PCDATA)>

<!ELEMENT team2 (#PCDATA)>

<!ELEMENT score (#PCDATA)>

Public specification in XML

The external DTD’s with PUBLIC keyword are intended to larger public use such as, organizations such as ISO, IEEE. The DTD’s defined this way standardizes the tag names and makes the usage, exchange and manipulation of the documents easier. Generally they are stored in web servers and shared among its users.

Syntax of external public DTD in XML

<!DOCTYPE root_element PUBLIC “DTD_name” “DTD_URL”>

The conventions of DTD name used with PUBLIC are:

  • If DTD is ISO standard the name begins with ISO.
  • For non-ISO, if DTD is approved by a standard body the name begins with +.
  • For non-ISO, if DTD is not approved by any standard bodies the name begins with -.
  • These initial string is followed by ‘// DTD_owner’ and ‘//Type_of_document’ the DTD describes.
  • This can be followed by ‘//ISO 639 language identifier’.

Example with external public DTD in XML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

›› go to examples ››