Java Reference
In-Depth Information
<!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 DTD above were stored in the AddressDoc.dtd file in the same directory as the document, the
DOCTYPE declaration in the document would be:
<?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 XML file above (as Address.xml
perhaps), in your Beg Java Stuff directory.
One further possibility we need to consider is that in many situations it is desirable to allow some child
elements to be omitted. For instance, <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 optional by using the cardinality operator , ? . This operator expresses the same idea as the
equivalent regular expression operator - 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 other two cardinality operators are
* , which we have already seen, and + . In each case the operator follows the operand to which it applies.
We now have four operators that we can use in element declarations and they are each similar in action
to their equivalent in the regular expression context:
Search WWH ::




Custom Search