DTD Tutorial

Welcome to The Coding College! In this tutorial, we will explore Document Type Definitions (DTD), an essential concept in XML for defining the structure, elements, and attributes of an XML document. DTD ensures that XML documents conform to a specified format, making them easier to validate and share across systems.

What is a DTD?

A Document Type Definition (DTD) defines the structure and rules for an XML document. It specifies:

  • The allowed elements and their order.
  • Attributes for each element.
  • Relationships between elements.

Using DTD, XML documents can be validated to ensure they follow the defined structure.

Types of DTD

There are two ways to include a DTD in an XML document:

  1. Internal DTD: The DTD is embedded directly within the XML document.
  2. External DTD: The DTD is stored in a separate file and referenced from the XML document.

Internal DTD Example

XML with Internal DTD:

<!DOCTYPE library [
  <!ELEMENT library (book+)>
  <!ELEMENT book (title, author, price)>
  <!ELEMENT title (#PCDATA)>
  <!ELEMENT author (#PCDATA)>
  <!ELEMENT price (#PCDATA)>
]>
<library>
  <book>
    <title>Learning XML</title>
    <author>John Doe</author>
    <price>29.99</price>
  </book>
  <book>
    <title>Mastering XQuery</title>
    <author>Jane Smith</author>
    <price>39.99</price>
  </book>
</library>

Explanation:

  • <!ELEMENT> defines the structure of each element.
  • #PCDATA means the element contains text data.
  • (book+) means the <library> element must have one or more <book> elements.

External DTD Example

  1. Create a DTD file (library.dtd):
<!ELEMENT library (book+)>
<!ELEMENT book (title, author, price)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT price (#PCDATA)>
  1. Reference the DTD in the XML file:
<!DOCTYPE library SYSTEM "library.dtd">
<library>
  <book>
    <title>Learning XML</title>
    <author>John Doe</author>
    <price>29.99</price>
  </book>
  <book>
    <title>Mastering XQuery</title>
    <author>Jane Smith</author>
    <price>39.99</price>
  </book>
</library>

Explanation:
The SYSTEM keyword references the external DTD file library.dtd.

DTD Syntax and Rules

1. Defining Elements

  • Syntax:
<!ELEMENT element-name (content-type)>
  • Content Types:
    • #PCDATA: Text data.
    • (child1, child2): Specific child elements in sequence.
    • (child1 | child2): One of the child elements.
    • (child1*): Zero or more occurrences.
    • (child1+): One or more occurrences.

Example:

<!ELEMENT book (title, author, price)>
<!ELEMENT title (#PCDATA)>

2. Defining Attributes

  • Syntax:
<!ATTLIST element-name attribute-name attribute-type default-value>
  • Attribute Types:
    • CDATA: Character data.
    • ID: Unique identifier for the element.
    • IDREF: Reference to another element’s ID.
    • (value1 | value2): Enumerated values.

Example:

<!ELEMENT book (title, author, price)>
<!ATTLIST book category (Programming | Databases) "Programming">

Explanation:

  • The category attribute must be either “Programming” or “Databases.”
  • The default value is “Programming.”

3. Defining Mixed Content

Mixed content allows elements to contain both text and other elements.

Example:

<!ELEMENT paragraph (#PCDATA | bold | italic)*>
<!ELEMENT bold (#PCDATA)>
<!ELEMENT italic (#PCDATA)>

4. Defining Empty Elements

An element with no content is defined as EMPTY.

Example:

<!ELEMENT br EMPTY>

Validating XML with DTD

Validation ensures that an XML document conforms to the rules defined in the DTD.

Steps for Validation:

  1. Create a DTD and include it in your XML file (internal or external).
  2. Use an XML validator tool (like XML Validator, Altova XMLSpy, or online tools).

Advantages of DTD

  • Ensures XML document consistency.
  • Defines the document structure clearly.
  • Supports validation to detect errors.

Limitations of DTD

  • Limited support for data types (e.g., no support for numeric types).
  • Cannot enforce constraints like “minimum or maximum value.”
  • Less powerful compared to XML Schema.

DTD vs XML Schema

FeatureDTDXML Schema
Data TypesLimited (#PCDATA, CDATA)Extensive (integers, dates)
NamespacesNot SupportedSupported
Complex ConstraintsNot SupportedSupported
ReadabilitySimplerMore complex

Tools for Working with DTD

  1. Altova XMLSpy: A powerful XML editor for creating and validating DTDs.
  2. Oxygen XML Editor: A versatile tool for DTD and schema validation.
  3. Online Validators: Quickly test your XML and DTD (e.g., xmlvalidation.com).

Conclusion

DTD is a foundational tool for defining the structure and validating XML documents. While it has limitations compared to modern alternatives like XML Schema, it remains a lightweight and widely supported option for XML validation.

For more tutorials and resources, visit The Coding College and enhance your XML skills! Keep learning, keep coding!

Leave a Comment