Declaring XML Attributes in DTD Using ATTLIST

In XML Document Type Definitions (DTD), attributes define additional properties and metadata for elements. The <!ATTLIST> declaration specifies which attributes belong to which elements, what data types those attributes accept, and how their default values or presence requirements are handled. This article covers the syntax, data types, default value rules, and examples for declaring attributes using ATTLIST.


Basic Syntax of ATTLIST

An attribute list declaration defines the element associated with the attribute, the name of the attribute, its data type, and its default behavior.

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

You can declare multiple attributes for the same element within a single <!ATTLIST> block:

<!ATTLIST element-name
    attribute1-name attribute1-type default-value
    attribute2-name attribute2-type default-value>

Attribute Types in DTD

DTD supports several attribute types to constrain the data an attribute can hold.

1. CDATA (Character Data)

CDATA specifies that the attribute contains unparsed text.

<!ATTLIST book title CDATA #REQUIRED>

2. Enumerated Types

An enumeration restricts the attribute value to a specific list of predefined options separated by the pipe (|) symbol.

<!ATTLIST payment method (credit | debit | cash) "credit">

3. ID and IDREF / IDREFS

<!ATTLIST employee id ID #REQUIRED>
<!ATTLIST project manager IDREF #REQUIRED>
<!ATTLIST project team IDREFS #IMPLIED>

4. NMTOKEN and NMTOKENS

<!ATTLIST product code NMTOKEN #REQUIRED>

5. ENTITY and ENTITIES

<!ATTLIST image source ENTITY #REQUIRED>

Default Value Declarations

The fourth component of the ATTLIST declaration specifies the requirement status and default value of the attribute.

Value Declaration Description Example
value Provides a default literal value if omitted in the XML instance. <!ATTLIST item status CDATA "pending">
#REQUIRED The attribute must be present on every instance of the element. <!ATTLIST user id ID #REQUIRED>
#IMPLIED The attribute is optional and has no default value. <!ATTLIST user nickname CDATA #IMPLIED>
#FIXED "value" The attribute is optional, but if specified, it must match the fixed value. <!ATTLIST document version CDATA #FIXED "1.0">

Complete Example

Below is a complete example showing a DTD declaration followed by a valid XML document:

DTD Definition

<!ELEMENT student (name)>
<!ELEMENT name (#PCDATA)>
<!ATTLIST student
    id ID #REQUIRED
    enrolled (true | false) "true"
    grade CDATA #IMPLIED
    institution CDATA #FIXED "State University">

Valid XML Document

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE student SYSTEM "student.dtd">
<student id="s101" grade="A">
    <name>Jane Doe</name>
</student>

In this XML document: * id is provided as required. * enrolled is omitted, so it automatically takes the default value "true". * grade is optional and provided as "A". * institution is omitted, so it defaults to the fixed value "State University".