Skip to content

DTD Attributes

In a Document Type Definition (DTD), attributes for elements are declared using an <!ATTLIST> declaration.

This declaration specifies the element the attribute belongs to, the attribute’s name, its type, and its default value or requirement.

Note: Defining attributes in DTDs is less flexible than using XML Schema (XSD), which offers richer data typing and namespace support. XSD is generally preferred for modern XML development.

The basic syntax for an attribute declaration is:

<!ATTLIST element-name attribute-name attribute-type attribute-default>

Example:

DTD declaration:
<!ATTLIST payment type CDATA "cash">
Corresponding XML element usage:
<payment type="check" />
<!-- 'type' attribute provided -->
<payment />
<!-- 'type' attribute omitted, defaults to "cash" -->

The attribute-type specifies the kind of value the attribute can hold. Common types include:

TypeDescription
CDATACharacter Data (a string of text)
(val1|val2|..)Enumerated: The value must be one from the specified list
IDA unique identifier within the XML document (must follow XML name rules)
IDREFA reference to an ID declared on another element
IDREFSA space-separated list of IDREFs
NMTOKENA valid XML name token (similar to names, but allows more characters)
NMTOKENSA space-separated list of NMTOKENs
ENTITYThe name of an unparsed external entity
ENTITIESA space-separated list of ENTITY names
NOTATIONThe name of a notation declared in the DTD

Note the lack of specific numeric, date, or boolean types, which is a major limitation compared to XSD.

The attribute-default specifies if an attribute is required and what its value is if omitted:

Default Value KeywordExplanation
”defaultValue”Specifies a default value if the attribute is not present in the XML tag.
#REQUIREDThe attribute must be explicitly included in the XML tag.
#IMPLIEDThe attribute is optional. If omitted, the application processing the XML decides how to handle its absence (no default value is supplied by the parser).
#FIXED “fixedValue”The attribute must either be omitted (in which case it takes the fixedValue) or included with exactly the specified fixedValue. Any other value causes a validation error.
DTD:
<!ELEMENT square EMPTY>
<!ATTLIST square width CDATA "0">
Valid XML:
<square width="100" />
<square /> <!-- width defaults to "0" -->
DTD:
<!ELEMENT person EMPTY>
<!ATTLIST person number ID #REQUIRED>
Valid XML:
<person number="p5677" />
Invalid XML:
<person /> <!-- missing required 'number' attribute -->
DTD:
<!ELEMENT contact EMPTY>
<!ATTLIST contact fax CDATA #IMPLIED>
Valid XML:
<contact fax="555-667788" />
<contact /> <!-- 'fax' is optional -->
DTD:
<!ELEMENT sender EMPTY>
<!ATTLIST sender company CDATA #FIXED "MyCorp">
Valid XML:
<sender company="MyCorp" />
<sender /> <!-- company implicitly has value "MyCorp" -->
Invalid XML:
<sender company="OtherCorp" /> <!-- value must be "MyCorp" -->
DTD:
<!ELEMENT payment EMPTY>
<!ATTLIST payment type (cash | check | card) #REQUIRED>
Valid XML:
<payment type="check" />
<payment type="cash" />
Invalid XML:
<payment type="invoice" /> <!-- 'invoice' is not in the allowed list -->
<payment /> <!-- 'type' is required -->

Enumerated types restrict the attribute value to one of the predefined options.