1. Trang chủ
  2. » Công Nghệ Thông Tin

XML Step by Step- P18 pps

15 116 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 15
Dung lượng 334,58 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Chapter 10 Displaying XML Documents Using Data Binding 315Using a Nested Table to Display a Hierarchical Record Set In the previous sections, you learned how to use a single table to dis

Trang 1

Chapter 10 Displaying XML Documents Using Data Binding 313

<PAGES>256</PAGES>

<PRICE>$4.95</PRICE>

</BOOK>

<BOOK>

<TITLE>Roughing It</TITLE>

<AUTHOR>Mark Twain</AUTHOR>

<BINDING>mass market paperback</BINDING>

<PAGES>324</PAGES>

<PRICE>$5.25</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>

Listing 10-3.

Inventory Big Table.htm

<! File Name: Inventory Big Table.htm >

<HTML>

<HEAD>

<TITLE>Book Inventory</TITLE>

</HEAD>

<BODY>

<XML ID="dsoInventory" SRC="Inventory Big.xml"></XML>

<H2>Book Inventory</H2>

Trang 2

314 XML Step by Step

<BUTTON ONCLICK="InventoryTable.firstPage()">

|&lt; First Page

</BUTTON>

&nbsp;&nbsp;

<BUTTON ONCLICK="InventoryTable.previousPage()">

&lt; Previous Page

</BUTTON>

&nbsp;&nbsp;

<BUTTON ONCLICK="InventoryTable.nextPage()">

Next Page &gt;

</BUTTON>

&nbsp;&nbsp;

<BUTTON ONCLICK="InventoryTable.lastPage()">

Last Page &gt;|

</BUTTON>

<P>

<TABLE ID="InventoryTable" DATASRC="#dsoInventory"

DATAPAGESIZE="5" BORDER="1" CELLPADDING="5" WIDTH="100%"> <THEAD>

<TH>Title</TH>

<TH>Author</TH>

<TH>Binding</TH>

<TH>Pages</TH>

<TH>Price</TH>

</THEAD>

<TR ALIGN="center">

<TD><SPAN DATAFLD="TITLE"

STYLE="font-style:italic"></SPAN></TD>

<TD><SPAN DATAFLD="AUTHOR"></SPAN></TD>

<TD><SPAN DATAFLD="BINDING"></SPAN></TD>

<TD><SPAN DATAFLD="PAGES"></SPAN></TD>

<TD><SPAN DATAFLD="PRICE"></SPAN></TD>

</TR>

</TABLE>

</BODY>

</HTML>

Listing 10-4.

Trang 3

Chapter 10 Displaying XML Documents Using Data Binding 315

Using a Nested Table to Display a Hierarchical Record Set

In the previous sections, you learned how to use a single table to display an XML document structured as a simple record set (A simple record set is described at the beginning of the earlier section “Using a Single HTML Table to Display a Simple Record Set.”) You’ll now learn how to use nested tables to display an XML document whose elements are structured in a hierarchical record set.

In a hierarchical record set, each record can contain, in addition to an optional fixed set of fields, zero or more nested records (see the following Note) Listing 10-5 shows an example of an XML document structured as a hierarchical

record set (You’ll find a copy of this listing on the companion CD under the filename Inventory Hierarchy.xml.) In this document, the root element (INVEN-TORY) contains a series of CATEGORY records Each CATEGORY record be-gins with a single CATNAME field, which contains character data only, and then has two or more nested BOOK records Each nested BOOK record has ex-actly five fields (TITLE, AUTHOR, BINDING, PAGES, and PRICE).

note

As a general rule, if an element contains one or more children (or attributes, as

explained later), or if the same element type occurs more than once within a given

parent, the DSO interprets the element or elements as a record (or set of records), rather than as a field (or set of fields) In Inventory Hierarchy.xml, the CATEGORY and BOOK elements meet both of these criteria, so they are clearly considered to be records (The CATNAME, TITLE, AUTHOR, BINDING, PAGES, and PRICE elements meet neither criteria, so they are considered to be fields.) A TABLE element is the only type of HTML element that you can bind to an XML record.

Inventory Hierarchy.xml

<?xml version="1.0"?>

<! File Name: Inventory Hierarchy.xml >

<INVENTORY>

<CATEGORY>

<CATNAME>Middle Ages</CATNAME>

<BOOK>

<TITLE>The Canterbury Tales</TITLE>

<AUTHOR>Geoffrey Chaucer</AUTHOR>

<BINDING>hardcover</BINDING>

<PAGES>692</PAGES>

Trang 4

Chapter 10 Displaying XML Documents Using Data Binding 317

</BOOK>

<BOOK>

<TITLE>The History of Tom Jones: A Foundling</TITLE>

<AUTHOR>Henry Fielding</AUTHOR>

<BINDING>hardcover</BINDING>

<PAGES>438</PAGES>

<PRICE>$16.95</PRICE>

</BOOK>

<BOOK>

<TITLE>Love in Excess</TITLE>

<AUTHOR>Eliza Haywood</AUTHOR>

<BINDING>trade paperback</BINDING>

<PAGES>429</PAGES>

<PRICE>$12.95</PRICE>

