perfectxml.com
 Basic Search  Advanced Search   
Topics Resources Free Library Software XML News About Us
  You are here: home ĽĽ Free Library ĽĽ Microsoft Press » XML Step by Step: Chapter 3: Creating Well-Formed XML Documents Saturday, 23 February 2008
 
XML Step by Step: Chapter 3: Creating Well-Formed XML Documents


  • Chapter 3: Creating Well-Formed XML Documents
    • The Parts of a Well-Formed XML Document
      • A Minimalist XML Document
    • Adding Elements to the Document
      • The Anatomy of an Element
      • Types of Element Content
      • Empty Elements
      • Create Different Types of Elements
    • Adding Attributes to Elements
      • Rules for Creating Attributes
      • Rules for Legal Attribute Values
      • Convert Content to Attributes

In this chapter, youíll learn the basic techniques for creating a well-formed XML document. A well-formed document is one that meets the minimal set of criteria for a conforming XML document. When you create a well-formed XML document, you can pitch right in and begin adding elements as you need them and entering your documentís data, just as you do when you create an HTML Web page. (Although, as you learned in the previous chapters, in an XML document you invent your own elements rather than using predefined ones.) And youíll have no problem handling and displaying any well-formed XML document in Internet Explorer 5.

In Chapter 5, youíll learn how to create a valid XML document: a document that is not only well-formed but also conforms to a more rigid set of constraints. Creating a valid XML document is not as easy as creating one that is only well-formed. Before you begin adding elements and data to a valid document, you must fully define the structure of the document in a document type declaration added to the documentís prolog. In Chapter 5 youíll learn that there are some advantages to making documents valid, especially if you or others are creating a group of similar documents.

In this chapter, youíll first learn about all the required and optional parts of a well-formed XML document. Next youíll discover how to add information to an XML document by defining the documentís elements. Youíll then learn how to supply additional document information by adding attributes to the elements.


The Parts of a Well-Formed XML Document

As you learned in Chapter 2, an XML document consists of two main parts: the prolog and the document element (which is also known as the root element). In addition, following the document element, a well-formed XML document can include comments, processing instructions, and white space. Hereís an example of a well-formed XML document that shows the different document parts and the items you can add to each part:


Listing 3-1 shows the complete version of this example document. (Youíll find a copy of this listing on the companion CD under the filename Parts.xml.)

Parts.xml


<?xml version='1.0' standalone='yes' ?>
<!-- File Name: Parts.xml -->
<?xml-stylesheet type="text/css" href="Inventory01.css"?>
<INVENTORY>
   <BOOK>
      <TITLE>The Adventures of Huckleberry Finn</TITLE>
      <AUTHOR>Mark Twain</AUTHOR>
      <BINDING>mass market paperback</BINDING>
      <PAGES>298</PAGES>
      <PRICE>$5.49</PRICE>
   </BOOK>
   <BOOK>
      <TITLE>Leaves of Grass</TITLE>
      <AUTHOR>Walt Whitman</AUTHOR>
      <BINDING>hardcover</BINDING>
      <PAGES>462</PAGES>
      <PRICE>$7.75</PRICE>
   </BOOK>
   <BOOK>
      <TITLE>The Legend of Sleepy Hollow</TITLE>
      <AUTHOR>Washington Irving</AUTHOR>
      <BINDING>mass market paperback</BINDING>
      <PAGES>98</PAGES>
      <PRICE>$2.95</PRICE>
   </BOOK>
   <BOOK>
      <TITLE>The Marble Faun</TITLE>
      <AUTHOR>Nathaniel Hawthorne</AUTHOR>
      <BINDING>trade paperback</BINDING>
      <PAGES>473</PAGES>
      <PRICE>$10.95</PRICE>
   </BOOK>
   <BOOK>
      <TITLE>Moby-Dick</TITLE>
      <AUTHOR>Herman Melville</AUTHOR>
      <BINDING>hardcover</BINDING>
      <PAGES>724</PAGES>
      <PRICE>$9.95</PRICE>
   </BOOK>
   <BOOK>
      <TITLE>The Portrait of a Lady</TITLE>
      <AUTHOR>Henry James</AUTHOR>
      <BINDING>mass market paperback</BINDING>
      <PAGES>256</PAGES>
      <PRICE>$4.95</PRICE>
   </BOOK>
   <BOOK>
      <TITLE>The Scarlet Letter</TITLE>
      <AUTHOR>Nathaniel Hawthorne</AUTHOR>
      <BINDING>trade paperback</BINDING>
      <PAGES>253</PAGES>
      <PRICE>$4.25</PRICE>
   </BOOK>
   <BOOK>
      <TITLE>The Turn of the Screw</TITLE>
      <AUTHOR>Henry James</AUTHOR>
      <BINDING>trade paperback</BINDING>
      <PAGES>384</PAGES>
      <PRICE>$3.35</PRICE>
   </BOOK>
