XML DTD Attributes
Attributes in Document Type Definition (DTD) are defined with an ATTLIST
declaration.
Different types of attributes are shown below.
How to declare DTD Attributes
An attribute declaration has the following syntax:
<!ATTLIST element-name attribute-name attribute-type attribute-value>
where the attribute-type can be one of the following
Type | Description |
---|---|
CDATA | The value is character data |
(en1|en2|...) | The value must be 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 a name of a notation |
xml: | The value is a predefined xml value |
and the attribute-value can be one of the following
Value | Explanation |
---|---|
value | The default value of the attribute |
#REQUIRED | The attribute is required |
#IMPLIED | The attribute is optional |
#FIXED value | The attribute value is fixed |
Possible DTD Attribute values
Default Attribute Value
Example of element-name element defined to be an empty element with attribute-name attribute of type CDATA with default value of 0.
<!ELEMENT element-name EMPTY>
<!ATTLIST element-name attribute-name CDATA "0">
This corresponds to:
<element-name attribute-name="100" />
If no value is specified for attribute-name
, it has a default value of 0 (as defined before in ATTLIST).
#REQUIRED
Attribute attribute-name is required. The attribute is mandatory but you don't have a default value for it.
<!ATTLIST element-name attribute-name attribute-type #REQUIRED>
For example:
<!ATTLIST car number CDATA #REQUIRED>
<car number="12345" />
<car />
#IMPLIED
Attribute attribute-name is optional. It is used if you don't want to force to include an attribute and you don't have a default value.
<!ATTLIST element-name attribute-name attribute-type #IMPLIED>
For example:
<!ATTLIST contact phone CDATA #IMPLIED>
<contact phone="555-667788" />
<contact />
#FIXED
The attribute value of attribute-name attribute can't be changed (it is fixed). If an author includes another value, the XML parser will return an error.
<!ATTLIST element-name attribute-name attribute-type #FIXED "value">
For example:
<!ATTLIST manufacturer company CDATA #FIXED "Microsoft">
<manufacturer company="Microsoft" />
<manufacturer company="Tutorial Reference"/>