May 28, 2021 DTD
In DTD, properties are declared through the ATTLIST declaration.
Property declarations use the following syntax:
<!ATTLIST element-name attribute-name attribute-type attribute-value>
DTD 实例:
<!ATTLIST payment type CDATA "check">
XML 实例:
<payment type="check" />
Here are the options for property types:
类型 | 描述 |
---|---|
CDATA | 值为字符数据 (character data) |
( en1 | en2 |..) | 此值是枚举列表中的一个值 |
ID | 值为唯一的 id |
IDREF | 值为另外一个元素的 id |
IDREFS | 值为其他 id 的列表 |
NMTOKEN | 值为合法的 XML 名称 |
NMTOKENS | 值为合法的 XML 名称的列表 |
ENTITY | 值是一个实体 |
ENTITIES | 值是一个实体列表 |
NOTATION | 此值是符号的名称 |
xml: | 值是一个预定义的 XML 值 |
The default property value can use the following values:
值 | 解释 |
---|---|
值 | 属性的默认值 |
#REQUIRED | 属性值是必需的 |
#IMPLIED | 属性不是必需的 |
#FIXED value | 属性值是固定的 |
DTD:
<!ELEMENT square EMPTY>
<!ATTLIST square width CDATA "0">
合法的 XML:
<square width="100" />
In the example above, "square" is defined as an empty element with the "width" property of the CDATA type. If the width is not set, its default value is 0.
DTD:
<!ATTLIST person number CDATA #REQUIRED>
合法的 XML:
<person number="5677" />
非法的 XML:
<person />
If you don't want to force the author to include properties, and you don't have a default option, use the keyword #IMPLIED.
Instance
DTD:
<!ATTLIST contact fax CDATA #IMPLIED>
合法的 XML:
<contact fax="555-667788" />
合法的 XML:
<contact />
If you don't want to force the author to include properties, and you don't have a default option, use the keyword #IMPLIED.
DTD:
<!ATTLIST sender company CDATA #FIXED "Microsoft">
合法的 XML:
<sender company="Microsoft" />
非法的 XML:
<sender company="W3Cschools" />
If you want the property to have a fixed value and do not allow the author to change it, use #FIXED keyword. If the author uses a different value, the XML parser returns an error.
<!ATTLIST element-name attribute-name (en1|en2|..) default-value>
DTD:
<!ATTLIST payment type (check|cash) "cash">
XML 例子:
<payment type="check" />
or
<payment type="cash" />
If you want the property value to be one of a series of fixed legal values, use Enumerated property values.
That's what we know about properties in DTD, and in the next section we'll compare the elements and properties of XML.