DOCTYPE

In XML ([[Extensible Markup Language]]), a DOCTYPE declaration, also known as a [[Document Type Definition (DTD)|Document Type Declaration]], serves a purpose similar to its counterpart in [[HTML]]. It defines the structure and the rules for an XML document. However, in XML, the DOCTYPE declaration is used to specify the Document Type Definition (DTD) that the document adheres to.

The DOCTYPE declaration in an XML document is used to reference a DTD. A DTD defines the structure and the legal elements and attributes for an XML document. The syntax for a DOCTYPE declaration in XML can be either internal or external:

For internal, the DTD is defined within the XML document itself:

<!DOCTYPE root-element [
    ... DTD definitions ...
]>

For external, the DTD is defined in an external file, and the declaration references the external file:

<!DOCTYPE root-element SYSTEM "URL or path to DTD file">

The various components include:

  • root-element: Specifies the root element of the document.
  • SYSTEM: Indicates that the DTD is external. Alternatively, PUBLIC can be used to reference a public DTD.
  • "URL or path to DTD file": The location of the DTD file.

The DOCTYPE declaration allows an XML parser to validate the document against the specified DTD, ensuring that the document adheres to the rules of that DTD. While DTD is one way to define the rules and structure for XML documents, another more powerful and complex way is using XML Schema Definitions (XSD). XML Schemas provide more functionalities and are written in XML syntax.

An example of an XML document with an internal DTD:

<!DOCTYPE note [
  <!ELEMENT note (to,from,heading,body)>
  <!ELEMENT to (#PCDATA)>
  <!ELEMENT from (#PCDATA)>
  <!ELEMENT heading (#PCDATA)>
  <!ELEMENT body (#PCDATA)>
]>
<note>
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget the meeting!</body>
</note>

In this example, the DOCTYPE declaration defines the structure of the note element and its sub-elements. An XML parser can use this information to validate the document.