DTD – Attributes

An ATTLIST declaration is used to declare the attributes in a DTD.

Declaring Attributes:

Syntax: Attribute declaration:

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

Example: DTD:

<!ATTLIST payment method CDATA "cash">

Example: XML:

<payment method="cash" />

Attribute-type:

We can choose any of the below for the attribute-type:

Type Uses
CDATA To specify that the value is character data.
(en1|en2|..) To specify that the value must be one from an enumerated list.
ID To specify that the value is a unique id.
IDREF To specify that the value is the id of another element.
IDREFS To specify that the value is a list of other ids.
NMTOKEN To specify that the value is a valid XML name.
NMTOKENS To specify that the value is a list of valid XML names.
ENTITY To specify that the value is an entity.
ENTITIES To specify that the value is a list of entities.
NOTATION To specify that the value is a name of a notation.
xml: To specify that the value is a predefined xml value.

Attribute-value:

We can choose any of the below for the attribute-value:

Value Uses
value To define the default value of the attribute.
#REQUIRED To specify that the attribute is required.
#IMPLIED To specify that the attribute is optional.
#FIXED value To specify that the attribute value is fixed.

Default Attribute Value:

DTD:

<!ELEMENT rectangle EMPTY>
<!ATTLIST rectangle width CDATA "0">

Valid XML:

<rectangle width="150" />

Explanation:

In the above example, we are defining the “rectangle” element as an empty element with a “width” attribute of type CDATA. It has a default value of 0, which is applicable if no width is specified.

#REQUIRED:

In case we don’t have an option for a default value, but still want to force the attribute to be present, we can use the #REQUIRED keyword.

Syntax:

<!ATTLIST element-name attribute-name attribute-type #REQUIRED>

Example:

DTD:

<!ATTLIST student marks CDATA #REQUIRED>

Valid XML:

<student marks="100" />

Invalid XML:

<student />

#IMPLIED:

In case, we don’t want to force the author to include an attribute, and we also don’t have an option for a default value, we can use the #IMPLIED keyword.

Syntax:

<!ATTLIST element-name attribute-name attribute-type #IMPLIED>

Example:

DTD:

<!ATTLIST employee telephone CDATA #IMPLIED>

Valid XML:

<employee telephone="111111111" />

Invalid XML:

<employee />

#FIXED:

In case, we want an attribute to have a fixed value without allowing the author to change it, we can use the #FIXED keyword. When another value is included by the author, an error will be returned by the XML parser.

Syntax:

<!ATTLIST element-name attribute-name attribute-type #FIXED "value">

Example:

DTD:

<!ATTLIST client company CDATA #FIXED "ABC">

Valid XML:

<client company="ABC" />

Invalid XML:

<client company="XYZ" />

Enumerated Attribute Values:

In case, we want the attribute value to be one of a fixed set of legal values, we can use the enumerated attribute values.

Syntax:

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

Example:

DTD:

<!ATTLIST payment method (online|cash) "cash">

XML example:

<payment method="online" />

or

<payment method="cash" />
Please Share