DTD - Property


In DTD, attributes are declared through ATTLIST declarations.


declare property

Attribute declarations use the following syntax:

<!ATTLIST element-name attribute-name attribute-type attribute-value>

DTD example:

<!ATTLIST payment type CDATA "check">

XML example:

<payment type="check" />

The following areproperty typeoptions for:

Type Description
CDATA The value is character data
(en1|en2|..) This value is one of the values in an enumerated list
ID The value is a unique id
IDREF The value is an id of another element
IDREFS The value is a list of other ids
NMTOKEN The value is a valid XML name
NMTOKENS A list of legal 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

DefaultProperty valueThe following values can be used:

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


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.


#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 don't have a default value option, but still want to force authors to submit the attribute, use the keyword #REQUIRED.


#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 don't want to force authors to include the attribute, and you don't have a default value option, use the keyword #IMPLIED.


#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="W3Schools" />

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


List attribute values

Syntax

<!ATTLIST element-name attribute-name (en1|en2|..) default-value>

Example

DTD:
<!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 legal values, use enumerated attribute values.

other extensions