Java Reference
In-Depth Information
In fact an XML document always has a structure similar to this. Each element in a document can contain
other elements, or text, or elements and text, or it can be empty.
Document Type Definitions
We have seen several small examples of XML and in each case it was fairly obvious what the content
was meant to represent, but where are the rules that ensure such data is represented consistently and
correctly in different documents? Do the <radius> and <position> elements have to be in that
sequence in a <circle> element and could we omit one or other of them?
Clearly there has to be a way to determine what is correct and what is incorrect for any particular
element in a document. As we mentioned earlier, a D ocument T ype D efinition ( DTD ) defines how valid
elements are constructed for a particular type of document, so the XML for purchase order documents
in a company could be defined by one DTD, and sales invoice documents by another. The document
type definition for a document is specified in a document type declaration - commonly known as a
DOCTYPE declaration - that appears in the document prolog following any XML declaration. A DTD
essentially defines a vocabulary for describing data of a particular kind - the set of elements that you
use to identify the data in other words. It also defines the possible relationships between these elements
- how they can be nested. The contents of a document of the type indentified by a particular DTD must
be defined and structured according to rules that make up the DTD. Any document of a given type can
be checked for validity against its DTD.
A DTD can be an integral part of a document but it is usually, and more usefully, defined separately.
Including a DTD in an XML document makes the document self-contained, but it does increase its bulk.
It also means that the DTD has to appear within each document of the same type. A separate DTD that
is external to a document avoids this and provides a single reference point for all documents of a
particular type. An external DTD also makes maintenance of the DTD for a document type easier as it
only needs to be changed in one place for all documents that make use of it. Let's look at how we
identify the DTD for a document and then investigate some of the ways in which elements and their
attributes can be defined in a DTD.
Declaring a DTD
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 only have one DOCTYPE declaration.
You can include 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>
Search WWH ::




Custom Search