</INVENTORY>
<!-- Comments, processing instructions, and white space
     can also appear after the document element. -->
<?MyApp Parm1="value 1" Parm2="value 2" ?>

Listing 3-1.

The version number in the XML declaration at the start of the document prolog can be delimited with either single or double quotes. In general, quoted strings in XML markup-known as literals-can use either single or double quotes. Thus, both of the following are legal:


<?xml version='1.0'?>
<?xml version="1.0"?>

The XML declaration in the example document in Listing 3-1 also includes a standalone document declaration (standalone='yes'). This declaration can be used in some XML documents to simplify document processing. (Iíll discuss the standalone document declaration in Chapter 6.)

The example document includes a comment in the prolog and another comment following the document element. (Youíll learn more about comments in Chapter 4.)

The document also contains a blank line labeled "white space" in the prolog and another that follows the document element. White space consists of one or more space, tab, carriage return, or linefeed characters. To make an XML document more readable to humans, you can freely add white space between XML markup (such as start-tags, end-tags, comments, and processing instructions) and also in many places within markup. (For instance, the space between the 'yes' and the ? at the end of the XML declaration in the example document.) The processor simply ignores white space unless itís within an element that directly contains character data. (In this case, the processor passes the white space to the application as part of the elementís character data.)

The example document has a processing instruction in the prolog and another that follows the document element. (Iíll discuss processing instructions in Chapter 4.)

Finally, the document includes the sine qua non of an XML document: the document element. Creating the document element and the nested elements that it contains is the focus of this chapter.


NOTE:
As youíll learn in Chapter 5, a valid XML document needs to contain one additional component that isnít included in the example document in Listing 3-1: a document type declaration, which you can place anywhere in the prolog, outside of other markup, following the XML declaration. A document type declaration defines the structure of a valid XML document.




A Minimalist XML Document

The prolog in the example XML document in Listing 3-1 contains an example of each of the items allowed within a prolog. Note, however, that these items are all optional (although the XML specification states that you "should" include the XML declaration). Hence, the prolog itself is optional, and the following minimalist document, which contains only a simple document element, conforms to the XML standard for a well-formed document:



<minimal>A minimalist document.</minimal>

This document would be displayed in Internet Explorer 5 as shown here:





Adding Elements to the Document

The elements in an XML document contain the actual document information (in Listing 3-1, for example, the titles, authors, prices, and other information on the books in the inventory) and they indicate the logical structure of this information.

The elements are arranged in a treelike hierarchical structure, with elements nested within other elements. The document must have exactly one top-level element-the document element or root element-with all other elements nested within it. Hence, the following is a well-formed XML document:



<?xml version="1.0"?>
<!-- A well-formed XML document. -->
<INVENTORY>
   <BOOK>
      <TITLE>The Adventures of Huckleberry Finn</TITLE>
      <AUTHOR>Mark Twain</AUTHOR>
      <BINDING>mass market paperback</BINDING>
      <PAGES>298</PAGES>
      <PRICE>$5.49</PRICE>
   </BOOK>
   <BOOK>
      <TITLE>Leaves of Grass</TITLE>
      <AUTHOR>Walt Whitman</AUTHOR>
      <BINDING>hardcover</BINDING>
      <PAGES>462</PAGES>
      <PRICE>$7.75</PRICE>
   </BOOK>
</INVENTORY>

The following document is not well-formed:



<?xml version="1.0"?>
<!-- This document is NOT well-formed. -->
<BOOK>
   <TITLE>The Adventures of Huckleberry Finn</TITLE>
   <AUTHOR>Mark Twain</AUTHOR>
   <BINDING>mass market paperback</BINDING>
   <PAGES>298</PAGES>
   <PRICE>$5.49</PRICE>
</BOOK>
<BOOK>
   <TITLE>Leaves of Grass</TITLE>
   <AUTHOR>Walt Whitman</AUTHOR>
   <BINDING>hardcover</BINDING>
   <PAGES>462</PAGES>
   <PRICE>$7.75</PRICE>
