当前位置: 首页 > 网络学院 > XML相关教程 > DTD > DTD - 属性
In a DTD, Attributes are declared with an ATTLIST declaration.
在DTD中,属性是通过ATTLIST声明来声明的。
An attribute declaration has the following syntax:
属性声明的语法如下:
<!ATTLIST element-name attribute-name example: DTD example: |
The attribute-type can have the following values:
属性类型可以包含下述这些值:
Value 值 | Explanation 解释 |
---|---|
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 have the following values:
默认值可以包含下述这些值:
Value 值 | Explanation 解释 |
---|---|
value | The default value of the attribute |
#REQUIRED | The attribute value must be included in the element |
#IMPLIED | The attribute does not have to be included |
#FIXED value | The attribute value is fixed |
DTD: Valid XML: |
In the example above, the "square" element is defined to be an empty element with a "width" attribute of type CDATA. If no width is specified, it has a default value of 0.
在上述案例中,"square"元素定义为一个包含有CDATA类型的"width"属性的空元素。如果没有指定width值,那它的默认值为0。
<!ATTLIST element-name attribute-name |
DTD: Valid XML: Valid XML: |
Use the #IMPLIED keyword if you don't want to force the author to include an attribute, and you don't have an option for a default value.
如果你不希望让 author 元素包含一个属性值,并且此时你还不具备对默认值的选择权,那么你不妨使用#IMPLIED关键字。
<!ATTLIST element-name attribute_name |
DTD: Valid XML: Invalid XML: |
Use the #REQUIRED keyword if you don't have an option for a default value, but still want to force the attribute to be present.
如果你不具备对默认值的选择权,但是却希望仍然让该属性值存在,那么你不妨使用#REQUIRED关键词。
<!ATTLIST element-name attribute-name |
DTD: Valid XML: Invalid XML: |
Use the #FIXED keyword when you want an attribute to have a fixed value without allowing the author to change it. If an author includes another value, the XML parser will return an error.
当你希望属性值是一个不允许author改变的固定值,那么,你不妨使用#FIXED关键字。如果author包含另外一个值,XML解析器将会返回一条错误信息。
Syntax: DTD example: |
Use enumerated attribute values when you want the attribute values to be one of a fixed set of legal values.
当你希望属性值是一组合法的固定值时,那么,你不妨使用“列举属性值”的方法。