Java Reference
In-Depth Information
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 will be 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 will 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 will come in handy later.
You specify a default value for an attribute between double quotes. For example:
<!ATTLIST circle
radius CDATA "1"
>
This indicates that the value of
radius
will be
1
if the attribute is not specified for a
<circle>
element.
You can also insist that a value for an attribute must be one of a fixed set. For instance, suppose we had
a
color
attribute for our circle that could only be red, blue, or green. We could define it like this:
<!ATTLIST circle
color (red|blue|green) #IMPLIED
>
The value for the
color
attribute in a
<circle>
element must be one of the options between the
parentheses. In this case the attribute can be omitted because it is specified as
#IMPLIED
, and an
application processing it will supply a default value. To make the inclusion of the attribute mandatory,
we would define it as:
<!ATTLIST circle
color (red|blue|green) #REQUIRED
>
An important aspect of defining possible attribute values by an enumeration like this is that an XML
editor can help the author of a document by prompting with the list of possible attribute values from the
DTD when the element is being created.
An attribute that is declared as
#FIXED
must always have the default value. For example:
<!ATTLIST circle
color (red|blue|green) #REQUIRED
line
_
thickness medium #FIXED
>
Here the XML processor will only supply an application with the value
medium
for the
thickness
attribute. If you were to specify this attribute for the
<circle>
element in the body of the document
you can only use the default value, otherwise it is an error.