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

XML Step by Step- P4 ppt

15 175 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 đề XML Step by Step
Tác giả Mark Twain, Walt Whitman, Washington Irving, Nathaniel Hawthorne, Herman Melville, Henry James
Trường học Standard University
Chuyên ngành Computer Science
Thể loại Bài tập tốt nghiệp
Năm xuất bản 2023
Thành phố New York
Định dạng
Số trang 15
Dung lượng 343,1 KB

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

Nội dung

Add the following processing instruction to the end of the document prolog, directly above the INVENTORY element:... Chapter 2 Creating and Displaying Your First XML Document 39 This pro

Trang 1

38 XML Step by Step

Display the AUTHOR element in bold (font-weight:bold).

Do not display the PAGES element (display:none).

Inventory02.css

/* File Name: Inventory02.css */

BOOK {display:block;

margin-top:12pt;

font-size:10pt}

TITLE {display:block;

font-size:12pt;

font-weight:bold;

font-style:italic}

AUTHOR {display:block;

margin-left:15pt;

font-weight:bold}

BINDING {display:block;

margin-left:15pt}

PAGES {display:none}

PRICE {display:block;

margin-left:15pt}

Listing 2-4.

9 In your text editor, open the Inventory.xml document Add the following processing instruction to the end of the document prolog, directly above the INVENTORY element:

Trang 2

Chapter 2 Creating and Displaying Your First XML Document 39

<?xml-stylesheet type=”text/css” href=”Inventory02.css”?>

This processing instruction links the new CSS you just created to the

XML document

10 To reflect the new filename you’re going to assign, change the comment

near the beginning of the document from

<! File Name: Inventory.xml >

to

<! File Name: Inventory02.xml >

Listing 2-5 shows the complete XML document (You’ll find a copy of this

listing on the companion CD under the filename Inventory02.xml.)

11 Use your text editor’s Save As command to save a copy of the modified

document under the filename Inventory02.xml Be sure to save it in the

same file folder in which you saved Inventory02.css

Inventory02.xml

<?xml version=”1.0"?>

<! File Name: Inventory02.xml >

<?xml-stylesheet type=”text/css” href=”Inventory02.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>

Trang 3

40 XML Step by Step

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

Listing 2-5.

Trang 4

Chapter 2 Creating and Displaying Your First XML Document 41

12 In Windows Explorer or in a folder window, double-click the

Inventory02.xml filename to open it

Internet Explorer will open the Inventory02.xml document and display it

according to the rules in the linked Inventory02.css style sheet, as shown

here (only the first six books are shown; scrolling down would reveal the

last two books):

tip

Part 3 of the book provides complete instructions for displaying XML documents

on the Web I’ll cover cascading style sheets, such as the one you created here,

in Chapters 8 and 9 I’ll cover XSLT style sheets in Chapter 12 You’ll learn alternative methods for displaying XML documents on the Web in Chapters

10 and 11

Trang 6

PART 2

Creating XML Documents

Trang 8

Creating Well-Formed

XML Documents

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 docu-ment, 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 use predefined ones.) And you’ll have

no problem handling and displaying any well-formed XML document in

Microsoft Internet Explorer

In Chapters 5 through 7, you’ll learn how to create a valid XML document: a

document that is not only well-formed but that also conforms to a more rigid set of constraints When you create a valid XML document, in addition to add-ing the elements and data, you must formally define the document’s content and structure, either in a document type definition (DTD) or in an XML schema file

CHAPTER

3

Trang 9

46 XML Step by Step

note

Permitting XML documents to be merely well-formed, rather than requiring them to be valid, is an important concession that the XML specification makes

to enhance XML as a universal markup language for the Web Merely well-formed documents are simpler to create than valid ones, making XML as easy for Web developers to use as the more familiar HTML Also, merely well-formed documents are often easier to transmit over the Web than valid documents because they are less likely to rely on external files; and a non-validating processor can often ignore external files, making the document more suitable for Web browsing Finally, because an XML processor isn’t required to check for validity, writing a conforming XML processor that can handle any XML document is simpler, encouraging the proliferation of universal XML processors

on the Web

In Chapter 5, however, you’ll learn that there are some important advantages

to making documents valid, especially for 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 (spaces, tabs, or line breaks) Here’s an example of a well-formed XML document that shows the dif-ferent document parts and the items you can add to each part:

Trang 10

Chapter 3 Creating Well-Formed XML Documents 47

Prolog

Document

element

(Root

element)

Items

following

document

element

XML declaration Comment White space Processing instruction

Processing instruction White space Comment

White space

White space

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'?>

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

Trang 11

48 XML Step by Step

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

Trang 12

Chapter 3 Creating Well-Formed XML Documents 49

<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 first line of the example document consists of the XML declaration

Al-though technically the XML declaration is optional, the XML specification rec-ommends including it In addition to making the document self-identifying as XML and specifying the XML version number (which will become important if later versions are developed), it provides a place for including two optional

pieces of information: the encoding declaration and the standalone document declaration The encoding declaration specifies the encoding scheme used for the

characters in the document, and is discussed in the sidebar “Characters,

Encod-ing, and Languages” on page 77 The standalone document declaration indicates

whether the document contains external markup declarations (explained in

Chapter 5) that affect the content of the document It’s covered in the sidebar

“The standalone Document Declaration” on page 159 If you include an XML

declaration, it must appear at the very beginning of the document (You’re not permitted to include even white space characters before the XML declaration.) The version number in the XML declaration 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 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 two blank lines in the prolog and two more blank lines that follow the document element, each labeled “White space.” White

space consists of one or more space, tab, carriage-return, or line feed characters

Trang 13

50 XML Step by Step

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 pro-cessing instructions—and also in many places within markup—for instance, the

space between xml and version at the beginning of the XML declaration in the

example document The processor simply ignores white space unless it’s within

an element (that is, between an element start-tag and a matching end-tag, but not within markup) In that case, the processor passes the white space to the ap-plication as part of the character data of the element that contains the white space For details on the way white space is handled in elements, see the sidebar

“White Space in Elements” on page 56

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 example 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 must either contain a docu-ment type declaration or be processed using a separate XML schema file A document type declaration is an additional component, not included in the example document in Listing 3-1, which you can place anywhere in the prolog (outside other markup) following the XML declaration A document type dec-laration contains a document type definition (DTD) that defines the content and structure of a valid XML document

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 hierarchy, 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 >

Trang 14

Chapter 3 Creating Well-Formed XML Documents 51

A Minimalist XML Document

The prolog in the 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 as shown here:

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

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