</BOOK>

Elements must also be properly nested. That is, if an element (delimited by a start-tag and an end-tag, as Iíll explain later) begins within another element, it must also end within that same element. For example, these elements are well-formed:



<BOOK>
   <TITLE>Leaves of Grass</TITLE>
   <AUTHOR>Walt Whitman</AUTHOR>
</BOOK>

These elements, however, are not well-formed:



<!-- NOT well-formed: -->
<BOOK><TITLE>Leaves of Grass</BOOK></TITLE>   

NOTE:
An element that contains one or more nested elements (such as BOOK in Listing 3-1) is known as a parent element. An element contained directly within the parent (such as TITLE within BOOK) is known as a child element, subelement, or nested element of the parent.




The Anatomy of an Element

As youíve seen, an element usually consists of a start-tag, content, and an end-tag.

Figure 3-3.

Unlike HTML, XML requires you to always include both the start-tag and the end-tag. (The only exception is an element without content, for which you can use the special empty-element tag that Iíll describe later in this chapter.)

The name that appears at the beginning of the start-tag and in the end-tag (TITLE in the above example) is known as the elementís type or generic identifier (GI). The type name identifies a particular type or class of element, not a specific element. Thus, the document can contain more than one element that has the same type name (such as the BOOK or TITLE elements in Listing 3-1).

When you add an element to your XML document, you can select any type name you want, provided that you follow these guidelines:

  • The name must begin with a letter or underscore (_), followed by zero or more letters, digits, periods (.), hyphens (-), or underscores.

  • The XML specification states that element-type names beginning with the prefix "xml" (in any combination of uppercase or lowercase letters) are "reserved for standardization." Although Internet Explorer 5 doesnít enforce this restriction, itís better not to use this prefix to avoid future problems.

The following are legal element-type names:


Part
_1stPlace
A
B-SECTION
Street.Address.1

The following, however, are illegal element-type names:


1stPlace   <!-- Digit not allowed as first character -->
B Section  <!-- Space not allowed within a name -->
B/Section  <!-- Slash not allowed within a name -->
:Chapter   <!-- Colon not allowed as first character in IE5 -->
A:Section  <!-- In IE5, allowed only if you've declared A
                as a namespace -->

NOTE:
According to the XML specification, the colon (:) in an element name is reserved for designating namespaces. Namespaces differentiate elements that have the same name; Iíll discuss them in "Inserting HTML Elements into XML Documents and Using Namespaces" in Chapter 7. Internet Explorer 5 lets you insert a colon in an element name only if it follows a namespace that youíve declared in the document. For example, A:Section would be legal only if youíve declared A as a namespace.



Also, the name in the start-tag must exactly match the name in the end-tag, including the case of all letters. Thus, the following element is not well-formed:


<Title>Chapter One</title>   <!-- NOT well-formed. -->

Case is significant in element names, as it is in all text within markup. Thus, an element type named Ace is not the same element type as ace or ACE.


Types of Element Content

The element content is the text between the start-tag and the end-tag. You can include the following types of items in the content of an element:


  • Nested elements. In Listing 3-1, both the INVENTORY element and the BOOK elements contain nested elements as their content:

    Figure 3-4.


    <

  • Character data. Character data is text that expresses the information content of an element, such as a specific book title in a TITLE element:

    Figure 3-5.



    Hereís an example of element content that consists of both character data and a nested element:

    Figure 3-6.



    When adding character data to an element, you can insert any characters except the left angle bracket (<), the ampersand (&), or the string ]]>.


    NOTE:
    The XML parser scans an elementís character data for XML markup. You canít insert < or & or the string ]]> as a part of the character data because the parser would interpret < as the start of a nested element, & as the beginning of an entity or character reference, and ]]> as the end of a CDATA section. (Iíll cover entity and character references in Chapter 6; Iíll cover CDATA sections in Chapter 4). If you want to insert < or & as a part of the character data, you can use a CDATA section. You can also insert any character (including one not on your keyboard) by using a character reference, and you can insert certain characters (such as < or &) by using predefined general entity references. I'll explain character and predefined general entity references in Chapter 6.




  • General entity references or character references. Hereís an element containing one of each: Figure 3-7.

  • CDATA sections. A CDATA section is a block of text in which you can freely insert any characters except the string ]]>. Hereís an example of a CDATA section in an element:

    Figure 3-8.



  • Processing instructions. A processing instruction provides information to the XML application. (See Chapter 4.)

  • Comments. A comment is an annotation to your XML document that people can read but that the XML processor ignores. (See Chapter 4.)

    Hereís an element containing both a processing instruction and a comment:

    Figure 3-9.



