Sobes.tech
Junior

What is DTD (Document Type Definition) and what is its role in XML documents?

sobes.tech AI

Answer from AI

DTD (Document Type Definition) is a schema that defines the structure of an XML document. It describes:

  • Which elements can be present.
  • Attributes of each element.
  • The order and nesting of elements.
  • Data types for the content of elements and attributes.

The role of DTD in XML documents:

  • Validation: Allows checking whether an XML document conforms to a specific structure, ensuring data integrity.
  • Structure description: Provides a formal description of permissible elements and their relationships, simplifying understanding and processing of the document.
  • Reusability: A single DTD can be used to validate multiple XML documents with the same structure.

Example of defining an element and attribute in DTD:

<!ELEMENT книга (название, автор, год)>
<!ELEMENT название (#PCDATA)>
<!ELEMENT автор (#PCDATA)>
<!ELEMENT год (#PCDATA)>
<!ATTLIST книга
 id CDATA #REQUIRED>

Example of an XML document referencing a DTD:

<!DOCTYPE книга SYSTEM "книга.dtd">
<книга id="123">
 <название>Пример книги</название>
 <автор>Иван Иванов</автор>
 <год>2023</год>
</книга>
What is DTD (Document Type Definition) and what is… - sobes.tech