Document Type Definitions

A DTD specifies how elements inside an XML document should relate to each other. It also provides grammar rules for the document and each of its elements. A document that adheres to the specifications outlined by its DTD is considered to be valid. (Don’t confuse this with a well-formed document, which adheres to the XML syntax rules outlined earlier.)

Element Declarations

You must declare each of the elements that appear inside your XML document within your DTD. You can do so with the <!ELEMENT> declaration, which uses the this format:

<!ELEMENT elementname 
                  rule>

This declares an XML element and an associated rule, which relates the element logically in the XML document. The element name should not include <> characters. An element name must start with a letter or an underscore. After that, it can have any number of letters, numbers, hyphens, periods, or underscores in its name. Element names may not start with the string xml, in any variation of upper- or lowercase. You can use a colon in element names only if you are using namespaces; otherwise, it is forbidden.

ANY and PCDATA

The simplest element declaration states that between the opening and closing tags of the element, anything can appear:

<!ELEMENT library ANY>

Using the ANY keyword allows you to include both other tags and general character data within the element. However, you may want to specify a situation where you want only general characters appearing. This type of data is better known ...

Get XML Pocket Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.