Share via


ATTLIST (Attribute List)

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.

Syntax

<!ATTLIST  elementName  attributeName  dataType  default >

Parameters

  • elementName
    The name of the element to which the attribute list applies.
  • attributeName
    The name of an attribute. This parameter can be repeated as many times as needed to list all attributes available for use with elementName.
  • dataType
    The data type for the attribute named in the attributeName parameter, which must be one of the following:

    • CDATA – The attribute will contain only character data.

    • ID - The value of the attribute must be unique. It cannot be repeated in other elements or attributes used in the document.

    • IDREF – The attribute references the value of another attribute in the document, of ID type.

    • ENTITY – The attribute value must correspond to the name of an external unparsed ENTITY, which is also declared in the same DTD.

    • ENTITIES - The attribute value contains multiple names of external unparsed entities declared in the DTD.

    • NMTOKEN – The attribute value must be a name token. Name tokens allow character data values, but are more limited than CDATA. A name token can contain letters, numbers, and some punctuation symbols such as periods, dashes, underscores and colons. Name token values, however, cannot contain any spacing characters.

    • NMTOKENS - The attribute value contains multiple name tokens. See the description for NMTOKEN and Enumerated for more information.

    • Enumerated – The attribute values are limited to those within an enumerated list. Only values that match those listed are validly parsed. All enumerated data types are enclosed in a set of parentheses with each value separated by a vertical bar ("|").

  • default
    The default value for the attribute named in 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).

NoteNote

For each ATTLIST declaration made in the DTD, only one occurrence of the elementName needs to be used. The attributeName, dataType, and default 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 with elementName.

Example

This example declares the following for the <book> element:

  • An optional publisher attribute that can only contain character data.

  • A fixed reseller attribute with its value is set to "MyStore".

  • A required ISBN attribute that must contain a unique identifying value for each <book> element in the XML document.

  • A required 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"
>

See Also

Concepts

ELEMENT
ENTITY
NOTATION