Empty Elements

You can also enter an empty element-that is, one without content-into your document. You can create an empty element by placing the end-tag immediately after the start-tag, as in this example:


<HR></HR>

Or, you can save typing by using the special empty-element tag, as shown here:


<HR/>

These two notations have the same meaning.

Because an empty element has no content, you might question its usefulness. Here are two possible uses:


  • You can use an empty element to tell the XML application to perform an action or display an object. Examples from HTML are the BR empty element, which tells the browser to insert a line break, and the HR empty element, which tells it to add a horizontal dividing line. In other words, the mere presence of an element with a particular name-without any content-can provide important information to the application.

  • An empty element can store information through attributes, which youíll learn about later in this chapter. (You havenít seen elements with attributes yet.) An example from HTML is the IMG (image) empty element, which contains attributes that tell the processor where to find the graphics file and how to display it.


TIP:
As youíll learn in Chapter 8, a cascading style sheet can use an empty element to display an image. In Chapter 8, youíll learn how to use data binding to access the attributes of an empty or non-empty element. And in Chapters 9 and 10, youíll learn how to use HTML scripts (Chapter 9) and XSL style sheets (Chapter 10) to access elements (empty or non-empty) and their attributes, and then perform appropriate actions.




Create Different Types of Elements


  1. Open a new, empty text file in your text editor, and type in the XML document shown in Listing 3-2 (youíll find a copy of this listing on the companion CD under the filename Inventory03.xml). If you want, you can use the Inventory.xml document you created in Chapter 2 (given in Listing 2-1 and on the companion CD) as a starting point.

  2. Use your text editorís Save command to save the document on your hard disk, assigning the filename Inventory03.xml.
    Inventory03.xml
    <?xml version="1.0"?>
    <!-- File Name: Inventory03.xml -->
    <?xml-stylesheet type="text/css" href="Inventory02.css"?>
    <INVENTORY> <!-- Inventory of selected 19th Century 
                     American Literature -->
       <BOOK>
          <COVER_IMAGE Source="Huck.gif" />
          <TITLE>The Adventures of Huckleberry Finn</TITLE>
          <AUTHOR>Mark Twain</AUTHOR>
          <BINDING>mass market paperback</BINDING>
          <PAGES>298</PAGES>
          <PRICE>$5.49</PRICE>
       </BOOK>
       <BOOK>
          <COVER_IMAGE Source="Leaves.gif" />
          <TITLE>Leaves of Grass</TITLE>
          <AUTHOR>Walt Whitman</AUTHOR>
          <BINDING>hardcover</BINDING>
          <PAGES>462</PAGES>
          <PRICE>$7.75</PRICE>
       </BOOK>
       <BOOK>
          <COVER_IMAGE Source="Faun.gif" />
          <TITLE>The Marble Faun</TITLE>
          <AUTHOR>Nathaniel Hawthorne</AUTHOR>
          <BINDING>trade paperback</BINDING>
          <PAGES>473</PAGES>
          <PRICE>$10.95</PRICE>
       </BOOK>
       <BOOK>
          <COVER_IMAGE Source="Moby.gif" />
          <TITLE>
             Moby-Dick
             <SUBTITLE>Or, the Whale</SUBTITLE>
          </TITLE>
          <AUTHOR>Herman Melville</AUTHOR>
          <BINDING>hardcover</BINDING>
          <PAGES>724</PAGES>
          <PRICE>$9.95</PRICE>
       </BOOK>
    </INVENTORY>

    Listing 3-2.


    NOTE:
    The document you typed uses the CSS named Inventory02.css that you created in a previous exercise. (Itís given in Listing 2-4 and on the companion CD.) Make sure that this style sheet file is in the same folder as Inventory03.xml.




  3. In Windows Explorer or in a folder window, double-click the name of the file that you saved, Inventory03.xml:

    double-click saved file

    Internet Explorer 5 will now display the document as shown here:


