DTD Attributes IN XML

In the DTD, XML element attributes are defined with an ATTLIST declaration.
 
An attribute declaration has the following syntax:
 
<!ATTLIST element-name attribute-name attribute-type default-value>
 
Tthe ATTLIST declaration defines the element which can have the attribute, attribute-name is the name of the attribute, attribute-type is the type of the attribute, and the default attribute value.
 
The attribute-type can have the following values:
 
Value Explanation
xml: The value is predefined
ID The value is an unique id
IDREF The value is the id of another element
IDREFS The value is a list of other ids
CDATA The value is character data
NMTOKEN The value is a valid XML name
NMTOKENS The value is a list of valid XML names
ENTITY The value is an entity
ENTITIES The value is a list of entities
NOTATION The value is a name of a notation
(eval|eval|..) The value must be an enumerated value
 
 
The default-value can have the following values:
 
Value Explanation
#DEFAULT value The attribute has a default value
#REQUIRED The attribute value must be included in the element
#IMPLIED The attribute does not have to be included
#FIXED value The attribute value is fixed
 
Attribute declaration example:
 
<?xml version=”1.0″ encoding=”ISO-8859-1″?>
<!DOCTYPE email [
  <!ELEMENT email (sender,recipient,subject,message)>
   <!ATTLIST email priority CDATA “normal”>
]>
<email priority=’high’>
</email>
 
In the above example, the element email is defined to be an empty element with the attributes propriety of type CDATA. The priority attribute has a default value of “Normal”.
 
Click here to view the example.
 
Enumerated attribute values
 
Syntax:
<!ATTLIST element-name attribute-name (eval|eval|..) default-value>
 
DTD example:
<!ATTLIST payment mode (check|cash) “cash”>  
 
XML example:
<payment mode=”check”>
or
<payment mode=”cash”> 
 
Use enumerated attribute values when you want the attribute values to be one of a fixed set of legal values.
 
 
Fixed attribute value
 
<?xml version=”1.0″ encoding=”UTF-7″?>
<!DOCTYPE ebiz [
   <!ELEMENT ebiz (associate_details,emp_details,sponser_details)>
   <!ATTLIST ebiz companysite CDATA #FIXED “http://www.ebizel.com”>
]>
<ebiz>
</ebiz>
 
Use a fixed attribute value when you want an attribute to have a fixed value that the author can not change. If an author includes another value, the XML parser will return an error.
 
 
Required attribute
 
<!ELEMENT payment (amount+,duration+)>
     <!ATTLIST payment mode CDATA #REQUIRED>
 
XML example:
<payment mode=”cash”></payment>
 
Use a required attribute if you don’t have an option for a default value, but still want to force the attribute to be present. The required (#REQUIRED) attribute force the document author to use the attribute.
 
 
Implied attribute
 
<!ELEMENT addressdetails (address+,city?,state,pin)>
     <!ATTLIST addressdetails communication_mode CDATA #IMPLIED>
      <addressdetails communication_mode=”post”>
      </addressdetails>
 
Use an implied attribute if you don’t want to force the author to include an attribute and you don’t have an option for a default value either.
Scroll to Top