DTD Basics   «Prev  Next»

Lesson 7Referencing DTD declarations
ObjectiveReference DTD Declarations in XML

Reference DTD Declarations in XML

Every XML file that uses a DTD must have a document type declaration[1] which is used to declare the DTD. That is, the structure and syntax of a given DTD is defined using a document type declaration. A document type declaration has thefollowing general syntax:

<!DOCTYPE root element name [DTD]>

This syntax may vary depending on whether the document type declaration points to an internal DTD, as indicated using the syntax above, or an external DTD. Using a document type declaration to reference an external DTD will be presented later in the course.

How a DTD declaration can be referenced?

In an XML document, a DTD (Document Type Definition) declaration can be referenced using a DOCTYPE declaration at the beginning of the document. The DOCTYPE declaration specifies the name of the root element of the XML document and the location of the DTD file.
Here is an example of a DOCTYPE declaration that references a DTD file named "example.dtd":


In this example, the "example" keyword specifies the name of the root element of the XML document, and the "SYSTEM" keyword indicates that the DTD file is located on the local system. The "example.dtd" string specifies the file path and name of the DTD file. The DTD file itself contains the rules and definitions for the elements and attributes used in the XML document. Here is an example of a simple DTD file that defines an element named "example" with child elements for "name", "age", and "address":

<!ELEMENT example (name, age, address)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT age (#PCDATA)>
<!ELEMENT address (street, city, state, zip)>
<!ELEMENT street (#PCDATA)>
<!ELEMENT city (#PCDATA)>
<!ELEMENT state (#PCDATA)>
<!ELEMENT zip (#PCDATA)>

When an XML document is validated against a DTD, the parser checks that the document conforms to the rules and definitions specified in the DTD. This helps to ensure that the document is well-formed and contains valid data.

The Standalone Attribute

A special standalone attribute may be added to the <?xml?> declaration to indicate whether the <!DOCTYPE> declaration should point to
  1. internal or
  2. external
DTD declarations. If all the DTD information will be enclosed within the XML document, you can specify that the standalone attribute's value is "yes". If the XML document should be referencing an external DTD, the value of this attribute should be "no" The following tag indicates that the DTD information should appear in the XML document itself:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 

If the DTD will exist in a separate file, you would use the following XML declaration instead:
<?xml version="1.0" encoding="UTF-8" standalone="no"?> 

Both the internal and external DTD examples require a <!DOCTYPE> declaration in some form.

The <!DOCTYPE> declaration should be on the line just below the <?xml?> declaration to avoid errors when the document is parsed.
In the next lesson, I will discuss how to create a DTD file to work with a separate XML files.
[1]Document Type Declaration : The document type declaration names the document type being used and links to or includes its definition, the DTD. The <!DOCTYPE > declaration should be on the line just below the <?xml?> declaration.