If an element is to have attributes, the names and possible values of those attributes must be declared in the DTD. Here is the general form:
<!ATTLISTename{anameatypedefault} ...>
where
is
the name of the element for which you're defining
attributes,
ename is
the name of one of that element's possible attributes,
aname
describes what values it can have, and
atype
describes whether it has a default value. The last three
items can be repeated inside an
default<!ATTLIST...> declaration, one
group per attribute.
The
part describing the attribute's type can have three kinds
of values:atype
The keyword CDATA means that
the attribute can have any character string as a
value.
For example, suppose you want every
<play> element to have
a title attribute that can
contain any text, and that attribute is required. Here is
the complete attribute declaration:
<!ATTLIST play title CDATA #REQUIRED>
There are several tokenized attribute types, which are required to have a certain structure. See tokenized attributes below.
You can provide a specific set of legal values for the attribute; see enumerated attributes below.
The last part of the declaration,
,
specifies whether the attribute can be omitted, and what
value it will have if omitted. This must be one of the
following:default
#REQUIREDThe attribute must always be supplied.
#IMPLIEDThe attribute can be omitted, and the DTD does not provide a default value. Anyone reading this file may assume a default value, but that is not the DTD's problem.
"value"The attribute can be omitted, and the default value is the quoted string that you provide.
#FIXED
"value"The attribute must be given and must have the given
".value"
You can restrict an attribute to have only values
with a certain structure. Here are the possible values
of the
part of the attribute declaration for such
attributes:atype
IDAn ID attribute must be
a unique identifier for that node. This allows other
nodes to refer to it. The attribute value must also
be a
valid XML name
(see above).
IDREFAn IDREF attribute is a
reference to an ID attribute
in a different node.
For example, suppose that in
your DTD, there is a <sailor> element with an
ID-type
nickname attribute, and another
element <duty> with
an IDREF-type attribute called
sailor-nick. Then if you have
an element like this:
<sailor nickname='Bluto'>...</sailor>
then this tag would refer to that element:
<duty sailor-nick='Bluto'>...</duty>
IDREFSThe value of an IDREFS
attribute must contain one or more
ID references separated by
spaces. Example:
<roster sailor-nicks='Bluto Popeye Olive_Oyl'/>
ENTITYUse this attribute type to refer to external, non-parsed entities. See the section on notations, below.
ENTITIESLike ENTITY, but
the attribute can be a list of one or more
entity names separated by spaces.
NMTOKENThe attribute value must be a name token, conforming to the rules for XML names (see above).
NMTOKENSLike NMTOKEN, but the
attribute value can contain one or more name
tokens separated by spaces.