Cover Image for XML DTD
81 views

XML DTD

XML DTD (Document Type Definition) is a formal declaration used to define the structure, elements, and attributes of an XML document. It serves as a schema or blueprint for validating and describing the content of XML documents, ensuring their conformity to a specific set of rules.

DTDs were one of the earliest methods used to define the structure of XML documents before other schema languages like XML Schema (XSD) became more prevalent. Although XSD is now widely used for XML validation, DTDs are still supported by many XML processors and are used in some legacy systems.

A DTD is written in plain text and typically has a “.dtd” file extension. It can be either embedded within an XML document or declared externally, and it contains the following components:

  1. Element Declarations:
  • Define the elements that can appear in the XML document and their content models (sequence, choice, or mixed).
  • Example: <!ELEMENT book (title, author, price)>
  1. Attribute Declarations:
  • Define the attributes that can be associated with specific elements and their data types.
  • Example: <!ATTLIST book category CDATA #IMPLIED>
  1. Entity Declarations:
  • Define named entities to represent special characters, strings, or reusable pieces of content.
  • Example: <!ENTITY authorName "John Doe">
  1. Notation Declarations:
  • Define notations for multimedia objects, such as images or audio files, used in the XML document.
  • Example: <!NOTATION gif SYSTEM "image/gif">
  1. Comments and Processing Instructions:
  • Comments (enclosed in <!-- ... -->) and processing instructions (starting with <? and ending with ?>) provide human-readable notes or instructions within the DTD.

Example of a Simple XML DTD (books.dtd):

<!ELEMENT books (book+)>
<!ELEMENT book (title, author, price)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT price (#PCDATA)>
<!ATTLIST book category CDATA #IMPLIED>

In this example, the DTD defines a simple structure for an XML document containing books. It specifies that the root element is “books,” which must contain one or more “book” elements. Each “book” element must contain “title,” “author,” and “price” elements. The “book” element can have an optional “category” attribute.

To use this DTD to validate an XML document, you need to reference it either as an internal DTD subset within the XML document or as an external DTD file in the DOCTYPE declaration.

Example of an XML Document (books.xml) with Internal DTD Subset:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE books SYSTEM "books.dtd">
<books>
  <book category="fiction">
    <title>XQuery 101</title>
    <author>John Doe</author>
    <price>25.00</price>
  </book>
  <book>
    <title>XML Fundamentals</title>
    <author>Jane Smith</author>
    <price>30.00</price>
  </book>
</books>

DTDs are relatively simple and compact but have some limitations compared to more advanced schema languages like XSD. They lack support for defining data types, complex structures, and namespaces. For more complex and comprehensive XML validation, XML Schema (XSD) is the preferred choice.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS