Attributes

Attributes in XML are much like attributes in HTML: They're name -value pairs that let you specify additional data in start and empty tags. To assign a value to an attribute, you use an equals sign.

For example, I'm assigning values to the STATUS attribute of the <CUSTOMER> elements in the XML here to indicate the status of the customer's credit:

Listing ch02_03.xml
 <?xml version = "1.0" standalone="yes"?> <DOCUMENT>  <CUSTOMER STATUS="Good credit">  <NAME>             <LAST_NAME>Smith</LAST_NAME>             <FIRST_NAME>Sam</FIRST_NAME>         </NAME>         <DATE>October 15, 2003</DATE>         <ORDERS>             <ITEM>                 <PRODUCT>Tomatoes</PRODUCT>                 <NUMBER>8</NUMBER>                 <PRICE>.25</PRICE>             </ITEM>             <ITEM>                 <PRODUCT>Oranges</PRODUCT>                 <NUMBER>24</NUMBER>                 <PRICE>.98</PRICE>             </ITEM>         </ORDERS>     </CUSTOMER>  <CUSTOMER STATUS="Lousy credit">  <NAME>             <LAST_NAME>Jones</LAST_NAME>             <FIRST_NAME>Polly</FIRST_NAME>         </NAME>         <DATE>October 20, 2003</DATE>         <ORDERS>             <ITEM>                 <PRODUCT>Bread</PRODUCT>                 <NUMBER>12</NUMBER>                 <PRICE>.95</PRICE>             </ITEM>             <ITEM>                 <PRODUCT>Apples</PRODUCT>                 <NUMBER>6</NUMBER>                 <PRICE>.50</PRICE>             </ITEM>         </ORDERS>     </CUSTOMER>  <CUSTOMER STATUS="Good credit">  <NAME>             <LAST_NAME>Weber</LAST_NAME>             <FIRST_NAME>Bill</FIRST_NAME>         </NAME>         <DATE>October 25, 2003</DATE>         <ORDERS>             <ITEM>                 <PRODUCT>Asparagus</PRODUCT>                 <NUMBER>12</NUMBER>                 <PRICE>.95</PRICE>             </ITEM>             <ITEM>                 <PRODUCT>Lettuce</PRODUCT>                 <NUMBER>6</NUMBER>                 <PRICE>.50</PRICE>             </ITEM>         </ORDERS>     </CUSTOMER> </DOCUMENT> 

You can see this XML document in Internet Explorer, including the attributes and their values, in Figure 2-2.

Figure 2-2. Using attributes in Internet Explorer.

graphics/02fig02.gif

An XML processor can read the attributes and their values, and you can put that data to work in your own applications. We'll see how to read attribute values in both JavaScript and Java in this book.

A lot of debate rages over when you should store data using attributes and when you should store data using elements. There simply is no hard and fast rule, but here are a couple of guidelines that I find useful. First, too many attributes definitely make documents hard to read. For example, take a look at this element:

 <CUSTOMER>      <NAME>         <LAST_NAME>Smith</LAST_NAME>         <FIRST_NAME>Sam</FIRST_NAME>     </NAME>     <DATE>October 15, 2003</DATE>     <ORDERS>         <ITEM>             <PRODUCT>Tomatoes</PRODUCT>             <NUMBER>8</NUMBER>             <PRICE>.25</PRICE>         </ITEM>     </ORDERS> </CUSTOMER> 

It's fairly clear what's going on here, even if it's a little involved. However, if you try to convert all this data to attributes, you end up with something like this:

 <CUSTOMER LAST_NAME="Smith" FIRST_NAME="Sam"  DATE="October 15, 2003" PURCHASE="Tomatoes" PRICE=".25" NUMBER="8" /> 

Clearly, this is going to be a mess if you have a few such elements.

