Attribute List Declaration..

As we saw in the last chapter, the element type declaration constrains the element’s content. In this chapter we will see how an attribute list declaration constrains the attribute’s content.

Attributes provide additional information about elements. Elements and attributes also exist in HTML . <H1 align=”center”> includes an element ‘H1′, and an attribute value ‘center’. In XML, attributes specify additional data about an element, but it is never formatting related as in HTML. It is, instead, additional data about that particular element.

Attributes are created in the DTD, when the elements are specified.  They are specified through an attribute list.

<!–This defines a listing of books–>
<!DOCTYPE books [
<!ELEMENT booklist (title, author)>
<!ELEMENT title (#PCDATA)>
<!ATTLIST title
edition (CDATA) #REQUIRED
type (paper|cloth|hard)"paper">
<!ELEMENT author (#PCDATA)>
]>

Attribute declarations identify which elements may have attributes, what types of attributes they may have, what values the attributes may hold, and what default value each attribute has. Attribute specifications may appear only within start-tags and empty-element tags.

Attribute-list declarations specify the name, data type, and default value (if any) of each attribute associated with a given element type:

  • Name: To define the set of attributes pertaining to a given element type.
  • Type: To establish type constraints for these attributes
  • Default value: To provide default values for attributes

    Attribute Defaults

    • #REQUIRED
      means that the attribute must have a value every time this element is listed. Although there is no default value provided by the DTD, the attribute when actually implemented in an XML document must define a value. In our example the default value tells the validator that the ID must be present and a value must be specified.

    <!ATTLIST title id ID #REQUIRED>

    • #FIXED
      Sometimes you will want to provide a default value that the document author may not modify. In that case, you will use #FIXED. Attribute where only one value is allowed, that must be included. An attribute declaration may specify that an attribute has a fixed value. In this case, the attribute is not required, but if it occurs, it must have the specified value. One use for fixed attributes is to associate semantics with an element.
    • #IMPLIED
      The attribute value is not required, and no default value is provided. If a value is not specified, the XML processor must proceed without one.The processor ignores this attribute unless it is used as part of this element. It does not assume any default value.When you use the IMPLIED default, you will provide a default value for the document author. If the document author does not override your default, your default will be used. Unless otherwise specified, attributes are implied.
    • “VALUE”
      An attribute can be given any legal value as a default. The attribute value is not required on each element in the document, but if it is not present, it will appear to be the specified default. Value (default values) provides a default value for that attribute. If the attribute is not included in the element, the processing program assumes that this is the attributes value.

    interest (hot|warm|cool|unknown) “unknown”
    <!ATTLIST attitude interest (hot|warm|cool|unknown) “unknown”>
    e.g. <attitude interest=”warm”>

    If the document just contained
    <interest/>

    then the attribute “unknown” would be presumed by the validating software.

    Attribute values in XML must always be contained in quotes.

    Attribute TypesThere are three kinds of XML attribute types: a string type, a set of tokenized types, and enumerated types.

    1. String Type

    String Type may take any literal string as a value. CDATA attribute are strings where any text is allowed. Don’t confuse CDATA attributes with CDATA sections. In CDATA attributes, markup is recognized.

    role CDATA #IMPLIED

    <!ATTLIST actor role CDATA #IMPLIED>
    e.g.: <actor role=”hamlet”>

    2. Set of Tokenized Types

    A name must not appear more than once in an XML document as a value of this type. The tokenized types have varying lexical and semantic constraints. In other words, a tokenized attribute type represents a fixed set of keyword types with special meanings.

    • ID Attribute Default
      ID represents a unique ID name for the attribute that identifies the element within the context of the document. IDs are much like internal links in plain HTML. For the most part, ID is used primarily by programs or scripting languages that process the document. The value for ID must be a valid XML name beginning with a letter and containing alphanumeric characters or the
      underscore character without any whitespace. ID is incompatible with the #FIXED default but it must have a declared default of #IMPLIED or #REQUIRED. Also, take care that your ID values are unique within a document! All ID values used in a document must be different. IDs uniquely identify individual elements in a document. Elements can have only one single ID attribute

      id ID #REQUIRED

      <!ATTLIST title id ID #REQUIRED>
      e.g. <title id=”3″>

      .

    • IDREF
      Values of type IDREF must match the Name production, and values of type IDREFS must match Names; each Name must match the value of an ID attribute on some element in the XML document; i.e. The IDREF type allows the value of one attribute to be an element elsewhere in the document provided that the value of the IDREF is the ID value of the referenced element.
    • Entity Name
      Values of type ENTITY must match the Name production, values of type ENTITIES must match Names; each Name must match the name of an unparsed entity declared in the DTD.
    • Name Token
      The NMTOKEN and NMTOKENS types are another example of those types that are useful primarily to processing applications. The types are used to specify  valid names. You might use them when you are associating some other component with the element. Values of type NMTOKEN must match the NMTOKEN production; values of type NMTOKENS must match NMTOKENS.

    3. Enumerated Type

    You can specify the value of an attribute that must be taken from a specific list of names. There are two kinds of enumerated types

    • Enumeration
      Attributes can also be defined by a list of acceptable pipe delimited values from which the document author must choose. In this case each of the values is explicitly enumerated in the declaration.

    <!ATTLIST cast
    gender (male|female|unknown)”unknown”>#

    • Notation Type
      specifies that the names must match a particular notation name. Values of this type must match one of the NMTOKEN tokens in the declaration. For interoperability, the same NMTOKEN should not occur more than once in the enumerated attribute types of a single element type.

    Note!

    • No attribute name may appear more than once in the same start-tag or empty-element tag.
    • The attribute must have been declared; the value must be of the type declared for it.
    • No External Entity References.
    • Attribute values cannot contain direct or indirect entity references to external entities.
    • The replacement text of any entity referred to directly or indirectly in an attribute value (other than “&lt;”) must not contain a <.
  • ~ by xrizix on April 9, 2008.

    Leave a Reply