Skip to content

XML DTD Attributes

How to declare and use XML DTD attributes with types like CDATA, ID, IDREF, and more, including default values, required (#REQUIRED), optional (#IMPLIED), and fixed (#FIXED) attributes.

Attributes in Document Type Definition (DTD) are defined with an ATTLIST declaration.

Different types of attributes are shown below.

How to declare DTD Attributes

An attribute declaration has the following syntax:

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

where the attribute-type can be one of the following

Type Description
CDATA The value is character data
(en1|en2|...) The value must be one from an enumerated list
ID The value is a unique id
IDREF The value is the id of another element
IDREFS The value is a list of other ids
NMTOKEN The value is a valid XML name
NMTOKENS The value is a list of valid XML names
ENTITY The value is an entity
ENTITIES The value is a list of entities
NOTATION The value is a name of a notation
xml: The value is a predefined xml value

and the attribute-value can be one of the following

Value Explanation
value The default value of the attribute
#REQUIRED The attribute is required
#IMPLIED The attribute is optional
#FIXED value The attribute value is fixed

Possible DTD Attribute values

Default Attribute Value

Example of element-name element defined to be an empty element with attribute-name attribute of type CDATA with default value of 0.

<!ELEMENT element-name EMPTY>  
<!ATTLIST element-name attribute-name CDATA "0">

This corresponds to:

<element-name attribute-name="100" />

#REQUIRED

Attribute attribute-name is required. The attribute is mandatory but you don't have a default value for it.

<!ATTLIST element-name attribute-name attribute-type #REQUIRED>

For example:

DTDxml
<!ATTLIST car number CDATA #REQUIRED>
Valid XMLxml
<car number="12345" />
Invalid XMLxml
<car />

#IMPLIED

Attribute attribute-name is optional. It is used if you don't want to force to include an attribute and you don't have a default value.

<!ATTLIST element-name attribute-name attribute-type #IMPLIED>

For example:

DTDxml
<!ATTLIST contact phone CDATA #IMPLIED>
Valid XMLxml
<contact phone="555-667788" />
Also Valid XMLxml
<contact />

#FIXED

The attribute value of attribute-name attribute can't be changed (it is fixed). If an author includes another value, the XML parser will return an error.

<!ATTLIST element-name attribute-name attribute-type #FIXED "value">

For example:

DTDxml
<!ATTLIST manufacturer company CDATA #FIXED "Microsoft">
Valid XMLxml
<manufacturer company="Microsoft" />
Invalid XMLxml
<manufacturer company="Tutorial Reference"/>
Navigation