DTD - Attribute Default Values and the #FIXED Declaration in DTD
In an XML Document Type Definition (DTD), an attribute declaration specifies the attributes that an XML element is allowed to have, along with their data types and default behavior. XML attributes do not always have to be explicitly written in every element. A DTD can define a default value that an XML parser automatically provides when an attribute is missing. DTDs also provide the #FIXED declaration, which specifies that an attribute must have one particular value and cannot normally be changed by the XML document.
1. Attribute Default Values
A default value is used when an XML element does not explicitly provide a particular attribute. The general syntax is:
<!ATTLIST element-name attribute-name attribute-type "default-value">
For example:
<!ATTLIST student country CDATA "India">
This declaration says that the student element can have a country attribute of type CDATA. If the attribute is not provided, its value is considered to be "India".
The XML document can therefore contain:
<student>Rahul</student>
The parser treats the missing attribute as having the default value:
<student country="India">Rahul</student>
The important point is that the default value is supplied by the DTD when the attribute is absent.
2. Why Default Values Are Useful
Default values are useful when most elements share the same value for an attribute. Instead of repeatedly writing the same attribute value in every element, the DTD can specify it once.
For example:
<!ELEMENT book (#PCDATA)>
<!ATTLIST book language CDATA "English">
The XML document can contain:
<book>Introduction to XML</book>
<book language="Kannada">XML Programming</book>
For the first book, the default language is "English". For the second book, the explicitly specified value "Kannada" is used.
This reduces repetition and makes XML documents shorter and easier to maintain.
3. #REQUIRED
A related attribute declaration is #REQUIRED. It means that the attribute must be provided in the XML document.
Example:
<!ATTLIST student id ID #REQUIRED>
Here, every student element must contain an id attribute.
Valid XML:
<student id="S101">Rahul</student>
Invalid according to the DTD:
<student>Rahul</student>
Unlike a normal default value, #REQUIRED does not provide a value automatically. It requires the document author to supply one.
4. #IMPLIED
#IMPLIED indicates that an attribute is optional and has no default value.
Example:
<!ATTLIST student email CDATA #IMPLIED>
The following is valid:
<student>Rahul</student>
The following is also valid:
<student email="[email protected]">Rahul</student>
The difference is that the DTD does not provide a value when the attribute is omitted.
Therefore:
-
#REQUIREDmeans the attribute must be present. -
#IMPLIEDmeans the attribute is optional. -
"value"means the attribute has a default value. -
#FIXED "value"means the attribute has a fixed value.
5. The #FIXED Declaration
The #FIXED keyword is used when an attribute must have a specific value.
The syntax is:
<!ATTLIST element-name attribute-name attribute-type #FIXED "value">
For example:
<!ATTLIST company country CDATA #FIXED "India">
This declaration specifies that the country attribute has the fixed value "India".
The XML element can omit the attribute:
<company>ABC Technologies</company>
The default fixed value is then "India".
It can also explicitly specify the same value:
<company country="India">ABC Technologies</company>
However, specifying a different value is not allowed:
<company country="USA">ABC Technologies</company>
This violates the DTD because the attribute has been declared as fixed to "India".
6. Difference Between a Default Value and #FIXED
The most important distinction is that a normal default value can be overridden, while a fixed value cannot normally be changed.
Consider:
<!ATTLIST employee department CDATA "Sales">
An employee can omit the attribute:
<employee>John</employee>
The default is "Sales".
But the document can also specify another value:
<employee department="Finance">John</employee>
This is valid because "Sales" is only a default.
Now consider:
<!ATTLIST employee department CDATA #FIXED "Sales">
This allows:
<employee>John</employee>
and:
<employee department="Sales">John</employee>
But this is invalid:
<employee department="Finance">John</employee>
because "Sales" is a fixed value.
7. Complete Example
Consider the following DTD:
<!ELEMENT product (#PCDATA)>
<!ATTLIST product
category CDATA "General"
country CDATA #FIXED "India"
code ID #REQUIRED
description CDATA #IMPLIED
>
A valid XML document could be:
<product code="P101">
Laptop
</product>
Here:
-
categoryreceives the default value"General". -
countryreceives the fixed value"India". -
codemust be provided because it is#REQUIRED. -
descriptionis optional because it is#IMPLIED.
Another valid example is:
<product
code="P102"
category="Electronics"
country="India"
description="Portable computer">
Laptop
</product>
The category value can be changed because it has a normal default value. The country value must remain "India" because it is declared with #FIXED.
8. Comparison of Attribute Declarations
| Declaration | Attribute Required? | Automatic Value? | Can the Value Be Changed? |
|---|---|---|---|
"India" |
No | Yes | Yes |
#REQUIRED |
Yes | No | Yes |
#IMPLIED |
No | No | Yes |
#FIXED "India" |
No | Yes | No |
This distinction is important when designing a DTD because it determines how much control the DTD has over attribute values.
9. Practical Applications
Default values are useful for attributes that have a commonly used value. For example:
<!ATTLIST document format CDATA "digital">
Most documents can use the default format without explicitly specifying it.
#FIXED is useful when an XML vocabulary requires a value to remain constant. For example:
<!ATTLIST organization country CDATA #FIXED "India">
This can ensure that every document using this DTD represents an organization associated with the same fixed country value.
10. Key Points to Remember
Attribute declarations in DTDs provide rules for how attributes should behave. A normal default value is automatically supplied when an attribute is missing, but the XML document can override that default. #REQUIRED makes an attribute mandatory, while #IMPLIED makes it optional without providing a value. The #FIXED declaration provides a default value while also restricting the attribute to that particular value.
The central difference can be remembered as:
Default value → Used when no value is supplied, but can be overridden.
#FIXED value → Used when no value is supplied and cannot normally be overridden.
Understanding these declarations is essential for creating precise DTDs because they allow the document designer to control which attributes are mandatory, optional, automatically populated, or restricted to a specific value.