DTD - DTD Attribute Defaults: #REQUIRED, #IMPLIED, #FIXED, and Default Values
In a Document Type Definition (DTD), attributes provide additional information about XML elements. For example, an XML element such as <student> can have attributes like id, name, or course. DTD allows you to define how these attributes should behave. One important part of an attribute declaration is its default declaration, which determines whether an attribute is mandatory, optional, automatically assigned a value, or restricted to one fixed value.
The general syntax for declaring an attribute in DTD is:
<!ATTLIST element-name
attribute-name attribute-type default-declaration
>
For example:
<!ATTLIST student
id ID #REQUIRED
>
Here, student is the element, id is the attribute, ID is the attribute type, and #REQUIRED specifies that the attribute must be present.
1. #REQUIRED
The #REQUIRED keyword means that the attribute must be provided whenever the element appears in the XML document. If the attribute is missing, the XML document will fail DTD validation.
Example:
<!ELEMENT student EMPTY>
<!ATTLIST student
id ID #REQUIRED
>
A valid XML document would be:
<student id="S101"/>
The following document would be invalid:
<student/>
The reason is that the DTD requires every student element to have an id attribute.
#REQUIRED is useful when an attribute contains essential information that the XML document cannot function correctly without.
For example:
<!ATTLIST employee
employeeID ID #REQUIRED
>
Here, every employee must have an employeeID.
2. #IMPLIED
The #IMPLIED keyword means that the attribute is optional. The XML document can contain the attribute, but it is not required.
Example:
<!ELEMENT student EMPTY>
<!ATTLIST student
email CDATA #IMPLIED
>
Both of the following are valid:
<student email="[email protected]"/>
and:
<student/>
The DTD does not require the email attribute.
#IMPLIED is useful when an attribute provides additional information but is not essential.
For example:
<!ATTLIST book
title CDATA #REQUIRED
isbn CDATA #IMPLIED
>
Here, title must be provided, while isbn is optional.
A valid document could be:
<book title="XML Fundamentals" isbn="9781234567890"/>
Another valid document could be:
<book title="XML Fundamentals"/>
3. Default Values
DTD also allows you to specify a default value for an attribute.
The syntax is:
<!ATTLIST element-name
attribute-name attribute-type "default-value"
>
For example:
<!ELEMENT student EMPTY>
<!ATTLIST student
status CDATA "active"
>
If the XML document contains:
<student/>
the attribute has the default value:
status = "active"
The attribute can also be explicitly supplied:
<student status="inactive"/>
In this case, the supplied value replaces the default value.
Another example is:
<!ATTLIST product
category CDATA "general"
>
If the XML contains:
<product/>
the default category is general.
If the XML contains:
<product category="electronics"/>
the value is electronics instead.
Default values are useful when most elements normally use the same value but the XML author should still have the ability to specify a different value.
4. #FIXED
The #FIXED keyword means that an attribute has one fixed value. The XML document does not have the freedom to assign a different value to that attribute.
The syntax is:
<!ATTLIST element-name
attribute-name attribute-type #FIXED "value"
>
For example:
<!ELEMENT company EMPTY>
<!ATTLIST company
country CDATA #FIXED "India"
>
A valid XML element is:
<company country="India"/>
The attribute may also be omitted:
<company/>
In that case, the fixed value defined by the DTD applies.
However, this would be invalid:
<company country="USA"/>
because the DTD specifies that the value must be India.
#FIXED is useful when a particular attribute must always have the same value.
For example:
<!ATTLIST document
version CDATA #FIXED "1.0"
>
This ensures that the document's version attribute cannot be assigned a different value.
5. Difference Between #REQUIRED, #IMPLIED, Default Value, and #FIXED
These four options have different purposes.
| Declaration | Attribute Required? | Can XML Provide a Value? | Automatic Value? | Can the Value Change? |
|---|---|---|---|---|
#REQUIRED |
Yes | Yes | No | Yes |
#IMPLIED |
No | Yes | No | Yes |
"default" |
No | Yes | Yes | Yes |
#FIXED "value" |
No | Yes | Yes | No |
Consider the following DTD:
<!ELEMENT student EMPTY>
<!ATTLIST student
id ID #REQUIRED
email CDATA #IMPLIED
status CDATA "active"
country CDATA #FIXED "India"
>
The XML could be:
<student id="S101" email="[email protected]"
status="inactive" country="India"/>
This is valid because:
-
idis supplied as required. -
emailis optional and supplied. -
statushas a default value ofactive, but the XML providesinactive, which is allowed. -
countryis fixed asIndia.
This is also valid:
<student id="S102"/>
Here:
-
idis provided. -
emailis omitted because it is optional. -
statususes its default valueactive. -
countryuses its fixed valueIndia.
However:
<student/>
is invalid because id is declared as #REQUIRED.
Similarly:
<student id="S103" country="USA"/>
is invalid because country is declared as:
#FIXED "India"
6. Combining Attribute Types with Default Declarations
The default declaration works together with the attribute type.
For example:
<!ATTLIST employee
id ID #REQUIRED
name CDATA #REQUIRED
active (yes|no) "yes"
>
Here:
-
idmust contain a unique XML ID. -
namecan contain character data and is mandatory. -
activecan have onlyyesorno. -
If
activeis omitted, its default value isyes.
Valid XML:
<employee id="E101" name="John"/>
The effective value of active is yes.
Another valid example:
<employee id="E102" name="David" active="no"/>
The effective value of active is no.
But this is invalid:
<employee id="E103" name="Robert" active="maybe"/>
because maybe is not one of the permitted values.
7. Why Default Declarations Are Important
Default declarations help maintain consistency in XML documents. Instead of forcing the XML author to repeatedly specify common values, the DTD can define appropriate behavior.
For example:
<!ATTLIST book
language CDATA "English"
>
Instead of writing:
<book language="English"/>
<book language="English"/>
<book language="English"/>
the XML can simply contain:
<book/>
<book/>
<book/>
The DTD establishes English as the default value.
On the other hand, #REQUIRED is useful for information that must always be supplied:
<!ATTLIST book
isbn CDATA #REQUIRED
>
#IMPLIED is appropriate for optional information:
<!ATTLIST book
description CDATA #IMPLIED
>
And #FIXED is appropriate when a value must remain constant:
<!ATTLIST book
format CDATA #FIXED "XML"
>
8. Important Difference Between Default and #FIXED
A common point of confusion is the difference between a normal default value and a fixed value.
Consider:
<!ATTLIST book
language CDATA "English"
>
The XML can contain:
<book language="French"/>
This is valid because English is only the default value.
Now consider:
<!ATTLIST book
language CDATA #FIXED "English"
>
The following is invalid:
<book language="French"/>
because the value is fixed as English.
Therefore:
Default value: Used when no value is supplied, but the XML author can override it.
#FIXED: Used when a particular value must always be maintained.
9. Practical Example
Consider a library system:
<!ELEMENT book EMPTY>
<!ATTLIST book
bookID ID #REQUIRED
title CDATA #REQUIRED
author CDATA #IMPLIED
language CDATA "English"
format CDATA #FIXED "Printed"
>
A valid XML document could be:
<book bookID="B101"
title="XML Programming"
author="John Smith"
language="Kannada"
format="Printed"/>
Here:
-
bookIDis mandatory. -
titleis mandatory. -
authoris optional. -
languagehasEnglishas its default but can be changed. -
formatis permanently fixed toPrinted.
Another valid example is:
<book bookID="B102"
title="XML Basics"/>
The effective values are:
bookID = B102
title = XML Basics
author = not provided
language = English
format = Printed
This demonstrates how different attribute declarations can work together within the same DTD.
10. Summary
DTD attribute default declarations control how XML attributes behave when they are missing or supplied.
#REQUIRED means the attribute must be included in the XML document.
#IMPLIED means the attribute is optional and does not need to be included.
A quoted default value, such as "English", means the attribute is optional and receives that value when it is omitted, but the XML author can provide another valid value.
#FIXED "value" means the attribute has a constant value and cannot be changed to another value.
Understanding these four declarations is important when designing DTDs because they allow you to control mandatory information, optional information, automatic values, and fixed values in XML documents.