Java Reference
In-Depth Information
<!ELEMENT address (buildingnumber, street, city, state, zip)>
<!ELEMENT buildingnumber (#PCDATA)>
<!ELEMENT street (#PCDATA)>
<!ELEMENT city (#PCDATA)>
<!ELEMENT state (#PCDATA)>
<!ELEMENT zip (#PCDATA)>
The DOCTYPE declaration identifies the DTD for a particular document, so it is not part of the DTD. If the
preceding DTD were stored in the AddressDoc.dtd file in the same directory as the document, the DOCTYPE
declaration in the document would be the following:
<?xml version="1.0"?>
<!DOCTYPE address SYSTEM "AddressDoc.dtd">
<address>
<buildingnumber> 29 </buildingnumber>
<street> South LaSalle Street</street>
<city>Chicago</city>
<state>Illinois</state>
<zip>60603</zip>
</address>
Of course, the DTD file would also include definitions for element attributes, if there were any. These will
be useful later, so save the DTD (as AddressDoc.dtd ) and the preceding XML file (as Address.xml, per-
haps) in your Beg Java Stuff directory that is in your user.home directory.
One further possibility you need to consider is that in many situations it is desirable to allow some child
elements to be omitted. For example, <buildingnumber> may not be included in some cases. The <zip>
element, while highly desirable, might also be left out in practice. We can indicate that an element is op-
tional by using the cardinality operator , ? . This operator expresses the same idea as the equivalent regular
expression operator, and it indicates that a child element may or may not appear. The DTD would then look
like this:
<!DOCTYPE address
[
<!ELEMENT address (buildingnumber?, street, city, state, zip?)>
<!ELEMENT buildingnumber (#PCDATA)>
<!ELEMENT street (#PCDATA)>
<!ELEMENT city (#PCDATA)>
<!ELEMENT state (#PCDATA)>
<!ELEMENT zip (#PCDATA)>
]>
The ? operator following an element indicates that the element may be omitted or may appear just once.
This is just one of three cardinality operators that you use to specify how many times a particular child
element can appear as part of the content for the parent. The cardinality of an element is simply the number
of possible occurrences for the element. The other two cardinality operators are * , which you have already
seen, and + . In each case the operator follows the operand to which it applies. You now have four operators
that you can use in element declarations in a DTD, and they are each similar in action to their equivalent in
the regular expression context (see Table 22-2 ) :
TABLE 22-2 : Operators for Element Declarations in a DTD
 
 
Search WWH ::




Custom Search