Java Reference
In-Depth Information
You can see that both structures have a single root element, <address> in the first example and <circle>
in the second. You can also see that each element contains either other elements or some data that is a seg-
ment of the document content. In both diagrams all the document content lies at the bottom. Nodes at the
extremities of a tree are referred to as leaf nodes .
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
You 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 <diameter> and <position> elements have to be in that sequence in a
<circle> element, and could you omit either 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 I mentioned earlier, a 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 identified 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 every 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 you 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
Search WWH ::




Custom Search