The ATTLIST Statement

[Previous] [Next]

Every element can have a set of attributes associated with it. The attributes for an element are defined in an !ATTLIST statement. The format for the !ATTLIST statement is shown here:

 <!ATTLIST ElementName AttributeDefinition> 

ElementName is the name of the element to which these attributes belong.

AttributeDefinition consists of the following components:

 AttributeName AttributeType DefaultDeclaration 

AttributeName is the name of the attribute. AttributeType refers to the data type of the attribute. DefaultDeclaration contains the default declaration section of the attribute definition.

Attribute Data Types

XML DTD attributes can have the following data types: CDATA, enumerated, ENTITY, ENTITIES, ID, IDREF, IDREFS, NMTOKEN, and NMTOKENS.

CDATA

The CDATA data type indicates that the attribute can be set to any allowable character value. For our sample DTD used for creating Web pages, the vast majority of the elements will have attributes with a CDATA data type. The following body attributes should all be CDATA:

 <!ATTLIST body alink CDATA #REQUIRED text CDATA #REQUIRED bgcolor CDATA #REQUIRED link CDATA #REQUIRED vlink CDATA #REQUIRED> 

Notice that you can list multiple attributes for a single element.

Enumerated

The enumerated data type lists a set of values that are allowed for the attribute. Using an enumerated data type, you can rewrite the font element to limit the color attribute to Cyan, Lime, Black, White, or Maroon; limit the size attribute to 2, 3, 4, 5, or 6; and limit the face attribute to Times New Roman or Arial. The new font declaration would look as follows:

 <!ATTLIST font color (Cyan | Lime | Black | White | Maroon) #REQUIRED size (2 | 3 | 4 | 5 | 6) #REQUIRED face (&apos;Times New Roman&apos;|Arial) #REQUIRED> 

NOTE
Keep in mind that this declaration is case sensitive. Thus, entering cyan as a color value would cause an error. Also notice the use of &apos; as a placeholder for a single quotation mark and the use of the parentheses to group the collection of choices.

In the section "The Default Declaration" later in this chapter, you'll learn how to declare a default value for the color and size attributes.

ENTITY and ENTITIES

The ENTITY and ENTITIES data types are used to define reusable strings that are represented by a specific name. These data types will be discussed in detail in Chapter 5.

ID, IDREF, and IDREFS

Within a document, you may want to be able to identify certain elements with an attribute that is of the ID data type. The name of the attribute with an ID data type must be unique for all of the elements in the document. Other elements can reference this ID by using the IDREF or IDREFS data types. IDREFS can be used to declare multiple attributes as IDREF.

When you work with HTML, you use anchor (a) elements to bookmark sections of your document. These bookmarks can be used to link to sections of the document. Unlike the ID data type, the a element does not have to be unique. In XML, IDs are used to create links to different places in your document. When we examine linking in detail in Chapter 6, you'll see that the ID data type offers other advantages.

Our example document includes an a element at the top of the document as an anchor that can be used to jump to the top of the page. You can modify the a element definition in the DTD as follows:

 <!ATTLIST a linkid ID #REQUIRED href CDATA #IMPLIED name CDATA #IMPLIED target CDATA #IMPLIED> 

Now when you create an XML document, you can define an a element at the top of the page and associate a unique ID with it using the linkid attribute. To reference this ID from another element, you first have to add an IDREF attribute to that element, as shown here:

 <!ATTLIST ul headlink IDREF #IMPLIED type CDATA #REQUIRED> 

In your XML document, you can associate the linkid attribute of the a element with the headlink attribute of the ul element by assigning the same value (HeadAnchor, for example) to these two attributes. If a second ID attribute, named footlink, was added to an element at the bottom of the XML document, you could define references to both of these elements. In this case, you would need to use IDREFS, as shown here:

 <!ATTLIST ul headlink footlink IDREFS #IMPLIED type CDATA #REQUIRED> 

The actual XML document would contain the following code:

 <a linkid="HeadAnchor" name="head"> <!--Head anchor--> </a> <!--Some HTML code here--> <a href="#head"> <ul headlink="HeadAnchor"> <!--li elements here--> </ul> </a> <a href="#foot"> <ul footlink="FootAnchor"> <!--li elements here--> </ul> </a> <!--Some more HTML code here--> <a linkid="FootAnchor" name="foot"> <!--Foot anchor--> </a> 

This code will work with non-XML browsers and with browsers that support XML.

NMTOKEN and NMTOKENS

The NMTOKEN and NMTOKENS data types are similar to the CDATA data type in that they represent character values. The name tokens are strings that consist of letters, digits, underscores, colons, hyphens, and periods. They cannot contain spaces. A declaration using these data types could look as follows:

 <!ATTLIST body background NMTOKEN "Blue" foreground NMTOKENS "Green, Yellow, Orange" > 

The Default Declaration

The default declaration can consist of any valid value for your attributes, or it can consist of one of three predefined keywords: #REQUIRED, #IMPLIED, or #FIXED. The #REQUIRED keyword indicates that the attribute must be included with the element and that it must be assigned a value. There are no default values when #REQUIRED is used. The #IMPLIED keyword indicates that the attribute does not have to be included with the element and that there is no default value. The #FIXED keyword sets the attribute to one default value that cannot be changed. The default value is listed after the #FIXED keyword. If none of these three keywords are used, a default value can be assigned if an attribute is not set in the XML document.



Developing XML Solutions
Developing XML Solutions (DV-MPS General)
ISBN: 0735607966
EAN: 2147483647
Year: 2000
Pages: 115
Authors: Jake Sturm

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net