DTD - Attribute
- Previous Page DTD Element
- Next Page DTD Element Comparison with Attribute
In DTD, attributes are declared by the ATTLIST declaration.
Declare attribute
Attribute declarations use the following syntax:
<!ATTLIST element_name attribute_name attribute_type default_value>
DTD instance:
<!ATTLIST payment type CDATA "check">
XML instance:
<payment type="check" />
The following areAttribute typeOptions:
Type | Description |
---|---|
CDATA | The value is character data (character data) |
(en1|en2|..) | This value is one of the values in the enumeration list |
ID | The value is a unique id |
IDREF | The value is the id of another element |
IDREFS | The value is a list of other ids |
NMTOKEN | The value is a valid XML name |
NMTOKENS | Value is a list of valid XML names |
ENTITY | Value is an entity |
ENTITIES | Value is a list of entities |
NOTATION | This value is the name of a symbol |
xml: | Value is a predefined XML value |
Default value parameters can use the following values:
Value | Explanation |
---|---|
Value | Default value of the attribute |
#REQUIRED | Attribute value is required |
#IMPLIED | Attribute is not required |
#FIXED value | Attribute value is fixed |
Specify a default attribute value
DTD:
<!ELEMENT square EMPTY> <!ATTLIST square width CDATA "0">
Valid XML:
<square width="100" />
In the above example, "square" is defined as an empty element with a CDATA type attribute "width". If the width is not set, the default value is 0.
#IMPLIED
Syntax
<!ATTLIST element_name attribute_name attribute_type #IMPLIED>
Example
DTD:
<!ATTLIST contact fax CDATA #IMPLIED>
Valid XML:
<contact fax="555-667788" />
Valid XML:
<contact />
If you do not want to force the author to include the attribute and you do not have a default value option, please use the keyword #IMPLIED.
#REQUIRED
Syntax
<!ATTLIST element_name attribute_name attribute_type #REQUIRED>
Example
DTD:
<!ATTLIST person number CDATA #REQUIRED>
Valid XML:
<person number="5677" />
Illegal XML:
<person />
If you do not have a default value option but still want to force the author to submit the attribute, please use the keyword #REQUIRED.
#FIXED
Syntax
<!ATTLIST element_name attribute_name attribute_type #FIXED "value">
Example
DTD:
<!ATTLIST sender company CDATA #FIXED "Microsoft">
Valid XML:
<sender company="Microsoft" />
Illegal XML:
<sender company="W3School" />
If you want the attribute to have a fixed value and not allow the author to change this value, please use the #FIXED keyword. If the author uses a different value, the XML parser will return an error.
List of attribute values
Syntax:
<!ATTLIST element_name attribute_name (en1|en2|..) default_value>
DTD Example:
<!ATTLIST payment type (check|cash) "cash">
XML Example:
<payment type="check" />
Or
<payment type="cash" />
If the attribute value should be one of a series of fixed valid values, use enumerated attribute values.
- Previous Page DTD Element
- Next Page DTD Element Comparison with Attribute