Java Reference
In-Depth Information
After all this complexity, you mustn't forget that an element may also be empty, in which case it can be
defined like this:
<!ELEMENT position EMPTY>
This states that the
<position>
element has no content. Elements can also have attributes, so let's take a
quick look at how they can be defined in a DTD.
Defining Element Attributes
You use an
ATTLIST
declaration in a DTD to define the attributes for a particular element. As you know,
attributes are name-value pairs associated with a particular element, and values are typically, but not exclus-
ively, text. Where the value for an attribute is text, it is enclosed between quotation marks, so it is always
unparsed character data. Attribute values that consist of text are therefore specified just as
CDATA
. No pre-
ceding
#
character is necessary in this context because there is no possibility of confusion.
You could declare the elements for a document containing circles as follows:
<?xml version="1.0"?>
<!DOCTYPE circle
[
<!ELEMENT circle (position)>
<!ATTLIST circle
diameter CDATA #REQUIRED
>
<!ELEMENT position EMPTY>
<!ATTLIST position
x CDATA #REQUIRED
y CDATA #REQUIRED
>
]>
<circle diameter="30">
<position x="30" y="50"/>
</circle>
Three items define each attribute — the attribute name, the type of value (
CDATA
), and whether or not the
attribute is mandatory. This third item may also define a default value for the attribute, in which case this
value is assumed if the attribute is omitted. The
#REQUIRED
specification against an attribute name indicates
that it must appear in the corresponding element. You specify the attribute as
#IMPLIED
if it need not be
included. In this case the XML processor does not supply a default value for the attribute. An application is
expected to have a default value of its own for the attribute value that is implied by the attribute's omission.
Save this XML in your
Beg Java Stuff
directory with a suitable name such as
"circle with
DTD.xml;"
it comes in handy in the next chapter.
You specify a default value for an attribute between double quotes. For example:
<!ATTLIST circle
diameter CDATA "2"
>