XML Element Attributes

To enforce the attribution properties of a tag, you must add an attribute-list-declaration to the document's DTD. This list accomplishes the following:

The general attribute syntax is:

<!ATTLIST element attribute1 type1 default1
                  attribute2 type2 default2
                  attribute3 type3 default3 ...>

Here element is the element name associated with the attribute, attribute is the attribute name, type is the attribute's data type, and default indicates whether the attribute is required and whether it has a default value. In practice, this declaration can be split into several single ones, each describing a specific attribute:

<!ATTLIST element attribute1 type1 default1>
<!ATTLIST element attribute2 type2 default2>
<!ATTLIST element attribute3 type3 default3>
...

Attribute types

XML supports 9 attribute types, we consider only 4 of them here:

TypeDescription
CDATA character data
enumerated list a list of possible attribute values
ID a unique text string
IDREF a reference to an ID value

CDATA allows an attribute to contain almost any data except some reserved symbols like <, >, &.

Example:
<!ATTLIST item itemPrice CDATA>
allows the following values:
<item itemPrice="29.95">

In some cases you'll want to restrict the set of possible attribute values. You can do it with the following syntax:

<!ATTLIST element attribute (value1 | value2 | ...) default>
Example:
<!ATTLIST customer custType (home | business) ... >

An ID token is used whenever the attribute value must be unique in the document. Ones an ID is defined, other attribute values can reference to it by using the IDREF token.

Attribute defaults

There are 4 possible values listed in the following table:

Value Description
#REQUIRED the attribute must appear with every occurrence of the element
#IMPLIED the attribute is optional
"default" the attribute is optional; if its value is not specified, the XML parser set it up with the default value
#FIXED default the attribute is optional; if its value is specified, it must match the default value
Examples:
<!ATTLIST img alt CDATA #REQUIRED>
<!ATTLIST html xmlns:mod CDATA #IMPLIED>
<!ATTLIST customer custType (home | business) "home">
<!ATTLIST frame noresize CDATA #FIXED "noresize">