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
ID: Defines a unique identifier for the element within the XML document. The value must start with a letter, underscore, or colon, and cannot contain spaces.IDREF: References an existingIDvalue defined elsewhere in the document.IDREFS: References a space-separated list of multipleIDvalues.
<!ATTLIST employee id ID #REQUIRED>
<!ATTLIST project manager IDREF #REQUIRED>
<!ATTLIST project team IDREFS #IMPLIED>4. NMTOKEN and NMTOKENS
NMTOKEN: Restricts the attribute value to a valid XML name token (letters, numbers, hyphens, periods, underscores, and colons) without whitespace.NMTOKENS: Allows a space-separated list of valid name tokens.
<!ATTLIST product code NMTOKEN #REQUIRED>5. ENTITY and ENTITIES
ENTITY: Represents a reference to an external unparsed entity declared in the DTD.ENTITIES: Represents a list of external entities separated by spaces.
<!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".