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.
- Syntax:
<!ATTLIST ElementName AttributeName AttributeType #REQUIRED> - Use Case: Unique identifiers, primary keys, or
critical data that an element cannot function without (e.g., an
idattribute on anemployeeelement).
<!-- 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.
- Syntax:
<!ATTLIST ElementName AttributeName AttributeType #IMPLIED> - Use Case: Non-essential metadata, optional
descriptors, or secondary information (e.g., a
middle_nameattribute or adiscount_code).
<!-- 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.
- Syntax:
<!ATTLIST ElementName AttributeName AttributeType #FIXED "FixedValue"> - Use Case: Specifying version numbers, standard namespaces, or immutable configuration flags across all instances of an element.
<!-- 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
- Requirement Status:
#REQUIREDis mandatory;#IMPLIEDis optional;#FIXEDis structurally optional in the document text because the parser supplies the value automatically if omitted. - Value Constraints:
#REQUIREDand#IMPLIEDallow any valid value matching the specified attribute type;#FIXEDrestricts the attribute strictly to one predefined value. - Default Values:
#REQUIREDand#IMPLIEDprovide no default values, whereas#FIXEDinherently supplies a constant default.