DTD - 属性

Sa DTD, ang attribute ay idiniklara sa pamamagitan ng ATTLIST na deklarasyon.

Deklara ang attribute

Ang pagdeklara ng attribute ay gumagamit ng sumusunod na syntax:

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

DTD instance:

<!ATTLIST payment type CDATA "check">

XML instance:

<payment type="check" />

Ang mga sumusunod ay:Uri ng attributeMga opsyon ng:

Uri Paglalarawan
CDATA Ang halaga ay character data
(en1|en2|..) Ang halaga ay isang halaga sa listahan ng pagpipilian
ID Ang halaga ay isang nag-iisang id
IDREF Ang halaga ay id ng ibang elemento
IDREFS Ang halaga ay listahan ng iba pang id
NMTOKEN Ang halaga ay isang lehitimong pang-XML na pangalan
NMTOKENS The 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

The default value parameter can use the following values:

Value Explanation
Value The 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 "width" attribute. If the width is not set, the default value is 0.

#IMPLIED

Grammar

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

Grammar

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

Grammar

<!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 do 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 attribute values

Grammar:

<!ATTLIST element_name attribute_name (en1|en2|..) default_value>

DTD 例子:

<!ATTLIST payment type (check|cash) "cash">

XML 例子:

<payment type="check" />

或者

<payment type="cash" />

如果您希望属性值为一系列固定的合法值之一,请使用列举属性值。