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

XML Step by Step- P24 pdf

15 239 0

Đ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 355,6 KB

Nội dung

430 XML Step by Step Author: Binding type: Number of pages: Price: The xsl:for-each element has two main effects: ■ The browser carries out the instructions within the x

Trang 1

Chapter 12 Displaying XML Documents Using XSLT Style Sheets 427

<TITLE>Billy Budd</TITLE>

<AUTHOR>

<FIRSTNAME>Herman</FIRSTNAME>

<LASTNAME>Melville</LASTNAME>

</AUTHOR>

<BINDING>mass market paperback</BINDING>

<PAGES>195</PAGES>

<PRICE>$4.49</PRICE>

</BOOK>

<BOOK>

<TITLE>A Connecticut Yankee in King Arthur's Court</TITLE> <AUTHOR>

<FIRSTNAME>Mark</FIRSTNAME>

<LASTNAME>Twain</LASTNAME>

</AUTHOR>

<BINDING>mass market paperback</BINDING>

<PAGES>385</PAGES>

<PRICE>$5.49</PRICE>

</BOOK>

<BOOK>

<TITLE>Joan of Arc</TITLE>

<AUTHOR>

<FIRSTNAME>Mark</FIRSTNAME>

<LASTNAME>Twain</LASTNAME>

</AUTHOR>

<BINDING>trade paperback</BINDING>

<PAGES>465</PAGES>

<PRICE>$6.95</PRICE>

</BOOK>

<BOOK>

<TITLE>Leaves of Grass</TITLE>

<AUTHOR>

<FIRSTNAME>Walt</FIRSTNAME>

<LASTNAME>Whitman</LASTNAME>

</AUTHOR>

<BINDING>hardcover</BINDING>

<PAGES>462</PAGES>

<PRICE>$7.75</PRICE>

</BOOK>

<BOOK>

<TITLE>The Legend of Sleepy Hollow</TITLE>

<AUTHOR>

Trang 2

Chapter 12 Displaying XML Documents Using XSLT Style Sheets 429

</AUTHOR>

<BINDING>mass market paperback</BINDING>

<PAGES>256</PAGES>

<PRICE>$4.95</PRICE>

</BOOK>

<BOOK>

<TITLE>Roughing It</TITLE>

<AUTHOR>

<FIRSTNAME>Mark</FIRSTNAME>

<LASTNAME>Twain</LASTNAME>

</AUTHOR>

<BINDING>mass market paperback</BINDING>

<PAGES>324</PAGES>

<PRICE>$5.25</PRICE>

</BOOK>

<BOOK>

<TITLE>The Scarlet Letter</TITLE>

<AUTHOR>

<FIRSTNAME>Nathaniel</FIRSTNAME>

<LASTNAME>Hawthorne</LASTNAME>

</AUTHOR>

<BINDING>trade paperback</BINDING>

<PAGES>253</PAGES>

<PRICE>$4.25</PRICE>

</BOOK>

<BOOK>

<TITLE>The Turn of the Screw</TITLE>

<AUTHOR>

<FIRSTNAME>Henry</FIRSTNAME>

<LASTNAME>James</LASTNAME>

</AUTHOR>

<BINDING>trade paperback</BINDING>

<PAGES>384</PAGES>

<PRICE>$3.35</PRICE>

</BOOK>

</INVENTORY>

Listing 12-4.

The template in the style sheet of Listing 12-3 contains the following for-each

element:

<xsl:for-each select="INVENTORY/BOOK">

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

Trang 3

430 XML Step by Step

<xsl:value-of select="TITLE"/><BR/>

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

<xsl:value-of select="AUTHOR"/><BR/>

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

<xsl:value-of select="BINDING"/><BR/>

<SPAN STYLE="font-style:italic">Number of pages: </SPAN> <xsl:value-of select="PAGES"/><BR/>

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

<xsl:value-of select="PRICE"/><P/>

</xsl:for-each>

The xsl:for-each element has two main effects:

The browser carries out the instructions within the xsl:for-each

ele-ment once for every XML eleele-ment (or other type of node) that is contained in the node-set described by the location path assigned to

the xsl:for-each element’s select attribute In this example, the

in-structions are repeated once for each BOOK element within the IN-VENTORY document element The location path assigned to the

xsl:for-each element’s select attribute works just like a location path assigned to the xsl:value-of element’s select attribute, except that you

typically assign a location path that describes a node-set that in-cludes more than one node

Each time the browser carries out the instructions within the xsl:for-each element, one of the nodes contained in the node-set assigned to the select attribute becomes the current context node within the scope of the xsl:for-each element The nodes become the current

context node in the order of their appearance in the XML docu-ment In the example style sheet, each BOOK element (within the INVENTORY element, within the XSLT root node) would become the current context node in turn, as shown here:

<xsl:stylesheet

version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">

<! Here, the context node is the XSLT root node (/) > <xsl:for-each select="INVENTORY/BOOK">

Trang 4

Chapter 12 Displaying XML Documents Using XSLT Style Sheets 433

<xsl:stylesheet

version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">

<HTML>

