XML DTD Attributes: REQUIRED vs IMPLIED vs FIXED

In XML Document Type Definitions (DTDs), attribute default declarations specify whether an attribute must be included, can be omitted, or possesses a constant value. The #REQUIRED keyword mandates that an attribute must always appear in the XML element, #IMPLIED makes the attribute optional with no default value provided, and #FIXED binds the attribute to a specific, unchangeable value. Understanding the operational differences between these three keywords is fundamental to defining precise validation rules for XML documents.

The #REQUIRED Keyword

The #REQUIRED keyword indicates that an attribute is mandatory. Every time the specified element appears in an XML document, the attribute must be explicitly declared with a value. If the attribute is omitted, an XML validating parser will generate an error and mark the document as invalid.

<!-- DTD Declaration -->
<!ATTLIST user id CDATA #REQUIRED>

<!-- Valid XML -->
<user id="u101" />

<!-- Invalid XML (throws validation error) -->
<user />

The #IMPLIED Keyword

The #IMPLIED keyword specifies that an attribute is optional. The author of the XML document can choose whether or not to include the attribute. If the attribute is omitted, the XML processor ignores it, and no default value is supplied by the DTD.

<!-- DTD Declaration -->
<!ATTLIST product discount CDATA #IMPLIED>

<!-- Valid XML (included) -->
<product discount="15%" />

<!-- Valid XML (omitted) -->
<product />

The #FIXED Keyword

The #FIXED keyword defines an attribute with a static, unchangeable value. When using #FIXED, you must declare the fixed value directly after the keyword in the DTD. If an XML author includes the attribute in the document, its value must match the fixed value exactly; otherwise, the parser rejects it. If the attribute is omitted from the XML document, the parser automatically inserts the attribute with the defined fixed value.

<!-- DTD Declaration -->
<!ATTLIST document version CDATA #FIXED "1.0">

<!-- Valid XML (explicit match) -->
<document version="1.0" />

<!-- Valid XML (omitted, processor assumes version="1.0") -->
<document />

<!-- Invalid XML (value mismatch) -->
<document version="2.0" />

Summary of Differences