当前位置: 首页 > 网络学院 > XML相关教程 > DTD > DTD - 属性

DTD
DTD - 介绍
DTD - XML 基本组件群
DTD - 元素
DTD - 属性
DTD - 实体
DTD - 校验
DTD - 案例
DTD - 摘要
DTD - 元素和属性

DTD - 属性


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-01   浏览: 597 ::
收藏到网摘: n/a

In a DTD, Attributes are declared with an ATTLIST declaration.
在DTD中,属性是通过ATTLIST声明来声明的。


Declaring Attributes
声明属性

An attribute declaration has the following syntax:
属性声明的语法如下:

<!ATTLIST element-name attribute-name 
attribute-type default-value>
example:
DTD example:
<!ATTLIST payment type CDATA "check">

XML example:
<payment type="check" />

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
独立的id值

IDREF

The value is the id of another element
其它元素的id值

IDREFS

The value is a list of other ids
其它id的列表值

NMTOKEN

The value is a valid XML name
其值为一个有效的XML名称

NMTOKENS

The value is a list of valid XML names
其值为一张有效的XML名称列表

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
其值为一个 预定义的XML值

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
固定的属性值

 


Specifying a Default attribute value
指定一个默认的属性值

DTD:
<!ELEMENT square EMPTY>

<!ATTLIST square width CDATA "0">
Valid XML:
<square width="100" />

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。


#IMPLIED

Syntax
语法

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

Example
案例

DTD:
<!ATTLIST contact fax CDATA #IMPLIED>
Valid XML:
<contact fax="555-667788" />
Valid XML:
<contact />

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关键字。


#REQUIRED

Syntax
语法

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

Example
案例

DTD:
<!ATTLIST person number CDATA #REQUIRED>
Valid XML:
<person number="5677" />
Invalid XML:

<person />

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关键词。


#FIXED

Syntax
语法

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

Example
案例

DTD:
<!ATTLIST sender company CDATA #FIXED "Microsoft">
Valid XML:
<sender company="Microsoft" />
Invalid XML:
<sender company="W3Schools" />

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解析器将会返回一条错误信息。


Enumerated attribute values
列举属性值的方法

Syntax:
<!ATTLIST element-name
attribute-name (en1|en2|..) default-value>
DTD example:
<!ATTLIST payment type (check|cash) "cash">

XML example:
<payment type="check" />
or
<payment type="cash" />

Use enumerated attribute values when you want the attribute values to be one of a fixed set of legal values.
当你希望属性值是一组合法的固定值时,那么,你不妨使用“列举属性值”的方法。

评论 (0) All

登陆 | 还没注册?