Displaying XML Documents Using Data Binding Data binding is the first technique that you’ll learn for displaying an XML document from within a conventional HTML page.. Displaying XML fro
Trang 1292 XML Step by Step
■ The first and last verses in each stanza are placed in special elements named FIRSTVERSE and LASTVERSE This allows you to format these verses differently than the other verses (The first verse will have a larger first letter and the last verse will be right justified rather than left.)
2 Use your text editor’s Save command to save the document on your hard disk, assigning it the filename Raven04.xml
Raven04.xml
<?xml version=”1.0"?>
<! File Name: Raven04.xml >
<?xml-stylesheet type=”text/css” href=”Raven04.css”?>
<POEM>
<TITLE>The Raven</TITLE>
<AUTHOR>
Edgar Allan Poe
<AUTHOR-BIO>
Edgar Allan Poe was an American writer who lived
from 1809 to 1849.
</AUTHOR-BIO>
</AUTHOR>
<DATE>1845</DATE>
<IMAGE/>
<STANZA>
<FIRSTVERSE>Once upon a midnight dreary, while I
pondered, weak and weary,</FIRSTVERSE>
<VERSE>Over many a quaint and curious volume of
forgotten lore—</VERSE>
<VERSE>While I nodded, nearly napping,
suddenly there came a tapping,</VERSE>
<VERSE>As of some one gently rapping,
rapping at my chamber door.</VERSE>
<VERSE>”’Tis some visitor,” I muttered,
“tapping at my chamber door—</VERSE>
<LASTVERSE>Only this, and nothing more.”</LASTVERSE>
</STANZA>
<IMAGE/>
<STANZA>
<FIRSTVERSE>Ah, distinctly I remember it was in the
Trang 2Chapter 9 Displaying XML Documents Using Advanced Cascading Style Sheets 293
bleak December,</FIRSTVERSE>
<VERSE>And each separate dying ember wrought its
ghost upon the floor.</VERSE>
<VERSE>Eagerly I wished the morrow;—vainly I had
sought to borrow</VERSE>
<VERSE>From my books surcease of sorrow—sorrow
for the lost Lenore—</VERSE>
<VERSE>For the rare and radiant maiden whom the angels
name Lenore—</VERSE>
<LASTVERSE>Nameless here for evermore.</LASTVERSE>
</STANZA>
<IMAGE/>
<STANZA>
<FIRSTVERSE>And the silken sad uncertain rustling
of each purple curtain</FIRSTVERSE>
<VERSE>Thrilled me—filled me with fantastic
terrors never felt before;</VERSE>
<VERSE>So that now, to still the beating of my heart,
I stood repeating:</VERSE>
<VERSE>”’Tis some visitor entreating entrance at
my chamber door—</VERSE>
<VERSE>Some late visitor entreating entrance
at my chamber door;</VERSE>
<LASTVERSE>This it is, and nothing more.”</LASTVERSE>
</STANZA>
<IMAGE/>
<STANZA>
<FIRSTVERSE>Presently my soul grew stronger;
hesitating then no longer,</FIRSTVERSE>
<VERSE>”Sir,” said I, “or Madam, truly your
forgiveness I implore;</VERSE>
<VERSE>But the fact is I was napping, and so
gently you came rapping,</VERSE>
<VERSE>And so faintly you came tapping,
tapping at my chamber door,</VERSE>
<VERSE>That I scarce was sure I heard you”—here
I opened wide the door;—</VERSE>
<LASTVERSE>Darkness there, and nothing more.</LASTVERSE>
</STANZA>
</POEM>
Listing 9-8
Trang 3Chapter 9 Displaying XML Documents Using Advanced Cascading Style Sheets 295
AUTHOR
{word-spacing:.25em}
AUTHOR-BIO
{display:none}
DATE
{font-style:italic}
IMAGE
{background-image:url(RavShade.bmp);
background-repeat:no-repeat;
background-position:center;
width:89px;
height:58px;
float:left}
STANZA
{display:block;
margin-bottom:1em;
color:navy;
line-height:1.25em}
VERSE, FIRSTVERSE, LASTVERSE
{display:block}
FIRSTVERSE:first-letter
{font-size:large}
LASTVERSE
{text-align:right}
Listing 9-9
3 Display the document by opening the Raven04.xml file directly in Internet
Explorer:
Trang 4Displaying XML
Documents Using
Data Binding
Data binding is the first technique that you’ll learn for displaying an XML
document from within a conventional HTML page Displaying XML from
within HTML pages gives you the best of both worlds: the ability to store
data using XML, with its capacity for describing and structuring virtually
any type of information, plus the ability to display and work with that
information using HTML, with its feature-rich elements (such as tables,
hyperlinks, images, frames, forms, buttons, and other controls) and its
dynamic programmability
In data binding, you link an XML document to an HTML page, and then
bind standard HTML elements, such as SPANs or TABLEs, to individual XML elements or attributes The HTML elements then automatically display the
contents of the XML elements or attributes to which they’re bound
Data binding and the related techniques you’ll learn in this chapter work
best with an XML document that is structured symmetrically, like a typical
database—namely, a document whose elements can be interpreted as a set of
records and fields In its simplest form, such a document consists of a root
element containing a series of elements of the same type (the records), each of which has the same set of child elements that all contain character data (the
fields) An example is the Inventory.xml document you’ll see in Listing 10-1,
in which the BOOK elements can be interpreted as records, and the elements
nested within each BOOK element (TITLE, AUTHOR, and so on) can be inter-preted as fields Later in the chapter, you’ll learn more about the specific docu-ment structures that are suitable for data binding For docudocu-ments that aren’t
suitable, you can use the scripting techniques you’ll explore in Chapter 11
CHAPTER
10
Trang 5298 XML Step by Step
In this chapter, you’ll first gain an overview of the two main steps for data bind-ing You’ll then learn in detail how to link the XML document to the HTML page (the first main step), and how to bind HTML elements to XML elements and attributes (the second main step) Finally, you’ll learn how to program a Web page using scripts that employ the same underlying programming object as data binding (namely, the Data Source Object, or DSO) You can use these scripts in conjunction with data binding—or independently of it
In Chapter 11, you’ll learn an entirely different way to access, manage, and dis-play an XML document from within an HTML page The techniques given there will let you traverse the entire logical structure of an XML document, and you can use them with any type of XML document
tip
For more information on using data binding and the DSO with Microsoft Internet Explorer, see the topics “Binding the XML Data Source Object to Data,”
“Using the C++ XML Data Source Object,” and “Using the Master/Detail Feature with the C++ XML Data Source Object” in the Microsoft XML SDK 4.0 help file, or the same topics in the XML SDK documentation provided
by the MSDN (Microsoft Developer Network) Library on the Web at
http://msdn.microsoft.com/library/
The Main Steps
There are two main steps for using data binding:
1 Linking the XML document to the HTML page in which you want to
display the XML data This step is normally done by including an HTML
ment named XML within the HTML page For example, the following ele-ment in an HTML page links the XML docuele-ment Inventory.xml to the page:
<XML ID=”dsoInventory” SRC=”Inventory.xml”></XML>
2 Binding HTML elements to XML elements or attributes When you bind an
HTML element to an XML element or attribute, the HTML element auto-matically displays the content of the XML element or the value of the XML attribute For instance, the following SPAN element in an HTML page is bound to the AUTHOR element in the linked XML document:
<SPAN DATASRC=”#dsoInventory” DATAFLD=”AUTHOR”></SPAN>
As a result, the SPAN HTML element displays the contents of the AUTHOR XML element
Trang 6Chapter 10 Displaying XML Documents Using Data Binding 299
The basic technique of data binding really is as simple as this, although you’ll learn many variations in the way you can use it The following sections cover these two steps in detail
The First Step: Linking the XML Document
to the HTML Page
To display an XML document in an HTML page, you must link the document
to the page The easiest way to do this with Microsoft Internet Explorer is to in-clude in the page an HTML element named XML, which is also known as a
data island You can use one of two different forms for a data island.
The first form of data island includes the entire text of the XML document be-tween the XML start-tag and end-tag The data island in the following HTML page is an example:
<HTML>
<HEAD>
<TITLE>Book Inventory</TITLE>
</HEAD>
<BODY>
<XML ID="dsoInventory">
<?xml version="1.0"?>
<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>
Trang 7Chapter 10 Displaying XML Documents Using Data Binding 301
<?xml version="1.0"?>
<! File Name: Inventory.xml >
<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>
<! other BOOK elements >
</INVENTORY>
The second form of data island conforms more closely to the basic XML tenet
of keeping the data itself (the XML document) separate from the formatting
and processing information (the style sheet, or in this chapter, the HTML page) In particular, the second form makes it easier to maintain the XML document, espe-cially when a single document is displayed in several different HTML pages There-fore, you’ll see only the second form of data island in the examples in this book You should assign to the data island’s ID attribute a unique identifier, which you’ll use to access the XML document from within the HTML page (In the
previous example, I assigned ID the value dsoInventory.)
Trang 8302 XML Step by Step
In the second form of data island, you assign to the SRC attribute the URL of the file containing the XML data You can use a fully qualified URL, as in this example:
<XML
ID=”dsoInventory”
SRC=”http://www.mjyOnline.com/documents/Inventory.xml”>
</XML>
More commonly, however, you use a partial URL that specifies a location rela-tive to the location of the HTML page containing the data island For example, the SRC attribute in the following data island indicates that Inventory.xml is in the same folder as the HTML page:
<XML ID=”dsoInventory” SRC=”Inventory.xml”></XML>
Relative URLs are more common because the XML document is typically con-tained in the same folder as the HTML page, or perhaps in one of its subfolders
How the XML Data Is Stored
When Internet Explorer opens the HTML page, its XML processor reads and parses the XML document Internet Explorer also creates a programming object
known as a Data Source Object (DSO), which stores, or caches, the XML data
and provides access to this data The DSO stores the XML data as a record set—that is, as a collection of records and their fields For example, if you were
to include the Inventory.xml document (shown in Listing 10-1) in a page as a data island, the DSO would store each BOOK element as a record, and each of the child elements within BOOK (TITLE, AUTHOR, and so on) as a field When you bind an HTML element to an XML element, the DSO automatically supplies the value of the XML element and handles all the details The DSO also lets you directly access and manipulate the stored record set through a collection
of methods, properties, and events Methods are functions that you can call
from the page to access or modify the record set (For example, you can use
methods to move through the records.) Properties are current feature settings
that you can read and sometimes modify from the page (For example, you can
read a property that tells you if you’ve reached the last record.) And events are
occurrences (such as a record value changing) that you can handle from a script function that you include in the page
In the page, the identifier that you assigned to the ID attribute in the data island represents the DSO (In the example in the previous section, this identifier is
dsoInventory.)
Trang 9304 XML Step by Step
ment that contains a hierarchical record set (a more complex record structure that I’ll describe later in the chapter) The following sections explain both ap-proaches to displaying record sets
Using a Single HTML Table to Display a Simple Record Set
You can use a single HTML TABLE element to display an XML document in which the data is organized as a simple record set—that is, an XML document organized as follows:
■ The root element contains a series of record elements, all of the same
type (In this chapter, I sometimes call record elements simply
records.)
■ Each record element contains the same set of field elements, and in
this set a given element type occurs only once (In this chapter, I
sometimes call field elements simply fields.)
■ Each field element contains character data only
note
If a record element contains more than one child element of the same type, or
if a child element contains nested elements, the DSO interprets the child ele-ment as a nested record or record set, not as a field You’ll learn how to dis-play nested records in “Using a Nested Table to Disdis-play a Hierarchical Record Set” later in the chapter
An example of this type of XML document is Inventory.xml, which you’ve seen
in previous chapters It is given again in Listing 10-1 for convenient reference (You’ll find a copy of this file on the companion CD.) In this document, the root element (INVENTORY) contains a set of eight record elements (BOOK ele-ments), and each record element has the same set of field elements, which con-tain only character data (TITLE, AUTHOR, BINDING, PAGES, and PRICE)
Inventory.xml
<?xml version="1.0"?>
<! File Name: Inventory.xml >
<INVENTORY>
<BOOK>
Trang 10Chapter 10 Displaying XML Documents Using Data Binding 305
<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>