Declaring Attributes

Just as the element declaration limits the contents of a particular element, the attribute declaration controls which and what type of attributes can be included in a particular element.

The <!ATTLIST> markup tag is used to control the attributes that can be included in a particular element. As an example, here's the attribute declaration for the item element:

<!ATTLIST item
  id  ID    #IMPLIED
  ref IDREF #IMPLIED
>

The first name token after the <!ATTLIST> markup is the name of the element associated with this declaration. Following the element name is a list of attribute name, type, and default-value triples. Multiple <!ATTLIST> declarations can be given for a single element throughout the DTD. The result is the same as if all the declarations were combined into a single large declaration.

Looking at a single triple, the first token is the name of the attribute to be declared. The second token is the attribute type, which must come from the following list:

  • CDATA— Plain-character data

  • ID— A document-unique ID

  • IDREF, IDREFS— A single value or list of values that must be equal to the value of an ID attribute within the document

  • NMTOKEN, NMTOKENS— A single XML name token or list of tokens, respectively

  • ENTITY, ENTITIES— A single declared entity name, or a list of names

  • NOTATION— A single name from an enumeration of declared notation names

Besides these types, it is also possible to declare an attribute that is limited to a list of valid values. The full attribute declaration syntax is very complex and would require an entire chapter in itself. For a full explanation of valid attribute declarations, consult a complete XML reference. However the most frequently used attribute types by far are CDATA, ID, and IDREF.

CDATA Attributes

The CDATA type indicates that the attribute can contain an arbitrary string of character data. Character references (such as &#38;) are recognized and expanded within the string, as are entity references. Attribute strings cannot contain unescaped markup characters (such as <, >, and &). Also, as in most programming languages, because the attribute value is quoted, it cannot contain unescaped quote characters (for example, "He said "Boo"" would not be valid). In some cases using the alternative quote character (' instead of ") can be simpler than using the &quot; built-in entity reference: 'He said "Boo"'.

ID and IDREF Attributes

The ID and IDREF types are complementary. The XML ID mechanism is intended to give document authors a way to uniquely identify elements within a document. The IDREF attribute type indicates that the attribute may contain only a value that matches an ID value within the same document. Here are a few points to remember about ID and IDREF attributes:

  • ID values must be valid XML names (that is, start with a letter or underscore [_], and contain only letters, numbers, and certain punctuation).

  • No two elements (even of different element types) can have the same value in an ID attribute. For instance, having two elements <person id="ID20"/> <company id="ID20"/> in a single document would make that document invalid.

  • Every IDREF attribute must match the value of an ID attribute somewhere in the document. IDREFs are like one-way links to elements, and it is illegal to have a document with broken links in it.

NMOTOKEN Attributes

The NMTOKEN type indicates that the corresponding attribute must contain a value that is a valid XML name token. There is a fine difference between XML names and XML name tokens: name tokens may start with any valid name character, instead of being restricted to letters and the underscore (_) character (or colon, which should not be used in order to minimize namespace confusion).

Referencing External Entities

ENTITY attributes are provided to allow XML documents to include references to data that is not valid XML, and possibly not even textual. The basic usage of an entity attribute is to declare an external unparsed entity using an <!ENTITY> declaration (covered in the next section), and then use the entity name as the value for an entity attribute. One common application of this is to include image data in an XML document, as shown in Listing 2.3.

Listing 2.3. An Entity Attribute Example
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE picture [
  <!NOTATION gif SYSTEM "images/gif">
  <!ENTITY flowers_gif SYSTEM "flowers.gif" NDATA gif>
  <!ELEMENT picture EMPTY>
  <!ATTLIST picture
    src ENTITY #REQUIRED
  >
]>

<picture src="flowers_gif"/>

Although this approach has some advantages, such as associating notation information with external entity references, many XML experts recommend against using entity attributes for this purpose. More familiar and full-featured solutions such as the XLink standard should be used instead.

NOTATION Attributes

The NOTATION attribute type provides a mechanism for associating an XML notation name with a particular element. In the declaration, a list of possible valid notations is given, like so:

<!ATTLIST picture
  src   ENTITY #REQUIRED
  type  NOTATION (bmp | gif | jpg) #REQUIRED
>

Within the document, every <picture> element must have a type attribute that contains one of the notation names given in the list (bmp, gif, or jpg). When the document is parsed, the XML application can use the notation name to access additional information about the element type from the associated <!NOTATION> declaration.

Attribute List Types: IDREFS, ENTITIES, and NMTOKENS

The plural forms of the IDREF, ENTITY, and NMTOKEN types can be used to declare an attribute that contains a list of values, instead of a single value. The values in the list must be separated by whitespace and must conform to the same restrictions as the singular form (discussed earlier).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.158.32