</BOOK>

<BOOK>

<TITLE>Tristram Shandy</TITLE>

<AUTHOR>Laurence Sterne</AUTHOR>

<BINDING>hardcover</BINDING>

<PAGES>322</PAGES>

<PRICE>$9.49</PRICE>

</BOOK>

</CATEGORY>

<CATEGORY>

<CATNAME>19th Century</CATNAME>

<BOOK>

<TITLE>Dracula</TITLE>

<AUTHOR>Bram Stoker</AUTHOR>

<BINDING>hardcover</BINDING>

<PAGES>395</PAGES>

<PRICE>$17.95</PRICE>

</BOOK>

<BOOK>

<TITLE>Great Expectations</TITLE>

<AUTHOR>Charles Dickens</AUTHOR>

<BINDING>mass market paperback</BINDING>

<PAGES>639</PAGES>

<PRICE>$6.95</PRICE>

</BOOK>

<BOOK>

<TITLE>Percival Keene</TITLE>

<AUTHOR>Frederick Marryat</AUTHOR>

Trang 5

318 XML Step by Step

<BINDING>trade paperback</BINDING>

<PAGES>425</PAGES>

<PRICE>$12.89</PRICE>

</BOOK>

<BOOK>

<TITLE>Treasure Island</TITLE>

<AUTHOR>Robert Louis Stevenson</AUTHOR>

<BINDING>trade paperback</BINDING>

<PAGES>283</PAGES>

<PRICE>$11.85</PRICE>

</BOOK>

<BOOK>

<TITLE>Wuthering Heights</TITLE>

<AUTHOR>Emily Bronte</AUTHOR>

<BINDING>hardcover</BINDING>

<PAGES>424</PAGES>

<PRICE>$12.95</PRICE>

</BOOK>

</CATEGORY>

</INVENTORY>

Listing 10-5.

Listing 10-6 contains an HTML page that uses a nested table to display the hierar-chical record structure of the XML document in Listing 10-5 (You’ll find a copy of Listing 10-6 on the companion CD under the filename Inventory Hierarchy.htm.)

Inventory Hierarchy.htm

<! File Name: Inventory Hierarchy.htm >

<HTML>

<HEAD>

<TITLE>Inventory of Classic English Literature</TITLE>

</HEAD>

<BODY>

<XML ID="dsoInventory" SRC="Inventory Hierarchy.xml"></XML> <TABLE DATASRC="#dsoInventory" BORDER="1">

<THEAD>

<TH>Classic English Literature</TH>

Trang 6

Chapter 10 Displaying XML Documents Using Data Binding 319

</THEAD>

<TR>

<TD><SPAN DATAFLD="CATNAME"></SPAN></TD>

</TR>

<TR>

<TD>

<TABLE DATASRC="#dsoInventory" DATAFLD="BOOK"

BORDER="0" CELLSPACING="10">

<THEAD>

<TH WIDTH="25%">Title</TH>

<TH WIDTH="25%">Author</TH>

<TH WIDTH="25%">Binding</TH>

<TH WIDTH="10%">Pages</TH>

<TH WIDTH="15%">Price</TH>

</THEAD>

<TR ALIGN="CENTER">

<TD WIDTH="25%">

<SPAN DATAFLD="TITLE"

STYLE="font-style:italic"></SPAN>

</TD>

<TD WIDTH="25%">

<SPAN DATAFLD="AUTHOR"></SPAN>

</TD>

<TD WIDTH="25%">

<SPAN DATAFLD="BINDING"></SPAN>

</TD>

<TD WIDTH="10%">

<SPAN DATAFLD="PAGES"></SPAN>

</TD>

<TD WIDTH="15%">

<SPAN DATAFLD="PRICE"></SPAN>

</TD>

</TR>

</TABLE>

</TD>

</TR>

</TABLE>

</BODY>

</HTML>

Listing 10-6.

Trang 7

320 XML Step by Step

In Listing 10-6, the outer table is bound to the XML document, as you can see

in its start-tag:

<TABLE DATASRC=”#dsoInventory” BORDER=”1">

The outer table includes a heading (a THEAD element displaying “Classic En-glish Literature”), plus two table rows (two TR elements) The browser repeats the two rows for each top-level record (that is, for each CATEGORY record) The first row displays the CATNAME field So far, everything works just like the example table that displays a simple record set, given previously in Listing 10-2 However, the second row, rather than displaying fields, contains a nested table that displays the contents of each nested BOOK record within the current category Here’s the markup for only the nested table:

<TABLE DATASRC="#dsoInventory" DATAFLD="BOOK"

BORDER="0" CELLSPACING="10">

<THEAD>

<TH WIDTH="25%">Title</TH>

<TH WIDTH="25%">Author</TH>

<TH WIDTH="25%">Binding</TH>

<TH WIDTH="10%">Pages</TH>

<TH WIDTH="15%">Price</TH>

</THEAD>

<TR ALIGN="CENTER">

<TD WIDTH="25%">

<SPAN DATAFLD="TITLE"

STYLE="font-style:italic"></SPAN>

</TD>

<TD WIDTH="25%">

