DTD - Attribute
In DTD, attributes are declared using the ATTLIST declaration.
Declaring Attributes
Attribute declarations use the following syntax:
Here are the options for attribute types:
| Type | Description | ||
|---|---|---|---|
| CDATA | The value is character data | ||
| (en1 | en2 | ..) | The value is one from an enumerated list |
| ID | The value is a unique id | ||
| IDREF | The value is the id of another element | ||
| IDREFS | The value is a list of other ids | ||
| NMTOKEN | The value is a valid XML name | ||
| NMTOKENS | The value is a list of valid XML names | ||
| ENTITY | The value is an entity | ||
| ENTITIES | The value is a list of entities | ||
| NOTATION | The value is the name of a notation | ||
| xml: | The value is a predefined XML value |
The default attribute values can use the following values:
| Value | Explanation |
|---|---|
| value | The default value of the attribute |
| #REQUIRED | The attribute value is required |
| #IMPLIED | The attribute is not required |
| #FIXED value | The attribute value is fixed |
Default Attribute Value
In the example above, "square" is defined as an empty element with a "width" attribute of type CDATA. If the width is not set, its default value is 0.
#REQUIRED
Syntax
Example
If you do not have a default value option but still want to force authors to submit the attribute, use the keyword #REQUIRED.
#IMPLIED
Syntax
Example
If you do not want to force authors to include the attribute and you do not have a default value option, use the keyword #IMPLIED.
#FIXED
Syntax
Example
If you want the attribute to have a fixed value and not allow authors to change it, use the #FIXED keyword. If authors use a different value, the XML parser will return an error.
Enumerated Attribute Values
Syntax
Example
If you want the attribute value to be one of a fixed set of valid values, use enumerated attribute values.