Java Reference
In-Depth Information
You use a document type declaration (a DOCTYPE declaration) in the prolog of an XML document to specify
the DTD for the document. An XML 1.0 document can have only one DOCTYPE declaration. You can in-
clude the markup declarations for elements used in the document explicitly within the DOCTYPE statement, in
which case the declarations are referred to as the internal subset . You can also specify a URI that identifies
the DTD for the document, usually in the form of a URL. In this case the set of declarations is referred to as
the external subset . If you include explicit declarations as well as a URI referencing an external DTD, the
document has both an internal and an external subset. Here is an example of an XML document that has an
external subset:
<?xml version="1.0"?>
<!DOCTYPE address SYSTEM "http://docserver/dtds/AddressDoc.dtd">
<address>
<buildingnumber> 29 </buildingnumber>
<street> South LaSalle Street</street>
<city>Chicago</city>
<state>Illinois</state>
<zip>60603</zip>
</address>
The name following the DOCTYPE keyword must always match the root element name in the document,
so the DOCTYPE declaration here indicates that the root element in the document has the name address . The
declaration also indicates that the DTD in which this and the other elements in the document are declared is
an external DTD located at the URI following the SYSTEM keyword. This URI, which is invariably a URL,
is called the system ID for the DTD.
In principle, you can also specify an external DTD by a public ID using the keyword PUBLIC in place
of SYSTEM . A public ID is just a unique public name that identifies the DTD — a Uniform Resource Name
(URN), in other words. As you probably know, the idea behind URNs is to get over the problem of changes
to URLs. Public IDs are intended for DTDs that are available as public standards for documents of particular
types, such as SVG. However, there is a slight snag. Because there is no mechanism defined for resolving
public IDs to find the corresponding URL, if you specify a public ID, you still have to supply a system ID
with a URL so the XML processor can find it, so you won't see public IDs in use much.
If the file containing the DTD is stored on the local machine, you can specify its location relative to the
directory containing the XML document. For example, the following DOCTYPE declaration implies that the
DTD is in the same directory as the document itself:
<!DOCTYPE address SYSTEM "AddressDoc.dtd">
The AddressDoc.dtd file includes definitions for the elements that may be included in a document con-
taining an address. In general, a relative URL is assumed to be relative to the location of the document con-
taining the reference.
Defining a DTD
In looking at the details of how we put a DTD together, I'll use examples in which the DTD is an internal
subset, but the declarations in an external DTD are exactly the same. Here's an example of a document with
an integral DTD:
<?xml version="1.0"?>
<!DOCTYPE proverb [ <!ELEMENT proverb (#PCDATA)>
]>
Search WWH ::




Custom Search