<SPAN DATAFLD="AUTHOR"></SPAN>

</TD>

<TD WIDTH="25%">

<SPAN DATAFLD="BINDING"></SPAN>

</TD>

<TD WIDTH="10%">

<SPAN DATAFLD="PAGES"></SPAN>

</TD>

<TD WIDTH="15%">

<SPAN DATAFLD="PRICE"></SPAN>

</TD>

</TR>

</TABLE>

Trang 8

322 XML Step by Step

In this case, you could use an additional nested table to display all the authors for each BOOK element, employing the same basic techniques shown for a singly nested table (Although in the example XML document each book has only a single author, an additional nested table would display multiple authors,

if present For the additional nested table, you would set DATAFLD equal

to "AUTHOR".)

Using Single-Record Data Binding

Single-record data binding refers to binding an HTML element that isn’t a table

and isn’t included in a bound table The HTML element—for example, a SPAN,

BUTTON, or LABEL element—is bound to an individual XML field (not to a

record) The HTML element then automatically displays the content of the XML field to which you bind it For example, the following HTML SPAN ele-ment is bound to the TITLE field in the XML docuele-ment that’s accessed through

the dsoInventory data island:

<SPAN DATASRC=”#dsoInventory” DATAFLD=”TITLE”></SPAN>

To use single-record data binding as described in this section, the XML docu-ment must be organized as a simple record set I gave a description of a simple record set at the beginning of the section “Using a Single HTML Table to Dis-play a Simple Record Set” on page 302.

Because the HTML element doesn’t have multiple parts like a table, however, it can display the field’s value for only one record at a time For example, if you wanted to display the book information in the Inventory Big.xml document (given in Listing 10-3), you could bind a SPAN element to each of the fields of a BOOK record, as in the following HTML page:

<HTML>

<HEAD>

<TITLE>Book Description</TITLE>

</HEAD>

<BODY>

<XML ID="dsoInventory" SRC="Inventory Big.xml"></XML>

<H2>Book Description</H2>

<SPAN STYLE="font-style:italic">Title: </SPAN>

<SPAN STYLE="font-weight:bold" DATASRC="#dsoInventory"

DATAFLD="TITLE"></SPAN>

<BR>

Trang 9

Chapter 10 Displaying XML Documents Using Data Binding 323

<SPAN STYLE="font-style:italic">Author: </SPAN>

<SPAN DATASRC="#dsoInventory" DATAFLD="AUTHOR"></SPAN>

<BR>

<SPAN STYLE="font-style:italic">Binding type: </SPAN>

<SPAN DATASRC="#dsoInventory" DATAFLD="BINDING"></SPAN>

<BR>

<SPAN STYLE="font-style:italic">Number of pages: </SPAN>

<SPAN DATASRC="#dsoInventory" DATAFLD="PAGES"></SPAN>

<BR>

<SPAN STYLE="font-style:italic">Price: </SPAN>

<SPAN DATASRC="#dsoInventory" DATAFLD="PRICE"></SPAN>

</BODY>

</HTML>

This page, however, would display only the first BOOK record in the XML

document, as shown here:

To circumvent this limitation, you need to use the technique for navigating

through records described in the next section.

Navigating Through the Records

With single-record data binding, the bound HTML elements can display only

one record at a time The record that they display is known as the current

record (Hence, an alternative term for single-record data binding is current-record binding.) Initially, the current current-record is the first (or only) current-record in

the document.

Trang 10

324 XML Step by Step

The DSO associated with the XML document provides a set of methods that you can call to navigate through the records These methods belong to the

recordset member object of the DSO, and are listed in the following table Note

that the example calls in the last column assume that the HTML page contains

an XML data island with the ID dsoInventory.

DSO recordset Changes the Example call method current record to

moveFirst The first record in dsoInventory.recordset.moveFirst()

the document

movePrevious The previous record dsoInventory.recordset.movePrevious()

moveNext The next record dsoInventory.recordset.moveNext()

moveLast The last record in dsoInventory.recordset.moveLast()

the document

move The record with the dsoInventory.recordset.move(5)

specified number (Moves to the sixth record Records

are numbered starting with zero.)

note

The recordset member object of the DSO conforms to a standard data

access technology that Microsoft calls ActiveX Data Objects (ADO) You

can use the general-purpose ADO recordset object with a variety of different

data sources, in addition to the XML DSO described in this chapter For

general information on ADO and the ADO recordset object, see the topic

“Microsoft ActiveX Data Objects (ADO)” in the MSDN Library on the Web

at http://msdn.microsoft.com/library/ See also Microsoft’s home page for information on ADO at http://www.microsoft.com/data/ado/.

You can call any of these methods from a script that you write (as discussed later in the chapter) The easiest way to call one of them, however, is by

assigning the method to the ONCLICK attribute of a BUTTON element, as shown here:

<BUTTON ONCLICK=”dsoInventory.recordset.moveFirst()”>

First Record

</BUTTON>

This element creates a push button that displays the text “First Record.” When the user clicks the button, the method assigned to the ONCLICK attribute,

dsoInventory.recordset.moveFirst, gets called.

Ngày đăng: 03/07/2014, 07:20