The ATTLIST
statement is used to list and declare each attribute that can belong to an element. It first specifies the name of the element (or elements) for which the attribute list will apply. It then lists each attribute by name, indicates whether it is required, and specifies what character data it is allowed to have as a value.
<!ATTLIST elementName attributeName dataType default >
elementName
.attributeName
parameter, which must be one of the following:
attributeName
. The following table describes the possible defaults.
Defaults | Description |
---|---|
#REQUIRED | The attribute must appear in the XML document or a parsing error will result. To avoid a parse error in some cases, you can optionally use the defaultValue field directly following this keyword. |
#IMPLIED | The attribute can appear in the XML document, but if omitted, no parsing error will result. Optionally, in some cases you can also use the defaultValue field directly following this keyword. |
#FIXED | The attribute value is fixed in the DTD and cannot be changed or overridden in the XML document. If this keyword is used, the defaultValue field directly following this keyword must also be used to declare the fixed attribute value. |
defaultValue | A default or fixed value. The parser inserts this value into the XML document when the attribute is missing or is not used in the XML document. All values must be enclosed in a set of quotation marks (either single or double quotes). |
Note For eachATTLIST
declaration made in the DTD, only one occurrence of theelementName
needs to be used. TheattributeName
,dataType
, anddefault
parameters define each attribute in the list and can be repeated as many times as needed until you have listed and defined all attributes available for use withelementName
.
This example declares the following for the <book>
element:
publisher
attribute that can only contain character data.reseller
attribute with its value is set to "MyStore".
ISBN
attribute that must contain a unique identifying value for each <book>
element in the XML document.InPrint
attribute that must contain either a "yes"
or "no"
value. The default enforces a "yes"
value when the value is not explicitly set in the XML document.<!ATTLIST book publisher CDATA #IMPLIED reseller CDATA #FIXED "MyStore" ISBN ID #REQUIRED inPrint (yes|no) "yes" >