The document you entered contains the following types of elements and element content:


  • An element with a comment as part of its content (INVENTORY). Notice that the browser doesnít display the comment text.

  • An empty element named COVER_IMAGE at the beginning of each BOOK element. The purpose of this element is to tell the XML application to display the specified image of the bookís cover. (The Source attribute contains the name of the image file.) To be able to use such an element, however, you would need to display the XML document via a script in an HTML page or an XSL style sheet (as discussed in Chapters 9 and 10), rather than using a simple CSS as in this example.

  • An element (the TITLE element for Moby-Dick) that contains both character data and a child element (SUBTITLE). Notice that the browser displays both the character data and the child element in a single line, using the same format (the CSS format assigned to the TITLE element is inherited by the SUBTITLE element).

Adding Attributes to Elements

In the start-tag of an element, or in an empty-element tag, you can include one or more attribute specifications. An attribute specification is a name-value pair that is associated with the element. For example, the following PRICE element includes an attribute named Type, which is assigned the value retail:


<PRICE Type="retail">$10.95</PRICE>

For other books, this attribute might, for example, be set to wholesale.

The following BOOK element includes two attributes, Category and Display:


<BOOK Category="fiction" Display="emphasize">
   <TITLE>The Marble Faun</TITLE>
   <AUTHOR>Nathaniel Hawthorne</AUTHOR>
   <BINDING>trade paperback</BINDING>
   <PAGES>473</PAGES>
   <PRICE Type="retail">$10.95</PRICE>
</BOOK>

The following empty element includes an attribute named Source, which indicates the name of the file containing the image to be displayed:


<COVER_IMAGE Source="Faun.gif" />

Adding an attribute provides an alternative way to include information in an element. Typically, you place the bulk of the elementís data that you intend to display within the elementís content. And you use attributes to store various properties of the element, not necessarily intended to be displayed, such as a category or a display instruction. The XML specification, however, makes no rigid distinctions about the type of information that should be stored within attributes or content.


NOTE:
When you display an XML document using a CSS (the method covered in Chapter 7), the browser does not display attributes or their values. Displaying an XML document using data binding (Chapter 8), a script in an HTML page (Chapter 9), or an XSL style sheet (Chapter 10), however, allows you to access attributes and their values, and to display the values or perform other appropriate actions.




Rules for Creating Attributes

As you can see, an attribute specification consists of an attribute name followed by an equals sign followed by an attribute value. You can choose any attribute name you want, provided that you follow these rules:


  • The name must begin with a letter or underscore (_), followed by zero or more letters, digits, periods (.), hyphens(-), or underscores.

  • The XML specification states that attribute names beginning with the prefix "xml" (in any combination of uppercase or lowercase letters) are "reserved for standardization." Although Internet Explorer 5 doesnít enforce this restriction, itís better not to use this prefix to avoid future problems.

  • A particular attribute name can appear only once in the same start-tag or empty-element tag.

For example, the attribute names in the following start-tags are legal:


<ANIMATION FileName="Waldo.ani">
<LIST _1stPlace="Sam">
<ENTRY Zip.Code="94941">
The following attribute names, however, are illegal:
<!-- Duplicated attribute name in same tag: -->
<ANIMATION FileName="Waldo1.ani" FileName="Waldo2.ani">
<LIST 1stPlace="Sam"> <!-- Digit not allowed as 
                           first character -->
<ITEM A:Category="cookware"> <!-- In IE5, allowed only if you've 
                                  declared A as a namespace -->

NOTE:
According to the XML specification, using the colon in an attribute name is reserved for designating namespaces. Namespaces are used to differentiate attributes that have the same name; they are discussed in "Inserting HTML Elements into XML Documents and Using Namespaces" on page XXX. Internet Explorer 5 lets you insert a colon in an attribute name only if it follows a namespace that youíve declared in the document. For example, A:Category would be legal only if youíve declared A as a namespace.




Rules for Legal Attribute Values

