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: | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
The default-value can have the following values: | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
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. |