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

XML Step by Step- P19 pot

15 144 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

Tiêu đề Displaying Xml Documents Using Data Binding
Trường học University of XYZ
Chuyên ngành Computer Science
Thể loại Thesis
Năm xuất bản 2023
Thành phố New York
Định dạng
Số trang 15
Dung lượng 415,48 KB

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

Nội dung

Listing 10-9 is the same HTML page as the one in Listing 10-2, except that at the beginning of each row is an additional cell TD element that contains an IMG element rather than a SPAN..

Trang 1

<TR ALIGN="center">

<TD><IMG DATAFLD="COVERIMAGE"></TD>

<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-9.

Listing 10-8 is an XML document that contains a field named COVERIMAGE

in each BOOK record Each COVERIMAGE field contains the URL of a graph-ics file that stores an image of the book’s cover Listing 10-9 is the same HTML page as the one in Listing 10-2, except that at the beginning of each row is an additional cell (TD element) that contains an IMG element rather than a SPAN The IMG element is bound to the COVERIMAGE field in the XML document, and therefore displays the cover image for each book, as you can see here:

Trang 2

You can experiment with binding some of the other HTML elements listed in Table 10-1 to learn about their bound properties and how the elements use the data in the XML fields to which you bind them.

Rendering HTML

By default, if an XML field’s character data happens to include HTML markup, the HTML element bound to that field treats and displays the markup charac-ters as literal text Consider, for example, the following SPAN element, which is bound to the AUTHOR-BIO XML field:

<SPAN DATASRC=”#dsoInventory” DATAFLD=”AUTHOR-BIO”></SPAN>

If the AUTHOR-BIO field contains an italic (I) element, like this:

<AUTHOR-BIO>

Henry James was an American author who lived from 1843 to

1916, and wrote &lt;I>The Bostonians&lt;/I> and many other works of psychologically realistic fiction

</AUTHOR-BIO>

the SPAN element would treat the HTML markup characters as literal text and would display the field as follows:

With some of the bindable HTML elements, such as SPANs, you can assign the DATAFORMATAS attribute the value HTML to cause the element to process any HTML markup included in the field’s text, rather than simply treating it as literal characters Assume, for example, that you defined the example SPAN ele-ment shown previously like this:

<SPAN DATASRC=”#dsoInventory” DATAFLD=”AUTHOR-BIO”

DATAFORMATAS=”HTML”></SPAN>

It would then render the text within the I element in italics, like this:

Note that without the DATAFORMATAS="HTML" attribute specification, Internet Explorer preserves all white space in the XML element’s content With this attribute setting, however, the browser handles white space in the XML

Trang 3

ment just as it handles white space contained in text within HTML elements in a page That is, it replaces sequences of white space characters with a single space character, and it discards leading or trailing white space.

note

Assigning DATAFORMATAS its default value, TEXT, has the same effect as sim-ply omitting the attribute—that is, it causes HTML markup characters to be treated as literal text.

To find out which elements you can use to render HTML via the

DATAFORMATAS="HTML" attribute setting, see Table 10-1 These elements have a “Yes” in the next-to-last column (labeled “Render HTML contained in XML field?”).

Inserting and rendering HTML in XML fields is useful for modifying the for-matting of portions of the text (using I or B elements, for example) and for in-cluding HTML elements such as hyperlinks in the text Although formatting XML text by including HTML markup in the XML violates the basic tenet of separating data and formatting, when you use data binding, this technique

might be the only feasible way to modify the formatting or include HTML ele-ments within a field (In contrast, if you use the other methods discussed in this book for displaying XML, you can generally modify formatting or insert ele-ments within an XML element by including child eleele-ments and processing them appropriately.)

When you add HTML markup to an XML field, you can’t insert a left angle bracket (<) or an ampersand (&) literally in the text (Recall that these charac-ters are illegal within an element’s character data.) However, you can insert them

using the predefined entity references &lt; and &amp; Another option, which

makes the HTML more readable to humans and is especially useful for a large block of HTML, is to include the markup within a CDATA section, as discussed

in Chapter 4.

note

Another way to include HTML elements in an XML document is to identify the

elements using the html namespace prefix, and display the document using a

cascading style sheet, as discussed in “Inserting HTML Elements into XML Documents” in Chapter 9.

Trang 4

