XML Step by Step- P25 docx

15 261 0
XML Step by Step- P25 docx

Đ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

Chapter 12 Displaying XML Documents Using XSLT Style Sheets 451 12 Using XSLT Style Sheets <xsl:template match="BOOK"> <! display current BOOK element > </xsl:template> Although the filter clause could be added to the match attribute of the xsl:template element rather than the select attribute of the xsl:apply-templates element, the browser would then apply a template to all BOOK elements. For the BOOK elements that don’t match the filter criteria in the xsl:template ele- ment (the non-trade paperback books), the browser would use its built-in tem- plate, which would display all the BOOK elements’ text, creating extraneous output (unless you explicitly defined another template to match the non-trade paperback books). Accessing XML Attributes Technically, in XSLT an attribute is not considered to be a child of the element that contains it. However, in a location path or in a filter clause within a loca- tion path, you can reference an attribute as if it were a child of the element that contains it by entering the attribute name prefaced with the at sign (@), which indicates that the name refers to an attribute rather than to an element. For example, the location path in the following start-tag selects every BOOK el- ement with an attribute named InStock that has the value yes. In other words, it selects only the books that are in stock: <xsl:for-each select=”INVENTORY/BOOK[@InStock=’yes’]”> And the location path in the following xsl:for-each start-tag selects every BOOK element that has an attribute named InStock, regardless of the attribute’s value: <xsl:for-each select=”INVENTORY/BOOK[@InStock]”> You can use the xsl:value-of element to extract the value of an attribute in the same way you use it to extract the text content of an element. For example, the following xsl:value-of element outputs the value of the Born attribute belonging to the AUTHOR element: <xsl:value-of select=”AUTHOR/@Born”/> The location path in the following xsl:for-each start-tag selects every attribute belonging to the AUTHOR element: <xsl:for-each select=”AUTHOR/@*”/> 452 XML Step by Step note The attribute expressions @name and @* are also described in Table 12-1. The style sheet in Listing 12-8 demonstrates the techniques for accessing at- tributes belonging to elements in the XML document. This style sheet is linked to the XML document in Listing 12-9, and it displays all in-stock books in the book inventory. (You’ll find copies of these listings on the companion CD under the filenames XsltDemo06.xsl and XsltDemo06.xml.) XsltDemo06.xsl <?xml version="1.0"?> <! File Name: XsltDemo06.xsl > <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <HTML> <HEAD> <TITLE>Books in Stock</TITLE> </HEAD> <BODY> <H2>Books In Stock</H2> <TABLE BORDER="1" CELLPADDING="5"> <THEAD> <TH>Title</TH> <TH>Author</TH> <TH>Binding Type</TH> <TH>Number of Pages</TH> <TH>Price</TH> </THEAD> <xsl:for-each select="INVENTORY/BOOK[@InStock='yes']"> <TR ALIGN="CENTER"> <TD> <xsl:value-of select="TITLE"/> </TD> <TD> <xsl:value-of select="AUTHOR"/> <BR/> (born <xsl:value-of select="AUTHOR/@Born"/>) Chapter 12 Displaying XML Documents Using XSLT Style Sheets 453 12 Using XSLT Style Sheets </TD> <TD> <xsl:value-of select="BINDING"/> </TD> <TD> <xsl:value-of select="PAGES"/> </TD> <TD> <xsl:value-of select="PRICE"/> </TD> </TR> </xsl:for-each> </TABLE> </BODY> </HTML> </xsl:template> </xsl:stylesheet> Listing 12-8. XsltDemo06.xml <?xml version="1.0"?> <! File Name: XsltDemo06.xml > <?xml-stylesheet type="text/xsl" href="XsltDemo06.xsl"?> <INVENTORY> <BOOK InStock="yes"> <TITLE>The Adventures of Huckleberry Finn</TITLE> <AUTHOR Born="1835">Mark Twain</AUTHOR> <BINDING>mass market paperback</BINDING> <PAGES>298</PAGES> <PRICE>$5.49</PRICE> </BOOK> <BOOK InStock="no"> <TITLE>Leaves of Grass</TITLE> <AUTHOR Born="1819">Walt Whitman</AUTHOR> <BINDING>hardcover</BINDING> <PAGES>462</PAGES> <PRICE>$7.75</PRICE> 454 XML Step by Step </BOOK> <BOOK InStock="yes"> <TITLE>The Marble Faun</TITLE> <AUTHOR Born="1804">Nathaniel Hawthorne</AUTHOR> <BINDING>trade paperback</BINDING> <PAGES>473</PAGES> <PRICE>$10.95</PRICE> </BOOK> <BOOK InStock="yes"> <TITLE>Moby-Dick</TITLE> <AUTHOR Born="1819">Herman Melville</AUTHOR> <BINDING>hardcover</BINDING> <PAGES>724</PAGES> <PRICE>$9.95</PRICE> </BOOK> </INVENTORY> Listing 12-9. Each BOOK element in the XML document contains an InStock attribute set to yes or no to indicate whether the book is in stock. Each AUTHOR element has a Born attribute giving the birth year of the author. Rather than displaying the value of the InStock attribute, the style sheet uses the attribute in a filter clause to eliminate all out-of-stock books from the set of BOOK elements that it displays: <xsl:for-each select="INVENTORY/BOOK[@InStock='yes']"> <! display each BOOK element > </xsl:for-each> The style sheet displays the BOOK elements within an HTML table rather than in a list of SPAN elements as in the previous examples. It displays the value of the Born attribute, following the value of the AUTHOR element, by using the xsl:value-of element. The following elements create the table cell displaying these values: <TD> <xsl:value-of select="AUTHOR"/> <BR/> (born <xsl:value-of select="AUTHOR/@Born"/>) </TD> Here’s the way the Internet Explorer displays the document: Chapter 12 Displaying XML Documents Using XSLT Style Sheets 455 12 Using XSLT Style Sheets Adding an Attribute to a Literal Result Element You can add an attribute with a known, constant value to a literal result element by simply entering the attribute specification into the element start- tag that appears in the style sheet. You’ve seen many examples in the style sheets shown in this chapter, such as the STYLE attribute in the following literal result SPAN element: <SPAN STYLE="font-style:italic"> <xsl:value-of select="TITLE"/> </SPAN> If, however, you want to obtain the attribute’s value from the XML file, you need to use the xsl:attribute element. For example, consider an XML docu- ment with the following document element, in which the COLORCODE el- ements indicate the color that should be used for displaying each book’s title: <BOOKLIST> <BOOK> <TITLE>The Ambassadors</TITLE> <COLORCODE>red</COLORCODE> </BOOK> <BOOK> <TITLE>The Awakening</TITLE> continued 458 XML Step by Step </HEAD> <BODY> <H2>Collection Inventory</H2> <H3>Books</H3> <xsl:for-each select="book:COLLECTION/book:ITEM"> <xsl:value-of select="book:TITLE"/><BR/> <xsl:value-of select="book:AUTHOR"/><BR/> <xsl:value-of select="book:PRICE"/><BR/> <xsl:value-of select="@Status"/><BR/><BR/> </xsl:for-each> <H3>CDs</H3> <xsl:for-each select="book:COLLECTION/cd:ITEM"> <xsl:value-of select="cd:TITLE"/><BR/> <xsl:value-of select="cd:COMPOSER"/><BR/> <xsl:value-of select="cd:PRICE"/><BR/><BR/> </xsl:for-each> </BODY> </HTML> </xsl:template> </xsl:stylesheet> Listing 12-10. The example style sheet globally declares (in the xsl:stylesheet start-tag) two namespace prefixes that it can use to reference the two namespaces found in the XML document: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:book="http://www.mjyOnline.com/books" xmlns:cd="http://www.mjyOnline.com/cds"> The style sheet uses the book namespace prefix to refer to the XML elements that belong to the default namespace named http://www.mjyOnline.com/books, and it uses the cd namespace prefix to refer to the XML elements that belong to the explicit namespace named http://www.mjyOnline.com/cds. (In the style sheet, you can use any namespace prefixes you want, provided that the namespace names match those in the XML document.) Here’s an example of how the style sheet uses the book namespace prefix: <xsl:value-of select=”book:TITLE”/> Chapter 12 Displaying XML Documents Using XSLT Style Sheets 459 12 Using XSLT Style Sheets And here’s an example of how it uses the cd namespace prefix: <xsl:value-of select=”cd:TITLE”/> Using Conditional Structures This final section introduces the XSLT conditional elements, which allow you to selectively include transformation instructions in your style sheet based on con- ditions such as the value of an element or attribute. The simplest conditional element is xsl:if, which allows you to include or ex- clude a block of instructions based on a single test. Assume, for example, that the following template is in a style sheet linked to the XsltDemo06.xml XML document given in Listing 12-9. This template displays a simple list of the book titles. The xsl:if element within the template displays “out of stock!” to the left of the title for any BOOK element whose InStock attribute is set to no: <xsl:template match="/"> <! other elements > <xsl:for-each select="INVENTORY/BOOK"> <SPAN STYLE="font-style:italic"> <xsl:if test="@InStock = 'no'">out of stock! </xsl:if> <xsl:value-of select="TITLE"/> </SPAN> <BR/> </xsl:for-each> <! other elements > </xsl:template> You assign to the test attribute of the xsl:if element an expression that evaluates to true or false (that is, a Boolean expression). If the expression is true, the browser uses the transformation instructions contained within the xsl:if element. If the expression is false, the browser skips the instructions. You can assign test the same types of comparison expressions that are used in filter clauses (described in “Filtering” on page 438), and you can use any of the comparison operators listed in Table 12-2. You can branch to one of several alternative blocks of instructions by using the xsl:choose, xsl:when, and xsl:otherwise elements, which work like an if-else or switch construct in a programming language such as C. The following template contains an example. If this template were in a style sheet attached to the XsltDemo06.xml XML document, it would display a list of all book 460 XML Step by Step titles, indicating the size category of each book by displaying one, two, or three asterisks to the title’s left. (One asterisk indicates a length less than or equal to 300 pages, two asterisks a length greater than 300 but less than or equal to 500, and three asterisks a length greater than 500.) <xsl:template match="/"> <! other elements > <xsl:for-each select="INVENTORY/BOOK"> <SPAN STYLE="font-style:italic"> <xsl:choose> <xsl:when test="PAGES &lt;= 300">*</xsl:when> <xsl:when test="PAGES &lt;= 500">**</xsl:when> <xsl:otherwise>***</xsl:otherwise> </xsl:choose> <xsl:value-of select="TITLE"/> </SPAN> <BR/> </xsl:for-each> <! other elements > </xsl:template> When these three conditional elements are arranged as in the example, the browser uses the instructions contained in the first xsl:when element with a test attribute that evaluates to true. If none of the xsl:when elements have true test values, it uses the instructions contained in the xsl:otherwise element at the end (if it’s included). In the example, the “instructions” consist of only one to three asterisk characters to be copied to the output. Appendix 463 XML Schemas ■ For information on XML schemas as supported by Internet Explorer and MSXML 4.0, see the topic “XML Schemas” in the XML SDK documentation provided by the MSDN Library on the Web at http://msdn.microsoft.com/library/. ■ You’ll find the complete text of the W3C XML Schema specification in the following three pages on the Web: “XML Schema Part 0: Primer” at http://www.w3.org/TR/xmlschema-0/, “XML Schema Part 1: Structures” at http://www.w3.org/TR/xmlschema-1/, and “XML Schema Part 2: Datatypes” at http://www.w3.org/TR/xmlschema-2/. Cascading Style Sheets (CSS) ■ To learn about the CSS features supported by Internet Explorer, see the topic “Cascading Style Sheets” in the MSDN Library on the Web at http://msdn.microsoft.com/library/. ■ For a list of all the Internet Explorer color names that you can use for assigning colors in a cascading style sheet, see the topic “Color Table” in the MSDN Library on the Web at http://msdn.microsoft.com/library/. ■ The W3C publishes the specification for Cascading Style Sheets Level 1 (CSS1) at http://www.w3.org/TR/REC-CSS1. ■ The W3C publishes the specification for Cascading Style Sheets Level 2 (CSS2) at http://www.w3.org/TR/REC-CSS2. ■ For an introduction to the techniques for using cascading style sheets with HTML, see the topic “2.1 A brief CSS2 tutorial for HTML” in the W3C page “2 Introduction to CSS2” at http://www.w3.org/TR/REC-CSS2/intro.html. 464 Appendix Data Binding and the Data Source Object (DSO) ■ For information on using data binding and the DSO with Internet Explorer, see the topics “Binding the XML Data Source Object to Data,” “Use the C++ XML Data Source Object,” and “Use the Master/Detail Feature with the C++ XML Data Source Object” in the XML SDK documentation provided by the MSDN Library on the Web at http://msdn.microsoft.com/library/. ActiveX Data Objects (ADO) and the ADO recordset Object ■ 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/. ■ The Microsoft home page for information on ADO is located at http://www.microsoft.com/data/ado/. HTML and Dynamic HTML (DHTML) ■ For information on working with HTML and DHTML as implemented in Internet Explorer, see the topic “HTML and Dynamic HTML” in the MSDN Library on the Web at http://msdn.microsoft.com/library/. ■ To read the official specification for the latest version of HTML 4, see the following Web site, provided by the W3C: http://www.w3.org/TR/html4/. Microsoft JScript ■ For information on JScript and other Microsoft Web scripting tech- nologies, see the topic “Scripting” in the MSDN Library on the Web at http://msdn.microsoft.com/library/. [...]... cascading style sheets, 201 in XML documents defined, 56 examples, 56, 82 form of, 82 inserting, 25, 81–83 in Inventory Valid .xml example, 127 in Inventory .xml example, 25, 25 overview, 25 in Parts .xml example, 47, 49 as type of element content, 56, 62 where to place, 82–83 comparison operators, 442, 442 conditional structures in XSLT, 459–460 content model, 101 court documents (Open XML Court Interface), 17... 181–185 declaring in valid XML documents, 107–117 default declaration forms AttValue default declaration form, 117 #FIXED AttValue default declaration form, 117 illustrated, 116 #IMPLIED default declaration form, 116 overview, 116 #REQUIRED default declaration form, 116 enumerated type, 110, 114–116 form of declaration, 108–109 in Inventory Valid .xml example, 127 in Inventory04 .xml example, 66–68 naming,... binding, 323 See also single-record data binding D data binding and document type definitions, 337–344 how it works, 308 overview, 10, 308 single-record, 303, 322–327 steps for using binding HTML elements to XML elements, 297–298, 303–349 linking XML documents to HTML pages, 297, 299–302 table vx single-record, 303– 304 (See also table data binding) techniques for non-table HTML elements, 328–336 types of,... predefined, 156–157 as type of entity, 150 Chemical Markup Language (CML), 18 Chess Markup Language (ChessML), 18 child elements attributes as, 451 in Book .xml example, 369, 370, 375–376 defined, 52 in element content, 100, 101–105 and filters, 443 in Inventory03 .xml example, 62 and property inheritance in cascading style sheets, 203 specifying mixed content, 105–107 childNodes node property, 365, 370, 371,... 371, 374, 380 clear property, in cascading style sheets setting, 283–284 specifying CSS keyword values, 284, 284 CML (Chemical Markup Language), 18 Collection Default Valid .xml (Listing 5-1) listing, 118–119, 118–120 Collection Default .xml (Listing 3-5) listing, 74–75, 74–75 collections NamedNodeMap collection objects getNamedItem NamedNodeMap method, 386, 386, 392 item NamedNodeMap method, 386, 386 length... anonymous type definitions, 173 ANY keyword, 100 APPLET HTML element, 328 applications, SGML See also SGML (Structured Generalized Markup Language) defined, 11 applications, XML See also SGML (Structured Generalized Markup Language); XML (Extensible Markup Language) defined, 11–12, 14–15 apply-templates element See xsl: apply-templates element asterisk (*) character, in cascading style sheets comments,... 260, 260–261 Raven01.css example, 277, 277 Raven02.css example, 280–281, 281 Raven04.css example, 294, 294–295 rules for defined, 200 example, 200–201 illustrated, 200 multiple, 204 steps for using, 197–211 using to display XML documents, 34–41 versions of, 196 vs XSL style sheets, 10, 409–410, 411, 421 white space in, 58 case sensitivity in cascading style sheets, 203 in element-type names, 29, 54 in... movelast recordset method, 324 movenext recordset method, 324 moveprevious recordset method, 324 overview, 302 and single-record binding, 322 updating cached XML data, 336 using scripts with, 350-356 vs Document Object Model (DOM), 357 databases and XML, 7–8, 16 DATAFORMATAS HTML attribute, 334–335 DATAPAGESIZE HTML attribute, 309 dataType node property, 365 dbf files, 8 declaration block, in cascading... 470 Index length NodeList property, 374, 378 list of methods and properties, 374 nextNode NodeList method, 374 reset NodeList method, 374 using, 374 vs NamedNodeMap collection objects, 386 Collection .xml (Listing 3-4) listing, 71–72, 71 colon (:) in attribute names, 64 color background (See background-color property, in cascading style sheets) text (See color property, in cascading style sheets) color... markup, 54, 136 CDATA sections defined, 55 examples, 56, 87 form of, 86–87 where to place, 88 CDATASection node, 361 CDF (Channel Definition Format), 17 Channel Definition Format (CDF), 17 channels, role of XML, 17 character data adding, 55 defined, 27, 54 encoding and languages, 77–80 469 examples, 53, 55, 62 inserting special characters, 153–156 in mixed element content, 105–107 retrieving, 373–374 in schemas, . sheet attached to the XsltDemo06 .xml XML document, it would display a list of all book 460 XML Step by Step titles, indicating the size category of each book by displaying one, two, or three asterisks. output. Appendix 463 XML Schemas ■ For information on XML schemas as supported by Internet Explorer and MSXML 4.0, see the topic XML Schemas” in the XML SDK documentation provided by the MSDN Library. W3C XML Schema specification in the following three pages on the Web: XML Schema Part 0: Primer” at http://www.w3.org/TR/xmlschema-0/, XML Schema Part 1: Structures” at http://www.w3.org/TR/xmlschema-1/, and

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

Mục lục

    00000___07be008c8276e95dbbf4cdee2cdf4385

    00002___79c277462295ccdeedc24e5b50330987

    00003___7e0229f16eefad21c64fe455ba7840d5

    00004___a933fe67c82b988d8a19eb1cf4460489

    00005___c78cce8114aad80865103a642f927492

    00006___05fca2542d7816c3088c08cb5a953c52

    00007___dbcd3471236536641cfe07a8b300c340

    00008___51a230a8ae343e365423c24fc4c892b8

    00011___81cd371e1c1bacf76929df14a6b72ecd

    00012___fa71951b85f2335bba3707db9c9945c3

Tài liệu cùng người dùng

Tài liệu liên quan