DTD attribute
In a DTD, attributes ATTLIST statement to be declared.
Statement attributes
Property declaration uses the following syntax:
DTD 实例:
<!ATTLIST payment type CDATA "check">
XML 实例:
<payment type="check" />
These are theattributes types of options:
类型 | 描述 |
---|---|
CDATA | 值为字符数据 (character data) |
(en1|en2|..) | 此值是枚举列表中的一个值 |
ID | 值为唯一的 id |
IDREF | 值为另外一个元素的 id |
IDREFS | 值为其他 id 的列表 |
NMTOKEN | 值为合法的 XML 名称 |
NMTOKENS | 值为合法的 XML 名称的列表 |
ENTITY | 值是一个实体 |
ENTITIES | 值是一个实体列表 |
NOTATION | 此值是符号的名称 |
xml: | 值是一个预定义的 XML 值 |
Defaultproperty values can use the following values:
值 | 解释 |
---|---|
值 | 属性的默认值 |
#REQUIRED | 属性值是必需的 |
#IMPLIED | 属性不是必需的 |
#FIXED value | 属性值是固定的 |
Default property values
<!ELEMENT square EMPTY>
<!ATTLIST square width CDATA "0">
合法的 XML:
<square width="100" />
In the above example, "square" is defined as the "width" empty element with CDATA type of property. If the width is not set, it defaults to 0.
#REQUIRED
grammar
Examples
<!ATTLIST person number CDATA #REQUIRED>
合法的 XML:
<person number="5677" />
非法的 XML:
<person />
If you do not have a default value of the option, but still want to force authors to submit property, please use keyword #REQUIRED.
#IMPLIED
grammar
Examples
<!ATTLIST contact fax CDATA #IMPLIED>
合法的 XML:
<contact fax="555-667788" />
合法的 XML:
<contact />
If you do not want to force the author of a property, and you do not have a default value of an option, then use the keyword #IMPLIED.
#FIXED
grammar
Examples
<!ATTLIST sender company CDATA #FIXED "Microsoft">
合法的 XML:
<sender company="Microsoft" />
非法的 XML:
<sender company="W3Schools" />
If you want to have a fixed value of the property, and allowing the author to change this value, use #FIXED keywords. If the author uses a different value, XML parser will return an error.
Enumerated attribute values
grammar
Examples
<!ATTLIST payment type (check|cash) "cash">
XML 例子:
<payment type="check" />
或
<payment type="cash" />
If you want the attribute value is one of a fixed set of legal values, use the list attribute values.