DTD里属性用ATTLIST声明
语法:
<!ATTLIST element-name attribute-name attribute-type default-value>
例:
<!ATTLIST payment type CDATA "check">
XML例:<payment type="check" />
attribute-type列表如下:
Type | Description |
---|---|
CDATA |
The value is character data |
(en1|en2|..) |
The value must be one from an enumerated 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 |
The value is a list of valid XML names |
ENTITY |
The value is an entity |
ENTITIES |
The value is a list of entities |
NOTATION |
The value is a name of a notation |
xml: |
The value is a predefined xml value |
The default-value can be one of the following:
Value | Explanation |
---|---|
value |
The default value of the attribute |
#REQUIRED |
The attribute is required |
#IMPLIED |
The attribute is not required |
#FIXED value |
The attribute value is fixed |
带默认属性值的
DTD:<!ELEMENT square EMPTY>
<!ATTLIST square width CDATA "0">
valid xml:<square width="100" />
属性必须有的:
语法:<!ATTLIST element-name attribute-name attribute-type #REQUIRED>
例:<!ATTLIST person number CDATA #REQUIRED>
有效的xml:<person number="5677" />
属性可有可无的
语法:<!ATTLIST element-name attribute-name attribute-type #IMPLIED>
例:DTD:<!ATTLIST contact fax CDATA #IMPLIED>
有效xml: <contact fax="555-667788" /> or <contact />
固定属性的:
语法:<!ATTLIST element-name attribute-name attribute-type #FIXED "value">
例:DTD:<!ATTLIST sender company CDATA #FIXED "Microsoft">
有效xml:<sender company="Microsoft" />
带可选值属性的:
语法:<!ATTLIST element-name attribute-name (en1|en2|...) default-value>
例:<!ATTLIST payment type (check|cash) "cash">
xml例: <payment type="check">