<HEAD>

<TITLE>Book Inventory</TITLE>

</HEAD>

<BODY>

<H2>Book Inventory</H2>

<xsl:apply-templates select="INVENTORY/BOOK" />

</BODY>

</HTML>

</xsl:template>

<xsl:template match="BOOK">

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

<xsl:value-of select="TITLE"/><BR/>

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

<xsl:value-of select="AUTHOR"/><BR/>

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

<xsl:value-of select="BINDING"/><BR/>

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

<xsl:value-of select="PAGES"/><BR/>

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

<xsl:value-of select="PRICE"/><P/>

</xsl:template>

</xsl:stylesheet>

Listing 12-5.

The example style sheet includes two templates One template contains

instruc-tions for displaying the entire document (the one with the match="/" setting,

specifying the XSLT root node) The other template contains instructions for

displaying a BOOK element (the template with match="BOOK") The browser

begins by processing the template that matches the XSLT root node:

<xsl:template match="/">

<HTML>

<HEAD>

<TITLE>Book Inventory</TITLE>

Trang 5

434 XML Step by Step

</HEAD>

<BODY>

<H2>Book Inventory</H2>

<xsl:apply-templates select="INVENTORY/BOOK" />

</BODY>

</HTML>

</xsl:template>

The xsl:apply-templates element tells the browser that for every BOOK element

within the INVENTORY root element, it should apply a template As explained

in the sidebar “How Internet Explorer Applies XSLT Templates” on page 414, when the browser applies a template to a particular element or other type of node, it begins by looking for a matching template explicitly defined in the style sheet If it finds a matching template, it executes its instructions If it doesn’t find a matching template, it uses a built-in template that’s appropriate for the type of the node (See the referenced sidebar for a description of the exact ac-tions of the different built-in templates.)

As you learned earlier in the chapter, the browser automatically applies a

template to the XSLT root node (and if it doesn’t find a template matching the root node, its built-in template automatically applies a template to all child

nodes of the XSLT root node) With the xsl:apply-templates element, you can force the browser to apply a template to a designated set of nodes You specify this node-set by assigning a location path to the xsl:apply-templates element’s select attribute.

By assigning the location path INVENTORY/BOOK to the select attribute of the xsl:apply-templates element, the example style sheet causes the browser to apply

a template to every BOOK element Because the style sheet contains a template

that matches BOOK elements—that is, a template whose match attribute is set

to the value BOOK—the browser invokes this template for each BOOK element The template, shown below, contains the same display instructions you saw

within the example xsl:for-each element given in the previous section, and the

output is the same as that shown in the figure at the end of the previous section

<xsl:template match="BOOK">

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

<xsl:value-of select="TITLE"/><BR/>

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

<xsl:value-of select="AUTHOR"/><BR/>

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

<xsl:value-of select="BINDING"/><BR/>

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

Trang 6

Chapter 12 Displaying XML Documents Using XSLT Style Sheets 437

Location path Meaning or use Example (selects or matches)

expression

specified name

