XML DTD Attributes
Elements hold data. Attributes provide extra information about that data. In a DTD, you declare attributes separately from elements using the ATTLIST declaration. This tells the parser which attributes an element is allowed to have, what type of value each attribute accepts, and whether the attribute is required.
ATTLIST Declaration Syntax
<!ATTLIST element-name attribute-name attribute-type default-value >
An element can have multiple attributes, each declared on its own line inside one ATTLIST block.
A Simple Example
A product in a shopping catalog needs an ID and a category attribute:
<!ATTLIST product id ID #REQUIRED category CDATA #IMPLIED >
Valid XML
<product id="P001" category="electronics"> <name>Laptop</name> </product>
Attribute Types
1. CDATA
CDATA means the attribute accepts any text string. It is the most flexible and most commonly used attribute type.
<!ATTLIST item description CDATA #IMPLIED>
2. ID
An ID attribute holds a unique identifier. No two elements in the same document can have the same ID value. The value must start with a letter or underscore — not a number.
<!ATTLIST employee empID ID #REQUIRED>
Valid
<employee empID="E101"></employee>
Invalid — duplicate ID
<employee empID="E101"></employee> <employee empID="E101"></employee>
3. IDREF and IDREFS
IDREF links one element to another using that element's ID value. IDREFS holds multiple space-separated ID references.
<!ATTLIST order customerRef IDREF #REQUIRED> <!ATTLIST project memberRefs IDREFS #IMPLIED>
Example
<customer empID="C001">Sunita</customer> <order customerRef="C001">Book</order>
4. Enumeration
An enumeration restricts the attribute to a list of allowed values. Only those exact values are valid.
<!ATTLIST ticket priority (low | medium | high) "medium">
Valid
<ticket priority="high">Fix login bug</ticket>
Invalid
<ticket priority="urgent">Fix login bug</ticket>
5. NMTOKEN and NMTOKENS
NMTOKEN accepts a name token — a string that uses only letters, digits, periods, hyphens, underscores, and colons. NMTOKENS accepts multiple space-separated NMTOKEN values.
<!ATTLIST file format NMTOKEN #REQUIRED>
6. NOTATION
NOTATION refers to a format declared elsewhere in the DTD. It is used for non-XML data like images or binary content.
Default Value Types
| Default Type | Meaning |
|---|---|
| #REQUIRED | The attribute must always be present |
| #IMPLIED | The attribute is optional — no default value |
| #FIXED "value" | The attribute always has this fixed value |
| "default" | The attribute is optional; uses this value if missing |
#REQUIRED Example
<!ATTLIST employee name CDATA #REQUIRED> <!-- The name attribute must appear on every employee element -->
#IMPLIED Example
<!ATTLIST book subtitle CDATA #IMPLIED> <!-- subtitle is optional — no error if it is missing -->
#FIXED Example
<!ATTLIST document language CDATA #FIXED "en"> <!-- language must always be "en" — parser rejects any other value -->
Default Value Example
<!ATTLIST ticket status CDATA "open"> <!-- If status is missing, the parser treats it as "open" -->
Complete DTD Example with Attributes
A flight booking system:
<!DOCTYPE flights [
<!ELEMENT flights (flight+)>
<!ELEMENT flight (departure, arrival, airline)>
<!ATTLIST flight
flightID ID #REQUIRED
status (scheduled | delayed | cancelled) "scheduled"
class (economy | business | first) "economy"
>
<!ELEMENT departure (#PCDATA)>
<!ELEMENT arrival (#PCDATA)>
<!ELEMENT airline (#PCDATA)>
]>
Valid XML
<flights>
<flight flightID="AI202" status="scheduled" class="business">
<departure>Delhi</departure>
<arrival>London</arrival>
<airline>Air India</airline>
</flight>
<flight flightID="AI305">
<departure>Mumbai</departure>
<arrival>Dubai</arrival>
<airline>Air India</airline>
</flight>
</flights>
Attribute Declaration Diagram
<!ATTLIST flight
| |
| +-- element that owns these attributes
+---------- DTD keyword for attribute list
flightID ID #REQUIRED
| | |
| | +-- must be present — no default
| +-------- must be unique across document
+---------------- attribute name
status (scheduled | delayed | cancelled) "scheduled"
| | |
| | +-- default if missing
| +------------------------------------- allowed values only
+------------------------------------------- attribute name
Key Points to Remember
- ATTLIST declares attributes for a specific element in the DTD.
- CDATA is the most flexible type — it accepts any text.
- ID values must be unique across the entire document.
- Enumeration restricts the attribute to a predefined list of values.
- #REQUIRED means the attribute must always appear.
- #IMPLIED means the attribute is optional.
- #FIXED sets a permanent value that cannot be changed.
- A default value applies when the attribute is absent from an element.
