XML

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"
  >

by BrainBellupdated
Advertisement: