Latest web development tutorials

DTD attribute

In a DTD, attributes ATTLIST statement to be declared.


Statement attributes

Property declaration uses the following syntax:

<!ATTLIST element-name attribute-name attribute-type attribute-value>

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

DTD:
<!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

<!ATTLIST element-name attribute-name attribute-type #REQUIRED>

Examples

DTD:
<!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

<!ATTLIST element-name attribute-name attribute-type #IMPLIED>

Examples

DTD:
<!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

<!ATTLIST element-name attribute-name attribute-type #FIXED "value">

Examples

DTD:
<!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

<!ATTLIST element-name attribute-name (en1|en2|..) default-value>

Examples

DTD:
<!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.