Skip to main content

XML DTD 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

TypeDescription
CDATAThe value is character data
(en1|en2|...)The value must be one from an enumerated list
IDThe value is a unique id
IDREFThe value is the id of another element
IDREFSThe value is a list of other ids
NMTOKENThe value is a valid XML name
NMTOKENSThe value is a list of valid XML names
ENTITYThe value is an entity
ENTITIESThe value is a list of entities
NOTATIONThe 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

ValueExplanation
valueThe default value of the attribute
#REQUIREDThe attribute is required
#IMPLIEDThe attribute is optional
#FIXED valueThe 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" />
note

If no value is specified for attribute-name, it has a default value of 0 (as defined before in ATTLIST).

#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:

DTD
<!ATTLIST car number CDATA #REQUIRED>
Valid XML
<car number="12345" />
Invalid XML
<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:

DTD
<!ATTLIST contact phone CDATA #IMPLIED>
Valid XML
<contact phone="555-667788" />
Also Valid XML
<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:

DTD
<!ATTLIST manufacturer company CDATA #FIXED "Microsoft">
Valid XML
<manufacturer company="Microsoft" />
Invalid XML
<manufacturer company="Tutorial Reference"/>