Java Reference
In-Depth Information
The PCDATA specification does provide for markup - child elements - to be mixed in with ordinary
text. In this case you must specify the names of the elements that can occur mixed in with the text. If
you wanted to allow a <suite> element specifying a suite number to appear alongside the text within a
<buildingnumber> element you could express it like this:
<!ELEMENT buildingnumber (#PCDATA|suite)*>
This indicates that the content for a <buildingnumber> element is parsed character data and the text
can be combined with <suite> elements. The | operator here has the same meaning as the | operator
we met in the context of regular expressions in Chapter 13. It means one or other of the two operands
but not both. The * following the parentheses is required here, and has the same meaning as the *
operator that we also met in the context of regular expressions. It means that the operand to the left can
appear zero or more times.
If you want to allow several element types to be optionally mixed in with the text, you separate them by
| . Note that it is not possible to control the sequence in which mixed content appears.
The other elements used to define an address are similar, so we could define the whole document with
its DTD like this:
<?xml version="1.0"?>
<!DOCTYPE address
[
<!ELEMENT address (buildingnumber, street, city, state, zip)>
<!ELEMENT buildingnumber (#PCDATA)>
<!ELEMENT street (#PCDATA)>
<!ELEMENT city (#PCDATA)>
<!ELEMENT state (#PCDATA)>
<!ELEMENT zip (#PCDATA)>
]>
<address>
<buildingnumber> 29 </buildingnumber>
<street> South Lasalle Street</street>
<city>Chicago</city>
<state>Illinois</state>
<zip>60603</zip>
</address>
One point to note is that we have no way to constrain the text in an element definition. It would be nice
to be able to specify that the building number had to be numeric, for example, but the DTD grammar
and syntax provide no way to do this. This is a serious limitation of DTDs and one of the driving forces
behind the development of an alternative, XML Schemas. Schemas are beyond the scope of this topic
but if you want to know more you should get hold of a copy of Pro XML Schemas by Jon Duckett, Oliver
Griffin, et al, Wrox Press Ltd., (ISBN 1-861005-47-4)
If we were to create the DTD for an address document as a separate file, the file contents would just
consist of the element definitions:
<!ELEMENT address (buildingnumber, street, city, state, zip)>
<!ELEMENT buildingnumber (#PCDATA)>
Search WWH ::




Custom Search