DTD – Attributes

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

Declaring Attributes:

Syntax: Attribute declaration:


Example: DTD:


Example: XML:


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:



Valid XML:


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:


Example:

DTD:


Valid XML:


Invalid XML:


#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:


Example:

DTD:


Valid XML:


Invalid XML:


#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:


Example:

DTD:


Valid XML:


Invalid XML:


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:


Example:

DTD:


XML example:


or