DTD - Attributes
In a DTD, attributes are declared with an ATTLIST declaration.
Declaring attributes
Attribute declarations use the following syntax:
DTD example:
XML example:
The following are Attribute typesoptions:
| Type | Description |
|---|---|
| CDATA | The value is character data |
| (en1|en2|..) | The value is one of 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 |
DefaultAttribute valuesThe following values can be used:
| 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
DTD:
Valid XML:
In the example above, "square" is defined as an empty element with a "width" attribute of type CDATA. If no width is set, its default value is 0.
#REQUIRED
Syntax
Example
DTD:
Valid XML:
Invalid XML:
If you have no default value option but still want to force authors to supply the attribute, use the keyword #REQUIRED.
#IMPLIED
Syntax
Example
DTD:
Valid XML:
Valid XML:
If you do not want to force authors to include the attribute and you have no default value option, use the keyword #IMPLIED.
#FIXED
Syntax
Example
DTD:
Valid XML:
Invalid XML:
If you want the attribute to have a fixed value and do not allow authors to change it, use the #FIXED keyword. If an author uses a different value, the XML parser returns an error.
Enumerated attribute values
Syntax
Example
DTD:
XML example:
or
If you want the attribute value to be one of a fixed set of legal values, use enumerated attribute values.