An introduction to DTDs

Rules for the proudcuts.xml docuent:

  • the document must contain one and only one Produts element.
  • the document can contain multiple Product elements.
  • each Product element must contain two elements named Description and Price.
  • Each Product element mst cointain one attribute name Code that holds a string.
  • The Desription and Price elements can contain text data, but can't contain child elements.

A DTD that implements these 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) >

How to specify a DTD file in an XML document:

<?xml version="1.0" encoding=utf-8 ?>
<!- Product data –>
<Products>
<product code="java">
<Description>Beginning Java 2</Description>
<Price>49.50</Price>
</product>
<product code="jsps">
<Description>Java Servlets and JSP</Description>
<Price>51.50</Price>
</product>
<product code="zjcl">
<Description>OS/390 and z/OS JCL</Description>
<Price>62.50</Price>
</product>
</Products>

Description

  • DTD allows you to set conditions that must be enforced on an XML document. To defiane 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.
  • You use ELEMENT declaration in a DTD to define the names of the elements and the types of data tye will contain. You use the ATTLIST declaration to define the names of the attributes ans 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 the child element on the parent element declaration.
  • To specify that a child element can occur one or more times, code a plus sign (+) after the name of the child element on the parent element declaration.