DTD - Attribute

In DTD, attributes are declared through the ATTLIST declaration.

Declare attribute

Attribute declaration uses the following syntax:

<!ATTLIST element_name attribute_name attribute_type default_value>

DTD instance:

<!ATTLIST payment type CDATA "check">

XML instance:

<payment type="check" />

The following areAttribute typeOptions:

Type Description
CDATA The value is character data (character data)
(en1|en2|..) This value is one of the values in an enumeration 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 This value is the name of a symbol
xml: The value is a predefined XML value

The default value parameter 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

Specify a default attribute value

DTD:

<!ELEMENT square EMPTY>
<!ATTLIST square width CDATA "0">

Valid XML:

<square width="100" />

In the above example, "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.

#IMPLIED

Syntax

<!ATTLIST element_name attribute_name attribute_type #IMPLIED>

Example

DTD:

<!ATTLIST contact fax CDATA #IMPLIED>

Valid XML:

<contact fax="555-667788" />

Valid XML:

<contact />

If you do not want to force the author to include the attribute and you do not have a default value option, please use the keyword #IMPLIED.

#REQUIRED

Syntax

<!ATTLIST element_name attribute_name attribute_type #REQUIRED>

Example

DTD:

<!ATTLIST person number CDATA #REQUIRED>

Valid XML:

<person number="5677" />

Invalid XML:

<person />

If you do not have a default value option but still want to force the author to submit the attribute, please use the keyword #REQUIRED.

#FIXED

Syntax

<!ATTLIST element_name attribute_name attribute_type #FIXED "value">

Example

DTD:

<!ATTLIST sender company CDATA #FIXED "Microsoft">

Valid XML:

<sender company="Microsoft" />

Invalid XML:

<sender company="W3School" />

If you want the attribute to have a fixed value and not allow the author to change this value, please use the #FIXED keyword. If the author uses a different value, the XML parser will return an error.

Listed Attribute Values

Syntax:

<!ATTLIST element_name attribute_name (en1|en2|..) default_value>

DTD Example:

<!ATTLIST payment type (check|cash) "cash">

XML Example:

<payment type="check" />

Or

<payment type="cash" />

If you want the attribute value to be one of a series of fixed valid values, please use listed attribute values.