DTD Attributes
DTD - Attribute Declarations
Section titled “DTD - Attribute Declarations”In a Document Type Definition (DTD), attributes for elements are declared using an <!ATTLIST> declaration.
This declaration specifies the element the attribute belongs to, the attribute’s name, its type, and its default value or requirement.
Note: Defining attributes in DTDs is less flexible than using XML Schema (XSD), which offers richer data typing and namespace support. XSD is generally preferred for modern XML development.
Declaring Attributes
Section titled “Declaring Attributes”The basic syntax for an attribute declaration is:
<!ATTLIST element-name attribute-name attribute-type attribute-default>Example:
DTD declaration:<!ATTLIST payment type CDATA "cash">
Corresponding XML element usage:<payment type="check" /><!-- 'type' attribute provided -->
<payment /><!-- 'type' attribute omitted, defaults to "cash" -->Attribute Types in DTDs
Section titled “Attribute Types in DTDs”The attribute-type specifies the kind of value the attribute can hold. Common types include:
| Type | Description |
|---|---|
| CDATA | Character Data (a string of text) |
| (val1|val2|..) | Enumerated: The value must be one from the specified list |
| ID | A unique identifier within the XML document (must follow XML name rules) |
| IDREF | A reference to an ID declared on another element |
| IDREFS | A space-separated list of IDREFs |
| NMTOKEN | A valid XML name token (similar to names, but allows more characters) |
| NMTOKENS | A space-separated list of NMTOKENs |
| ENTITY | The name of an unparsed external entity |
| ENTITIES | A space-separated list of ENTITY names |
| NOTATION | The name of a notation declared in the DTD |
Note the lack of specific numeric, date, or boolean types, which is a major limitation compared to XSD.
Attribute Defaults and Requirements
Section titled “Attribute Defaults and Requirements”The attribute-default specifies if an attribute is required and what its value is if omitted:
| Default Value Keyword | Explanation |
|---|---|
| ”defaultValue” | Specifies a default value if the attribute is not present in the XML tag. |
| #REQUIRED | The attribute must be explicitly included in the XML tag. |
| #IMPLIED | The attribute is optional. If omitted, the application processing the XML decides how to handle its absence (no default value is supplied by the parser). |
| #FIXED “fixedValue” | The attribute must either be omitted (in which case it takes the fixedValue) or included with exactly the specified fixedValue. Any other value causes a validation error. |
Examples of Attribute Defaults
Section titled “Examples of Attribute Defaults”1. Default Value:
Section titled “1. Default Value:”DTD:<!ELEMENT square EMPTY><!ATTLIST square width CDATA "0">
Valid XML:<square width="100" /><square /> <!-- width defaults to "0" -->2. Required Attribute:
Section titled “2. Required Attribute:”DTD:<!ELEMENT person EMPTY><!ATTLIST person number ID #REQUIRED>
Valid XML:<person number="p5677" />
Invalid XML:<person /> <!-- missing required 'number' attribute -->3. Implied Attribute:
Section titled “3. Implied Attribute:”DTD:<!ELEMENT contact EMPTY><!ATTLIST contact fax CDATA #IMPLIED>
Valid XML:<contact fax="555-667788" /><contact /> <!-- 'fax' is optional -->4. Fixed Attribute:
Section titled “4. Fixed Attribute:”DTD:<!ELEMENT sender EMPTY><!ATTLIST sender company CDATA #FIXED "MyCorp">
Valid XML:<sender company="MyCorp" /><sender /> <!-- company implicitly has value "MyCorp" -->
Invalid XML:<sender company="OtherCorp" /> <!-- value must be "MyCorp" -->5. Enumerated Attribute Values:
Section titled “5. Enumerated Attribute Values:”DTD:<!ELEMENT payment EMPTY><!ATTLIST payment type (cash | check | card) #REQUIRED>
Valid XML:<payment type="check" /><payment type="cash" />
Invalid XML:<payment type="invoice" /> <!-- 'invoice' is not in the allowed list --><payment /> <!-- 'type' is required -->Enumerated types restrict the attribute value to one of the predefined options.