/ (within a The child operator, AUTHOR/LASTNAME (Every LASTNAME element that's

location steps

/ (at the The XSLT root node /INVENTORY/BOOK (Every BOOK element that’s a

beginning of a child of an INVENTORY element that’s a child

any level

(Can be used in a The context node //BOOK (Every BOOK element that’s a descendant

select value only) of the context node)

<xsl:value-of select=“.”/> (Outputs the value

of the context node)

(Can be used in a The parent node of /AUTHOR (Every AUTHOR element that's a child

select value only) the context node of the context node’s parent node)

BOOK element)

element that can be a child of the XSLT root node)

@name Every attribute with BOOK/@InStock (Every InStock attribute belonging

BOOK element)

comment() Every comment /comment() (Every comment that’s a child of the

XSLT root node) You could display the text of these comments as follows:

<xsl:for-each select=“/comment()”>

<xsl:value-of select=“.”/>

</xsl:for-each>

continued

Trang 7

Chapter 12 Displaying XML Documents Using XSLT Style Sheets 439

<xsl:template match="/">

<! context node is XSLT root node (/) >

<xsl:for-each select="INVENTORY/BOOK">

<! context node is the current /INVENTORY/BOOK element > <xsl:value-of select="TITLE"/><BR/>

</xsl:for-each>

</xsl:template>

In contrast, a relative location path you assign to a match attribute in an

xsl:template element doesn’t specify a location relative to a specific context

node Rather, the location it specifies can be anywhere within the document Consider, for instance, the following XML document element:

<BOOK>

<TITLE>XML Step by Step, Second Edition</TITLE>

<PREVIOUS>

<TITLE>XML Step by Step, First Edition</TITLE>

</PREVIOUS>

</BOOK>

The following template would match a TITLE element wherever it occurs in the document Specifically, it would match the TITLE element that’s a child of

BOOK, as well as the one that’s a child of PREVIOUS (and the template would therefore be invoked whenever the browser applied a template to either of these TITLE elements):

<xsl:template match="TITLE">

<! template elements >

</xsl:template>

If you wanted a template that matched only the TITLE element within

PREVI-OUS, you could set the match attribute equal to the relative location path

PRE-VIOUS/TITLE, as in this example:

<xsl:template match="PREVIOUS/TITLE">

<! template elements >

</xsl:template>

This template would match a TITLE element that is a child of a PREVIOUS ele-ment located anywhere in the docuele-ment (Similarly, if you wanted a template that matched only the TITLE element that’s an immediate child of BOOK, you

would set its match attribute to the relative location path BOOK/TITLE.)

Trang 8

440 XML Step by Step

Filtering and Sorting XML Data

In the next two sections, you’ll learn the basics of using an XSLT style sheet to filter or sort XML data Following that, you’ll see some example style sheets that demonstrate both filtering and sorting

Filtering

As you’ve seen, the location path that you assign to the select attribute (of the xsl:value-of, xsl:for-each, or xsl:apply-templates element) or to the match at-tribute (of the xsl:template element) selects or matches a set of elements or other

types of nodes within the XML document You’ve learned how to use various expressions and operators within a location path to accurately narrow down the set of nodes that is selected or matched

In this section, you’ll learn how to further restrict the set of selected or matched

nodes by including a filter clause, also known as a predicate, within a location

path A filter clause is contained within square brackets ([]) and can be placed immediately following any of the location steps within a location path (Recall that the location steps are the individual parts of a location path that are sepa-rated with slash (/) characters.) For example, the location path assigned to the

select attribute in the following xsl:for-each element specifies that each of the

selected nodes must be a BOOK element, must be contained within an INVEN-TORY element within the current context node, and (because of the filter clause) must have a child BINDING element containing the text “trade paperback”:

Filter clause

Location path

Here’s the same xsl:for-each element shown in its containing template:

<xsl:template match="/">

<! other elements >

<xsl:for-each

select="INVENTORY/BOOK[BINDING='trade paperback']">

<xsl:value-of select="TITLE"/><BR/>

</xsl:for-each>

<! other elements >

</xsl:template>

Trang 9

442 XML Step by Step

Note that with this alternative approach, the browser would apply a template to

every BOOK element For a trade paperback BOOK element, it would use the

matching template However, for a BOOK element that stores a different type of book (such as a hardcover), the browser would be unable to find a matching template (unless you added a matching template to the style sheet) It would therefore use its built-in template, which would display the entire text content of the element To prevent the browser from doing this, you could add a “do noth-ing” template that matches all non-trade paperback books:

<xsl:template match=”BOOK[BINDING!=’trade paperback’]”>

</xsl:template>

The not-equal operator (!=) is another of the comparison operators you can use

in a filter clause Table 12-2 provides a complete list of these operators

Comparison operator Meaning

Table 12-2 The XSLT comparison operators

You can use any of the last four operators to test the value of elements (or

at-tributes) that contain numeric values For instance, the xsl:for-each element in

the following example would select every BOOK element that has a child

PAGES element containing a numeric value greater than 300:

<xsl:for-each select=”INVENTORY/BOOK/PAGES[ > 300]”>

<xsl:value-of select=”.”/><BR/>

</xsl:for-each>

This example would display a list of page counts for all books that have more than 300 pages Notice the use of the context node operator (.) in the filter clause to test the value of the preceding element itself (PAGES) rather than a child of that element

Trang 10

444 XML Step by Step

<COLOR>white</COLOR>

<COLOR>black</COLOR>

</SHIRT>

<! other elements >

</CATALOG>

The location path in the following start-tag would select the first SHIRT element

in the example because one of its COLOR child elements contains “green:”

<xsl:for-each select=”CATALOG/SHIRT[COLOR=’green’]”>

To select only SHIRT elements in which all child COLOR elements contain

“green,” you could use the filter clause given in the following start-tag:

<xsl:for-each select=”CATALOG/SHIRT[not(COLOR!=’green’)]”>

(This location path wouldn’t select either of the SHIRT elements in the example document.)

The not is a Boolean operator you can use in a filter clause to reverse the true or false value of the expression in the parentheses that follow the not operator You can test the value of a specific child element by including a number in

square brackets ([]) following the element name, where [1] refers to the first child element with the specified name For example, the location path in the fol-lowing start-tag selects SHIRT elements in which the second COLOR child ele-ment contains “green:”

<xsl:for-each select=”CATALOG/SHIRT[COLOR[2]=’green’]”>

(This location path would select the first SHIRT element in the example document.)

If a location step yields a node-set containing more than one node (a common situation), you can select an individual node by appending a filter clause that contains just the number of the node you want to select, where [1] selects the first node in the set For instance, the location path in the following start-tag

se-lects all the child elements of the first BOOK element within INVENTORY:

<xsl:for-each select=”INVENTORY/BOOK[1]/*”>

And the following xsl:value-of element displays the text content of the second BOOK element within INVENTORY (Without the filter clause, the xsl:value-of

element would display the content of the first BOOK element.)

<xsl:value-of select=”INVENTORY/BOOK[2]”/>

A final type of filter clause contains just an element name It indicates that the preceding node must have at least one child element with the specified name,

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

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

w