Another point is that you really can't specify document structure using attributes. For instance, the example we've already seen in this chapter stores multiple ordered items per customer. But attribute names must be unique, so it's much tougher to store data like this using attributes:

 <CUSTOMER>      <NAME>         <LAST_NAME>Smith</LAST_NAME>         <FIRST_NAME>Sam</FIRST_NAME>     </NAME>     <DATE>October 15, 2003</DATE>     <ORDERS>         <ITEM>             <PRODUCT>Tomatoes</PRODUCT>             <NUMBER>8</NUMBER>             <PRICE>.25</PRICE>         </ITEM>         <ITEM>             <PRODUCT>Oranges</PRODUCT>             <NUMBER>24</NUMBER>             <PRICE>.98</PRICE>         </ITEM>     </ORDERS> </CUSTOMER> 

The upshot is that deciding whether to store your data in attributes or create new elements is really a matter of tasteuntil you get beyond a few attributes per tag. If you find yourself using (not just defining, but using) more than four attributes in a tag, consider breaking up the tag into a number of enclosed tags. Doing so will make the document structure much easier to work with and edit later.

You should follow specific rules when creating attributes. Those include setting attribute names and specifying attribute values. I'll take a look at these rules now.

Attribute Names

According to the XML 1.0 specification, attribute names must follow the same rules as those for tag names, which means that you can start an attribute name with a letter, an underscore , or a colon . The next characters may be letters , digits, underscores, hyphens, periods, and colons (but no whitespace because you separate attribute name-value pairs with whitespace).

Here are some examples showing legal attribute names:

 <circle origin_x="10.0" origin_y="20.0" radius="10.0" />  <image src="image1.jpg"> <pen color="red" width="5"> <book pages="1231" > 

And here are some illegal attribute names:

 <circle 2origin_x="10.0" 2origin_y="20.0" 2radius="10.0" />  <image src name="image1.jpg"> <pen color@="red" width@="5"> <book pages(excluding front matter)="1231" > 

Attribute Values

Because markup is always text, attributes are also text. Even if you're assigning a number to an attribute, you treat that number as a text string and enclose it in quotation marks, like this:

 <circle origin_x="10.0" origin_y="20.0" radius="10.0" /> 

Among other things, this means that XML processors will return attribute values as text strings. If you want to treat them as numbers, you'll have to make sure you translate them to numbers as the programming language you're using allows.

In XML, you must enclose attribute values in quotation marks. Usually you use double quotes, but consider the case in which the attribute value itself contains double quotes: You can't just surround such a value with double quotes because an XML processor will get confused as to where your text ends. In such a case, you can use single quotes to surround the text, like this:

 <quotation text='He said, "Not that!"' /> 

What if the attribute value contains both single and double quotes? In that case, you can use the XML-defined entity references for a single quote, &apos; , and for a double quote, &quot; , like this (I'll discuss the five XML-defined entity references in a few pages):

 <person height="5&apos;6&quot;" /> 

Assigning Values to Attributes

If you're going to use an attribute, you must assign it a value. Not doing so is a violation of well- formedness . That is, you cannot have "standalone" attributes in XML, such as the BORDER attribute in HTML tables, which need not be assigned a value.

A Useful Attribute: xml:lang

One general attribute bears mention: xml:lang . It's often convenient to specify the language of a document's content and attribute values, especially to help software such as Web search engines. You can specify the language in XML tags with the xml:lang attribute. (In valid documents, this attribute, like any other, must be declared if it is used.)

You can set the xml:lang attribute to these values:

  • A two-letter language code as defined by [ISO 639].

  • A language identifier registered with the Internet Assigned Numbers Authority (IANA). Such identifiers begin with the prefix "i-" (or "I-").

  • A language identifier assigned by you or for private use. Such identifiers must begin with "x-" or "X-".

Here is an example. In this case, I'm using the xml:lang attribute to indicate that an element's language is English:

 <p xml:lang="en">The color should be brown.</p> 

You can also use language subcodes if you follow the language code with a hyphen and the subcode. A subcode indicates a dialect or regional variation. For example, here I'm indicating that one element holds British English content and one holds American English:

 <p xml:lang="en-GB">The colour should be brown.</p>  <p xml:lang="en-US">The color should be brown.</p> 

Here's an example where I'm using German:

 <p farbe="braun" xml:lang="de"> 


Real World XML
Real World XML (2nd Edition)
ISBN: 0735712867
EAN: 2147483647
Year: 2005
Pages: 440
Authors: Steve Holzner

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