The value you assign to an attribute is a series of characters delimited with quotes, known as a quoted string or literal. You can assign any literal value to an attribute, provided that you observe these rules:


  • The string can be delimited using either single quotes (') or double quotes (").

  • The string cannot contain the same quote character used to delimit it.

  • The string can contain character references or references to general internal entities. (Iíll explain character and entity references in Chapter 6.)

  • The string cannot include the < character. (The parser would confuse this character with the start of XML markup.)

  • The string cannot include the & character, except to begin a character or entity reference.

Youíve already seen examples of legal attribute specifications. The following attribute specifications are illegal:


<EMPLOYEE Status=""downsized""> <!-- Can't use delimiting quote 
                                     within string. -->
<ALBUM Type="<CD>"> <!-- Can't use < within string. -->
<WEATHER Forecast="Cold & Windy"> <!-- Can't use & except to 
                                       start a reference. -->

If you want to include double quotes (") within the attribute value, you can use single quotes (') to delimit the string, as in this example:


<EMPLOYEE Status='"downsized"'> <!-- Legal attribute value. -->

Likewise, to include a single quote within the value, delimit it using double quotes:


<CANDIDATE name="W.T. 'Bill' Bagley"> <!-- Legal attribute value. -->

TIP:
You can get around the character restrictions and enter any character into an attribute value by using a character reference or-if available-a predefined general entity reference. I'll explain character and predefined general entity references in Chapter 6.



If you create a well-formed document that doesnít have a document type declaration (as youíve done in this chapter), you can assign an attribute any value that conforms to the rules described above. However, as youíll learn in Chapter 5, when you create a document type declaration and define attributes within it, you can limit the types of values that can be assigned to a particular attribute. For example, you could define an attribute that can be assigned only the value "yes" or "no". Thus, one advantage of storing certain types of information in element attributes rather than in content is that you can exert far more control over the types of data that can be assigned to the attribute, and have the parser enforce these type constraints. (As youíll see in Chapter 5, the basic XML specification doesnít provide a means for constraining the type of character data in an element.)


Convert Content to Attributes


  1. Open a new, empty text file in your text editor, and type in the XML document shown in Listing 3-3 (youíll find a copy of this listing on the companion CD under the file name Inventory04.xml). If you want, you can use the Inventory.xml document you previously typed (given in Listing 2-1 and on the companion CD) as a starting point.

  2. Use your text editorís Save command to save the document on your hard disk with the filename Inventory04.xml. Inventory04.xml
    <?xml version="1.0"?>
    <!-- File Name: Inventory04.xml -->
    <?xml-stylesheet type="text/css" href="Inventory02.css"?>
    <INVENTORY>
       <BOOK Binding="mass market paperback">
          <TITLE>The Adventures of Huckleberry Finn</TITLE>
          <AUTHOR Born="1835">Mark Twain</AUTHOR>
          <PAGES>298</PAGES>
          <PRICE>$5.49</PRICE>
       </BOOK>
       <BOOK Binding="hardcover">
          <TITLE>Leaves of Grass</TITLE>
          <AUTHOR Born="1819">Walt Whitman</AUTHOR>
          <PAGES>462</PAGES>
          <PRICE>$7.75</PRICE>
       </BOOK>
       <BOOK Binding="trade paperback">
          <TITLE>The Marble Faun</TITLE>
          <AUTHOR Born="1804">Nathaniel Hawthorne</AUTHOR>
          <PAGES>473</PAGES>
          <PRICE>$10.95</PRICE>
       </BOOK>
       <BOOK Binding="hardcover">
          <TITLE>Moby-Dick</TITLE>
          <AUTHOR Born="1819">Herman Melville</AUTHOR>
          <PAGES>724</PAGES>
          <PRICE>$9.95</PRICE>
       </BOOK>
    </INVENTORY>

    Listing 3-3.


    NOTE:
    The document you typed uses the CSS named Inventory02.css that you created in a previous exercise. (Itís given in Listing 2-4 and on the companion CD.) Make sure that this style sheet file is in the same folder as Inventory04.xml.




  3. In Windows Explorer or in a folder window, double-click the name of the file that you saved, Inventory04.xml:

    double-click saved file

    Internet Explorer 5 will now display the document as shown here:


The document you typed is based on Inventory.xml, which you created in a previous exercise. In addition to having fewer elements than Inventory.xml, the new document has two modifications that illustrate the use of attributes:


  • In each BOOK element, the bookís binding information was converted from content (in the form of the BINDING nested element) to an attribute named Binding. You might make this conversion if, for example, you wanted to store the binding type but didnít want to show it along with the other book information when displaying the document using a CSS. (In the figure above, notice that Internet Explorer 5 doesnít display the attribute values.)

  • An attribute named Born was added to each AUTHOR element to store the authorís birth date. This is an example of less important information that you might want to store but not necessarily display. One way to hide such information-and indicate its lesser importance-is to assign it to an attribute rather than placing it in the content of an element.

These are only a few of the many possible uses for attributes. Youíll see more in Chapter 5.




  Contact Us | E-mail Us | Site Guide | About PerfectXML | Advertise ©2004 perfectxml.com. All rights reserved. | Privacy