Web Development Tutorials


DTD Attributes

DTD Attributes

XML elements can contain attributes. An XML attribute is used to provide additional information about elements. In order to define the legal attributes that an XML element can contain, the DTD ATTLIST declaration is used.

In a DTD, attributes are declared using the following syntax:

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

Listing 2-4. DTD attribute declaration.

element-name refers to the name of the XML element, attribute-name refers to the name of the attribute, attribute-type refers to the type of attribute used to provide more information about the element, and default-value is the default value assigned to the attribute when a value is not specified. An example is shown below:

<?xml version="1.0"?>

    <Employee id="1111">

Listing 2-5. DTD attribute syntax.

The XML file above includes an Employee element. The Employee element contains an attribute "id" to assign each employee a unique employee number. The DTD below is used to validate this document:

<!ELEMENT Employee (SSN,FirstName,LastName,Salary,Department)>
<!ELEMENT FirstName (#PCDATA)>
<!ELEMENT Department(#PCDATA)>

<!ATTLIST Employee id CDATA>

Listing 2-6. DTD validation.

The ATTLIST declartion is coded below the ELEMENT declarations. Here the declaration states that the Employee element will contain an attribute named id. The attribute value will be of type CDATA (character data). No default value is specified. The most common attribute types are outlined below:

Type Description


The value is character data. CDATA is text that will not be parsed by a parser. Tags inside the text will NOT be treated as markup and entities will not be expanded.


The value must be one of the items listed


The value is a unique id


The value is an entity

The default-value can be one of the following:

Value Explanation


The default value of the attribute


The attribute is required. Use the #REQUIRED keyword if you don't have an option for a default value, but still want to force the attribute to be present.


The attribute is not required. Use the #IMPLIED keyword if you don't want to force the author to include an attribute, and you don't have an option for a default value.

#FIXED value

The attribute value is fixed. Use the #FIXED keyword when you want an attribute to have a fixed value without allowing the author to change it. If an author includes another value, the XML parser will return an error

The example below declares id as a required attribute for the Employee element.

<!ELEMENT Employee (SSN,FirstName,LastName,Salary,Department)>
<!ELEMENT FirstName (#PCDATA)>
<!ELEMENT Department(#PCDATA)>


Listing 2-7. id attribute.

The XML document containing the DTD above, must include the element Employee with an attribute id and the attribute must contain a value of type CDATA. If the XML document does not meet these requirements, it will not be considered a valid document.

TOP | NEXT: Entities