XML Document Type Definition (DTD):
Introduction to DTD for Defining XML Document Structure:
A Document Type Definition (DTD) is a set of rules that define the structure and the legal elements and attributes of an XML document.
They help in enforcing the structure for the XML documents which is an important factor for good structure in the transfer and storing of data.
Key Features of DTD:
- Defines the elements and attributes that can appear in an XML document.
- Specifies the structure and nesting of elements.
- Can declare entities to simplify the document and manage reusable content.
Internal and External DTDs
DTD can be included directly within an XML document (internal DTD) or in an external file (external DTD).
Internal DTD:
Declared within the <!DOCTYPE> declaration at the beginning of the XML document.
Example:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE bookstore [
<!ELEMENT bookstore (book+)>
<!ELEMENT book (title, author, price)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT price (#PCDATA)>
]>
<bookstore>
<book>
<title>XML Fundamentals</title>
<author>John Smith</author>
<price>29.99</price>
</book>
</bookstore>
In this particular case, the DTD is embedded within the XML document itself; the structure of a sample <bookstore> and its sub-elements.
External DTD:
Located in another document and whether in the XML document by using the SYSTEM keyword in the <!DOCTYPE> declaration.
Example:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE bookstore SYSTEM "bookstore.dtd">
<bookstore>
<book>
<title>XML Fundamentals</title>
<author>John Smith</author>
<price>29.99</price>
</book>
</bookstore>
Contents of bookstore.dtd:
<!ELEMENT bookstore (book+)>
<!ELEMENT book (title, author, price)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT price (#PCDATA)>
In this example, the XML document invokes an external DTD that is in a file called bookstore. dtd.
Elements, Attributes, and Entities in DTD:
Elements:
Defined using <!ELEMENT> declarations, specifying the element name and its content model.
Example:
<!ELEMENT book (title, author, price)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT price (#PCDATA)>
The book in this example has other components including title, author, and price; all of which consist of #PCDATA.
Attributes:
Defined using declarations and define the element, the attribute name, its data type, and its optional default value.
Example:
<!ELEMENT book (title, author, price)>
<!ATTLIST book id ID #REQUIRED>
In this example, the book element has an attribute id of type ID that is required.
Entities:
Entities are reusable fragments of text that are created using <ENTITY> declarations. It can be used to introduce special characters or repeated content.
Example:
<!ENTITY author "John Smith">
<!ELEMENT book (title, author, price)>
<!ELEMENT author (#PCDATA)>
In this case, the entity author is defined as “John Smith,” which can be further utilized within the document and helps to prevent repetitiveness.