For attribute declaration, the list of allowable attributes for each element is declared in DTD.

Syntax for attributes declaration in XML

<!ATTLIST  elementName  attributeName  attributeType  attributeValue>

Where,

  • ATTLIST is the keyword.
  • elementName - Element for which attribute is declared.
  • attributeName - Name of attribute
  • attributeType - Type of attribute. Attributes can be of following types:

Attributes declaration in XML

Attribute Type

Description

CDATA

Attribute is character data

(enum1| enum2…)

Attribute value is one from the list

ID

Attribute value is a unique ID for the whole document

IDREF

Attribute value is the id of another element

IDREFS

Attribute value is list of other ids

NMTOKEN

Attribute value is a valid XML name, which does not allow whitespaces and few punctuation marks

NMTOKENS

Attribute value is a list of valid XML names

ENTITY

Attribute value is an entity

ENTITIES

Attribute value is list of entities

NOTATION

Attribute value is name of a notation, i.e. non xml tag.

xml:

Attribute value is a predefined xml value.

Basic examples with attributes declaration in XML

<!ATTLIST from employee ID #FIXED>

<!ATTLIST picture image ENTITY #IMPLIED>

The attribute value can be any of the following:

Attributes values in XML

Attribute Value

Description

value

Default value of attribute. If no attribute value is given this value is set to attribute

#REQUIRED

Mandatory attribute

#IMPLIED

Optional attribute. This is given if there is no default value and attribute is not mandatory.

#FIXED value

Value is fixed. It cannot be changed by the author of the xml document.

Complex example of working with attributes in XML

 

›› go to examples ››