Attribute definitions
As well as defining its elements, a valid document must define all the
elements' attributes. This is done with the following declaration:
<!ATTLIST element attribute TYPE DEFAULT>
where ATTLIST is the keyword for the attribute list definition, element is the name of the element containing the attributes, attribute is the name of the attribute to define, TYPE is the type of the attribute, and DEFAULT is a default value for the attribute. You can define many attributes for the same element.
These are the most relevant attribute types:
- CDATA
- The value of a CDATA attribute is any string of text. For instance, the following defines the CDATA born and died attributes for a person element:
<!ATTLIST person born CDATA #REQUIRED
died CDATA #IMPLIED>
- Enumeration
- An enumeration is a list of possible values for an attribute, separated by a vertical bar (|). For instance, the following defines the sex attribute for a person element. The values of the sex attribute can be either male or female:
<!ATTLIST person sex (male | female) #REQUIRED>
- ID
- The value of an ID attribute is an XML name (in particular it cannot start with a number) such that no other ID attribute (with any name, of any element) in the same document have the same value. ID attributes assign unique identifiers to elements and correspond to keys in relational databases.
- IDREF
- The value of an IDREF attribute is an XML name that corresponds to the value of some ID attribute in the document. IDREF attributes are references to elements identified by unique identifiers. IDREF attributes correspond to foreign keys in relational databases.
- IDREFS
- The value of an IDREFS attribute is a whitespace separated list of IDREF values.
As well as providing a data type, each attribute definition includes a default
definition for the attribute. The are four possibilities for this default:
- #REQUIRED
- The attribute is required and must be provided.
- #IMPLIED
- The attribute is optional and may or may not be provided.
- "value"
- The attribute has the default specified value. The attribute may or may not be provided. If it is included, then it may have any legal value. If it is not included, then the validator will supply one with the specified value. E.g., the following defines the married attribute for a person element whose values can be either yes or no and, by default, the value no is provided:
<!ATTLIST person married (yes | no) "no">
- #FIXED "value"
- The attribute has the constant and immutable specified value. The attribute may or may not be provided. If it is included, then it must have the specified value. If it is not included, then the validator will supply one with the specified value. This default is typically used to define default namespaces, as in the following example:
<!ATTLIST html xmlns CDATA #FIXED "http://www.w3.org/1999/xhtml">
In this case, you do not need to provide the default namespace attribute in the XML document. The validator will do it for you.
Notice that the validation process outputs an XML document that may be different from the input one. You can used xmllint with option --dtdattr to populate the XML document with inherited attributes. Recall that, as a general rule, web browsers do not validate documents. However, they may read the (internal) DTD and may supply attributes that have been defined in the DTD.