Home > Articles

  • Print
  • + Share This
This chapter is from the book

Classifying Attributes: Attribute Types

Attributes can be classified based on the kind of information that they contain. When you define an attribute in XML, you are really defining the attribute type, or the kind of information that the attribute may legally contain to describe an element. Attributes can be one of three different types: strings, tokenized types, or enumerations.

In the interest of compatibility and to create rules for validation (which we will discuss later in this chapter), there are a number of different types of attributes. Attributes consist of predefined datatypes that can be assigned to your attributes when you are working with a Document Type Definition or an XML Schema.

For example, one attribute type is ID, which is an identifier for the element, a unique identifier. What this allows you to do is to refer to an element by its ID, rather than the tag name. An ID attribute can be used to provide a unique identifier for a specific instance of an element, whereas the element name might refer to multiple instances of an element within a single document. If you are working with a DTD or an XML Schema, you can declare your attributes to be an ID type.

Why is this useful? We could have a document describing the parts of a camera:

<camera>
<part ID="body">Nikon F4</part>
<part ID="lens">28mm</part>
<part ID="flash">Speedflash 50</part>
</camera>

Each of the part elements refers to a part of the camera, but what if we wanted to refer to just the lens for the camera? We could not refer to the part element, because there are several parts to the camera. Instead, with the ID attribute, we can reference the appropriate part by ID. This becomes even more useful for documents with large amounts of similar data, such as a catalog:

<catalog>
<part number="09339">Hyrdolic Pump</part>
<part number="33881">Flange Gasket</part>
<part number="33291">Flexi-hose</part>
</catalog>

TIP

Because you can have attributes of different types, sometimes you might want to have the type of the attribute explicitly in the name of the type. For example, we could have named our attribute "number_ID" to indicate that the attribute was a number. If you are working with attributes that are just text (CData), this probably isn't necessary, but for IDs or IDREFs it can come in handy.

Here we have a listing of parts that might not even be related to one another, other than as part of the same catalog. Although the usefulness of this type of attribute is readily apparent, the value of some other attribute types might not seem so straightforward.

NOTE

Attribute names do not necessarily reflect the datatype of the attribute. For example, the number attribute used in the catalog example could still be an ID attribute, even if it is not called ID.

Attribute types are associated with a specific datatype only if you are working with valid XML, including a DTD or an XML Schema. Otherwise, XML parsers do not differentiate between the types of information in attribute content.

Taking advantage of most of the different types of attributes requires that you use a DTD or XML Schema with your document. However, you can still use attributes in an XML document that is only well-formed. We'll talk more about using attributes with DTD later in Chapter 4 and with XML Schemas in Chapter 5. However, now let's get acquainted with the different types of attributes that you will have at your disposal, so you can start thinking about them as you work with XML.

ID

The first type, and arguably one of the most useful, is the ID attribute type. You've already seen an example of how an ID attribute can be used to help classify parts. The basic idea of an ID is to provide a unique identifier for each instance of an element type in a document.

This is important, because the nature of classifying information leads to multiple instances of elements. If you had an XML document describing the holdings of a library, you would probably have a book element, and there would likely be many instances of the book element. There might be hundreds, if not thousands, of <book> entries in your document. Although you might have child elements that would help narrow your searches, such as <title> or <author>, you could use an ID attribute to provide a unique identifier, that could then be referenced by other attributes or elements. The unique ID attribute might be the call number of the book, for example, which would allow you to quickly locate the correct book element that you were looking for.

This is a very important concept as XML usage becomes ubiquitous. Providing unique, internal identifiers can be a great mechanism for linking to specific elements within a document.

There aren't too many rules to follow when creating ID attributes:

  • The attribute name has to be a valid name. That is, like element names, it cannot begin with xml and so on.

  • The value of each attribute with an ID type must be unique.

IDREF and IDREFS

The next attribute type we're going to discuss goes hand-in-hand with the ID attribute type: IDREF. An IDREF attribute is a reference to an ID attribute. IDREFS refers to more than one IDREF.

What good is a reference to an ID? Well, for starters it can be a great way to organize and classify information. Let's look at an example. Suppose we work for a company that sells cameras and lenses. We want to have a document that lists the parts of each item we sell, which might look like this:

<catalog>
 <product type="film-camera">
 <part>Nikon F4 Body</part>
 <part>28mm Lens</part>
 </product>
 <product type="digital-camera">
 <part>Nikon D1 Body</part>
 <part>28mm Lens</part>
 </product>
</catalog>

Here we have two products, with an ID attribute called TYPE which lets us differentiate between our different types of cameras.

However, in this example, each camera shares a common part, the 28mm Lens. The lens in question is actually the same part, but we might sometimes need to be specific with respect to which camera the lens in question belongs. For example, we might want to alter the filter that shipped with the lens depending on whether it was for the film or the digital version of the camera.

There are several ways we could do this. We could create new elements such as <digital-lens> and <film-lens>; however, it really is the same lens, so instead, we can create an ID and an IDREF to link the two elements together. Our modified document might look like this:

<catalog>
 <product name="F4" type="film-camera">
 <part model="F4">Nikon F4 Body</part>
 <part model="F4">28mm Lens</part>
 </product>
 <product name="D1"type="digital-camera">
 <part model="D1">Nikon D1 Body</part>
 <part model="D1">28mm Lens</part>
 </product>
</catalog>

Now each of our elements has an ID attribute called name that is used to uniquely identify each product. We then can use an IDREF attribute—in this case, model—to refer to the name attribute of the appropriate product. Keep in mind that these types would all be declared in our DTD or XML Schema. Being able to refer to the specific product by a unique name can help ensure that future items that need to be linked to a model can also be linked correctly—for example, if we added a section for filters, we have the model number associated with the lens, so we could ship filters based on the model of camera.

Although, in this case, the relationship is clear because of the document structure, we might have a separate file that looks like this:

<catalog>
 <product model="F4" type="lens">28mm</product>
 <product model="F4" type="lens">35mm</product>
 <product model="F4" type="lens">50mm</product>
 <product model="F4" type="lens">70mm</product>
 <product model="D1" type="lens">28mm</product>
 <product model="D1" type="lens">35mm</product>
 <product model="D1" type="lens">50mm</product>
 <product model="D1" type="lens">70mm</product>
</catalog>

Here, we still have references to the ID attributes, even though the relationship of the lens is not clear from the structure. In fact, we can group together elements to many different IDs, by using the attribute type IDREFS, which is just more than one IDREF:

<catalog>
 <product model="F4 D1" type="lens">28mm</product>
 <product model="F4 D1" type="lens">35mm</product>
 <product model="F4 D1" type="lens">50mm</product>
 <product model="F4 D1" type="lens">70mm</product>
</catalog>

Now you will notice that our attribute for the shared lens reads <product model="F4 D1">. You can use the IDREFS type to reference multiple IDs, each one separated by a space.

To make effective use of these datatypes for attributes, you will need to use a DTD. The reason for this is simply because without a DTD, all attributes look the same to a parser. There is no way for an XML parser to know whether the attribute is an ID or not unless that datatype is specified in the DTD. We'll talk more about how you define attributes in the DTD later in Chapter 4.

Enumerated Attributes

One of the most useful types of attributes that can be defined in XML is enumerated attributes. These attributes allow you to define a list of values for the attribute, and allow users to select the value from that list. It's a way you can create multiple-choice attributes.

This is useful for a number of different applications. For example, let's say that you were defining an XML document for a clothing catalog. You might have XML that looked something like this:

<catalog>
 <item>
 <shirt>Men's T-Shirt</shirt>
 </item>
 <item>
 <shirt>Women's Blouse</shirt>
 </item>
</catalog>

It would certainly make sense to have an option for specifying the size of the garments that are available. One way might be to offer a choice from a list of available sizes—for example, Small, Medium, Large, and Extra-Large.

We can add a size attribute to the <shirt> element:

<shirt size="Large">Women's Blouse</shirt>

This attribute allows us to specify the size of an item. However, as it is currently defined, the value of the size attribute could be literally any value. To limit the choice of the value to "Small," "Medium," "Large," or "Extra-Large," we need to make the attribute an enumerated type.

An attribute type of enumeration means that the value for the attribute must be chosen from a list of attribute values defined in the DTD or XML Schema. Just like other attribute values, the enumerated values have to meet the constraints for attribute content (that is, no < symbols). And you can't use enumerated attributes without a DTD or an XML Schema, because without the rule specifying what the choices are, there is no way for the XML application to know ahead of time what you want them to be.

  • + Share This
  • 🔖 Save To Your Account