<!ELEMENT BOOK (TITLE, AUTHOR, BINDING, PAGES, PRICE)> <!ELEMENT TITLE (#PCDATA)>

<!ELEMENT AUTHOR (#PCDATA)>

<!ELEMENT BINDING (#PCDATA)>

<!ELEMENT PAGES (#PCDATA)>

<!ELEMENT PRICE (#PCDATA)>

]

>

These element declarations can be explained, using the language of record sets and data binding, as follows:

<!ELEMENT INVENTORY (CATEGORY*)> The document contains zero

or more CATEGORY records.

<!ELEMENT CATEGORY (CATNAME, BOOK*)> Each CATEGORY

record contains one CATNAME field, followed by zero or more nested BOOK records.

<!ELEMENT BOOK (TITLE, AUTHOR, BINDING, PAGES, PRICE)>

Each nested BOOK record contains exactly one of each of the following fields, in the order listed: TITLE, AUTHOR, BINDING, PAGES, and PRICE.

<!ELEMENT TITLE (#PCDATA)> and the remaining declarations Each

of the fields in a BOOK record contains character data only.

3 To reflect the new filename you’re going to assign, change the comment at the beginning of the document from:

<! File Name: Inventory Hierarchy.xml >

to this:

<! File Name: Inventory Hierarchy Valid.xml >

4 Use your text editor’s Save As command to save a copy of the modified document under the filename Inventory Hierarchy Valid.xml.

Listing 10-10 shows the complete XML document (You’ll find a copy of this listing on the companion CD under the filename Inventory Hierarchy Valid.xml.)

Inventory Hierarchy Valid.xml

<?xml version="1.0"?>

<! File Name: Inventory Hierarchy Valid.xml >

<!DOCTYPE INVENTORY

[

Trang 5

<!ELEMENT INVENTORY (CATEGORY*)>

<!ELEMENT CATEGORY (CATNAME, BOOK*)>

<!ELEMENT CATNAME (#PCDATA)>

<!ELEMENT BOOK (TITLE, AUTHOR, BINDING, PAGES, PRICE)>

<!ELEMENT TITLE (#PCDATA)>

<!ELEMENT AUTHOR (#PCDATA)>

<!ELEMENT BINDING (#PCDATA)>

<!ELEMENT PAGES (#PCDATA)>

<!ELEMENT PRICE (#PCDATA)>

]

>

<INVENTORY>

<CATEGORY>

<CATNAME>Middle Ages</CATNAME>

<BOOK>

<TITLE>The Canterbury Tales</TITLE>

<AUTHOR>Geoffrey Chaucer</AUTHOR>

<BINDING>hardcover</BINDING>

<PAGES>692</PAGES>

<PRICE>$18.95</PRICE>

</BOOK>

<BOOK>

<TITLE>Piers Plowman</TITLE>

<AUTHOR>William Langland</AUTHOR>

<BINDING>trade paperback</BINDING>

<PAGES>385</PAGES>

<PRICE>$10.95</PRICE>

</BOOK>

</CATEGORY>

<CATEGORY>

<CATNAME>Renaissance</CATNAME>

<BOOK>

<TITLE>The Blazing World</TITLE>

<AUTHOR>Margaret Cavendish</AUTHOR>

<BINDING>trade paperback</BINDING>

<PAGES>225</PAGES>

<PRICE>$8.79</PRICE>

</BOOK>

<BOOK>

<TITLE>Oroonoko</TITLE>

<AUTHOR>Aphra Behn</AUTHOR>

Trang 6

<BINDING>mass market paperback</BINDING>

<PAGES>295</PAGES>

<PRICE>$4.95</PRICE>

</BOOK>

<BOOK>

<TITLE>Doctor Faustus</TITLE>

<AUTHOR>Christopher Marlowe</AUTHOR>

<BINDING>hardcover</BINDING>

<PAGES>472</PAGES>

<PRICE>$15.95</PRICE>

</BOOK>

</CATEGORY>

<CATEGORY>

<CATNAME>18th Century</CATNAME>

<BOOK>

<TITLE>Gulliver's Travels</TITLE>

<AUTHOR>Jonathan Swift</AUTHOR>

<BINDING>hardcover</BINDING>

<PAGES>324</PAGES>

<PRICE>$11.89</PRICE>

</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>

Trang 7

</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>

<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-10.

Trang 8

5 In your text editor, open the Inventory Hierarchy.htm page you created earlier in this chapter (You’ll find this document in Listing 10-6 and on the companion CD.)

6 Change the SRC attribute of the page’s data island so that it links the new XML document you just created That is, change it from:

<XML ID=”dsoInventory” SRC=”Inventory Hierarchy.xml”></XML>

to this:

<XML ID=”dsoInventory” SRC=”Inventory Hierarchy Valid.xml”></XML>

7 To reflect the new filename you’re going to assign, change the comment at the beginning of the page from:

<! File Name: Inventory Hierarchy.htm >

to this:

<! File Name: Inventory Hierarchy Valid.htm >

8 Use your text editor’s Save As command to save a copy of the modified page under the filename Inventory Hierarchy Valid.htm.

Listing 10-11 shows the complete HTML page (You’ll find a copy of this list-ing on the companion CD under the filename Inventory Hierarchy Valid.htm.)

Inventory Hierarchy Valid.htm

<! File Name: Inventory Hierarchy Valid.htm >

<HTML>

<HEAD>

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

</HEAD>

<BODY>

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

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

<THEAD>

<TH>Classic English Literature</TH>

</THEAD>

Trang 9

<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-11.

Trang 10

9 Open the page in Internet Explorer.

It should look like this:

10 If the data doesn’t appear, the document must contain a well-formedness

or validity error To locate the error, use the DTD validity-checking page given in “Checking an XML Document for Validity Using a DTD” on page 396.

Binding HTML Elements to XML Attributes

In the example XML documents you’ve seen so far, none of the elements has in-cluded attributes Attributes add a bit of complexity to data binding, although you can bind to elements that include attributes, as well as to the attributes themselves When you use data binding, an attribute is treated essentially as if it were a child element.

With a record element, this treatment makes it easy to access (or to simply

ig-nore) an attribute For example, the following BOOK record contains an

at-tribute named InStock:

<BOOK InStock=”yes”>

<TITLE>The Adventures of Huckleberry Finn</TITLE>

<AUTHOR>Mark Twain</AUTHOR>

<BINDING>mass market paperback</BINDING>

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