XML
Introduction
DTDs
An introduction to DTDs
- XML allows you to set conditions that must be enforced on an XML document.
- To define these conditions, you use a schema language to create a schema.
- Document Type Definition (DTD) is a schema language that's part of standard XML.
Rules for the products.xml document
- The document must contain one and only one Products element.
- The document can contain multiple Product elements.
- Each Product element must contain two elements named Description and Price.
- Each Product element must contain one attribute named Code that holds a string.
- The Description and Price elements can contain text data, but they can't contain child elements.
A DTD that implements the products.xml rules
<?xml version="1.0" encoding="UTF-8"?> <!-- DTD for the products.xml file. --> <!ELEMENT Products (Product*)> <!ELEMENT Product (Description, Price)> <!ATTLIST Product Code CDATA #REQUIRED> <!ELEMENT Description (#PCDATA)> <!ELEMENT Price (#PCDATA ) >
Coding a DTD
- You use the ELEMENT declaration in a DTD to define the names of the elements and the types of data they will contain.
- You use the ATTLIST declaration to define the names of the attributes and the types of data they will contain.
- By default, each child element must occur one time. To specify that a child element can occur:
- zero or one time, code a question mark after the name of a child element on the parent element declaration
- zero or more times, code an asterisk
- one or more times, code a plus sign
- In an XML document, you can use a DOCTYPE declaration to refer to a DTD that's stored in a DTD file.