DTD - 属性

    技术2022-05-11  61

    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列举属性值方法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.当你希望得到一个固定合法的属性值时,请使用列举属性值(Enumerated attribute values)的方法。

     

    最新回复(0)