Previous Table of Contents Next


Attributes

Attributes are like the different flavors of an element. If ice cream were an element, “flavor” could be equal to “strawberry,” “vanilla,” or “chocolate.” As mentioned above, attributes are the different aspects of the element that must be defined for the SGML system to process an element in a document. For example, if your document has a graphic image that is embedded in its content but that must be loaded into the document during processing, there had better be some attribute to the <GRAPHIC> element that tells the system where to find the image. So, <GRAPHIC> could have a location attribute whose value could be either “remote” or “local.”

In an SGML document, attributes appear as part of the opening tag for an element. For example, the graphic with the location attribute just mentioned would look like this:

  Here’s sample text for a document.
  Now see a reference to this sample figure. See Figure 1.
  If you wanted to insert the figure here <GRAPHIC location="local">
  then the rest of the text could come later.

The syntax for the attribute is as follows:

 <GRAPHIC location="local">

An element can have as many of these attributes as it needs. Take the example of a memo—again, similar to the one in figure 3.2. But this time, suppose that the memo contains sensitive information and must have a restricted distribution. Also suppose that the memo is secret and requires a security clearance. You have two conditions that you want to keep track of in all your memo type documents, so you want to add two attributes to the <MEMO> element; call them distribution and clearance. Give each of these attributes two possible values, although they each could have more. Suppose that distribution can be either “full” or “limited” and that security clearance can be either “required” or “none.” How would a memo like that look? Here’s one possible example:

 <MEMO distribution="limited" clearance="required">
 <TO>Joe Mainman</TO>
 <FROM>Cloak N. Dagger</FROM>
 <BODY><P>The bird has flown South for the winter.</P></BODY></MEMO>

So, attributes can apply to any element at any level in the document. Big elements can have them and even very small elements can have them. Elements with any type of content model can have them. Figure 3.3 shows another example of how the memo from figure 3.2 might look with attributes added.


Fig. 3.3  The <DATE>, <SALUT>, and <P> elements have been modified. For example, “format” and “abbrev” further modify <DATE>, so different presentations for this element can exist in the same document.

Two Styles of Attributes. As you’ve seen, attributes in a document instance must be set to a value if such a value applies. There are two basic styles (or forms) for setting an attribute’s value. One style includes the equal sign (=) and one does not. One is a sort of shorthand, while the other style “spells it out,” so to speak. The style with the equal sign is the more common form, while the other form allows attribute values to appear by themselves. Both styles are acceptable. Both accomplish the same function, to tell the SGML system that is processing your document the current value of the attribute in question. Here are examples of each:

    <GRAPHIC location=local>

or

    <GRAPHIC local>

Which method should you use? You’ll be safest if you always use the equal sign. Just stating the value of the attribute without the equal sign is the shorter way, but it’s not as explicit. Adding the equal sign won’t add that much more work. See table 3.1 for a summary of these two styles of attributes.

Table 3.1 Styles or Forms of Attributes

Style Example

Common (with =) <SALUT punct=",">
Shorter form (without =) <SALUT ",">
Without quotes <SALUT comma> or <SALUT punct=comma>

When To Use Quotation Marks with Attributes. You probably noticed that sometimes attributes use quotes and sometimes they don’t. Basically, if an attribute’s value contains anything other than numbers or letters, the value must be set off with quotes. That means that if there are spaces, punctuation, symbols, or any non-letter or non-number, you must use quotes around the value. It can be confusing at first, but the rules are pretty simple. Table 3.2 gives you the basic idea.

Table 3.2 Determining When To Use Quotations with Attributes

Description Example

Attribute value includes only alpha or numbers <SALUT punct=comma> no quotes required
Attribute value includes non-alphanumeric characters <SALUT punct=","> quotes required
Attribute doesn’t include ”=” <SALUT ","> or <SALUT comma> quotes are optional

The thing to remember is whether or not your attribute value has any character other than letters or numbers. If you have so much as a blank space between two words, use quotation marks. Your safest bet is to just use quotation marks. You can’t go wrong that way.


Previous Table of Contents Next