Tài liệu Module 4: Displaying an XML Document Using XSL ppt

60 467 0
Tài liệu Module 4: Displaying an XML Document Using XSL ppt

Đ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

Module 4: Displaying an XML Document Using XSL Contents Overview Transforming XML with XSL Using Templates Filtering and Sorting XML 22 XSL Language Constructs 33 Creating New Nodes Dynamically 39 Lab 4: Displaying an XML Document Using XSL 44 Review 52 Information in this document is subject to change without notice The names of companies, products, people, characters, and/or data mentioned herein are fictitious and are in no way intended to represent any real individual, company, product, or event, unless otherwise noted Complying with all applicable copyright laws is the responsibility of the user No part of this document may be reproduced or transmitted in any form or by any means, electronic or mechanical, for any purpose, without the express written permission of Microsoft Corporation If, however, your only means of access is electronic, permission to print one copy is hereby granted Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual property rights covering subject matter in this document Except as expressly provided in any written license agreement from Microsoft, the furnishing of this document does not give you any license to these patents, trademarks, copyrights, or other intellectual property  2000 Microsoft Corporation All rights reserved Microsoft, PowerPoint, Visual InterDev, and Windows are either registered trademarks or trademarks of Microsoft Corporation in the U.S.A and/or other countries The names of companies, products, people, characters, and/or data mentioned herein are fictitious and are in no way intended to represent any real individual, company, product, or event, unless otherwise noted Other product and company names mentioned herein may be the trademarks of their respective owners Program Manager: Steve Merrill Instructional Designers: Sangeeta Nair (NIIT), Vijayalakshmi Narayanaswamy (NIIT) Subject Matter Experts: Andy Olsen (QA Training), Andy Longshaw (Content Masters) Content Lead: Janet Robinson Graphic Artist: Scott Serna (Creative Assets) Media Management: David Mahlmann Media Production: Dean Connolly (Art Source), Timothy Demmon (:timebomb Media) Editing Manager: Jennifer Linn Editor: Dennis Rae (Wasser) Production Manager: Miracle Davis Print Coordinator: Marlene Lambert (Online Training Solutions, Inc) Build Manager: Julie Challenger Build Coordinator: Jenny Boe Test Lead: Eric Myers Manufacturing Manager: John Williams Group Product Manager: Steve Elston Module 4: Displaying an XML Document Using XSL iii Instructor Notes Presentation: 165 Minutes Lab: 60 Minutes This module describes how to write an XSL style sheet and apply it to a static XML document The focus of the module is to describe the syntax for XSL template rules, and to introduce the various filter and path specifications that allow you to match particular elements and attributes in an XML document Many students find style sheets confusing, partly because there is so much syntax to learn, but also because the concept of rule-based programming can be quite alien The key points to emphasize during the module are that an XSL document is itself an XML document (with familiar < and > tags), and that a template rule always has a current context (for example, the element or attribute that is currently being matched) The module primarily shows how to transform XML into HTML for display in a browser Toward the end of the module, we briefly discuss how to create new XML elements/attributes/processing instructions and so on, so that one grammar of XML can be transformed into a different grammar Point out that this is useful in a business-to-business e-commerce situation After completing this module, students will be able to: ! Describe how XSL transforms an XML tree into a different XML result tree ! Write an XSL style sheet with template rules and actions ! Use to apply additional templates ! Use the path and filter capabilities of XSL for pattern matching ! Achieve complex styling with XSL language constructs ! Generate new nodes in the XML result tree Materials and Preparation This section provides you with the required materials and preparation tasks that you need to teach this module Required Materials To teach this module, you need the following materials: ! Microsoft PowerPoint® file 1905a_04.ppt ! Module 4, “Displaying an XML Document Using XSL” ! Lab 4, “Displaying an XML Document Using XSL” iv Module 4: Displaying an XML Document Using XSL Preparation Tasks To prepare for this module, you should: ! Read all of the materials for this module ! Complete the labs Due to the length of the answers to the labs for this course, we were unable to include them in the Delivery Guide Please see Appendix A or the Student CD for the lab answers ! Familiarize yourself with the XSL Debugger tool, which is illustrated in the first Demonstration Module 4: Displaying an XML Document Using XSL v Module Strategy Use the following strategies to present this module: ! Transforming XML with XSL Remind students that we have already seen the effects of XSL style sheets in Module 2, “Overview of XML Technologies.” Tell students that they will now see how to create XSL style sheets, rather than only being able to use them To understand how style sheets work, it is imperative that students be aware of how an XML document maps to a tree of nodes This mental picture is fundamental to the students’ understanding of the XSL template-matching rules Also, point out that the syntax shown in this module is the XSL syntax currently supported in Internet Explorer The W3C has since standardized XSL Transformations (XSLT), which is different in various respects to XSL in Internet Explorer Refer students to the W3C site at http://www.w3.org for more information about XSLT ! Using Templates There are three main aims in this section: to show how to match elements and attributes in a template rule, to show how to define the output template (for example, the result of the transformation), and to show how to invoke secondary templates by using Point out that you must have a template rule matching the root element in the XML document Without this, nothing else in the XSL style sheet is executed Also, many students find the distinction between “/” and “//” confusing Deal carefully with this topic There is one other surprise for many students: When you specify a match pattern for , it is not recursive; that is, it retrieves the value of the specified node but not its descendants ! Filtering and Sorting XML Pattern matching is a subtle topic The best way to approach it is with plenty of examples You might like to show a sample XML document (that is, with and elements), and describe the meanings of the various patterns shown in the slides In particular, stress that “//” is recursive (giving descendants at any depth), while “/” is not recursive (giving the direct child only) Also point out that “*” finds all elements but does not find attributes — you need “@*” if you wish to match all attributes By comparison, most students find filters straightforward This may be because filters resemble conventional If statements found in other programming languages ! XSL Language Constructs This is a short section and can be covered fairly quickly Students will immediately recognize these constructs from their experience with other programming languages Feel free to make conceptual comparisons with If statements, For loops, and Case statements found in other languages vi Module 4: Displaying an XML Document Using XSL ! Creating New Nodes Dynamically Before describing how these XSL constructs work, it is important to describe why you might use them — for example, to emphasize the use of XSL to transform one XML grammar into another This implies the need to generate XML elements, attributes, and so on; hence the need for these constructs The example in the demonstration shows how to add an HREF attribute to an element to generate a hyperlink This technique is used in the lab to define a hyperlink to an ASP where book details are generated and returned to the browser ! Lab 4: Displaying an XML Document Using XSL In this lab, students write an XSL style sheet from scratch The style sheet transforms the XML book details (returned from Query.asp) into an HTML table Students find the lab quite achievable because most of the tasks have been covered in a similar way in the module Module 4: Displaying an XML Document Using XSL # Overview Slide Objective To provide an overview of the module topics and objectives ! Transforming XML with XSL Lead-in ! Using Templates ! Filtering and Sorting XML ! XSL Language Constructs ! Creating New Nodes Dynamically ! Lab 4: Displaying an XML Document Using XSL ! Review In this module, you will learn about displaying XML data in a Web browser by using the Extensible Stylesheet Language, or XSL One important feature of XML is that it can be transformed into multiple formats This module will focus on how to transform XML into HTML format Raw data held as XML can be converted into a display format such as HTML for display in a Web browser For example, consider two applications that require the same data but in a different format of XML In such a case, a conversion is required when passing that data from one application to the other The Extensible Stylesheet Language (XSL) enables this kind of data conversion in XML After completing this module, you will be able to: ! Describe how XSL transforms an XML tree into a different XML result tree ! Write an XSL style sheet with template rules and actions ! Use to apply additional templates ! Use the path and filter capabilities of XSL for pattern matching ! Achieve complex styling with XSL language constructs ! Generate new nodes in the XML result tree Module 4: Displaying an XML Document Using XSL # Transforming XML with XSL Slide Objective To introduce the general concept of transforming an XML tree into a different format Lead-in For a Web browser to display XML in a userfriendly way, the XML must be converted into HTML XSL style sheets provide a way to achieve this type of transformation ! Transforming XML Trees ! Mapping XML Trees ! Mechanics of XSL Transformations ! Demonstration: Using the XSL Debugger XML is a very good mechanism for describing data However, this raw format may not always be the best way to export or display that data For example, although Internet Explorer 5.0 understands XML notations and syntax rules, when a raw XML document containing book information is loaded into Internet Explorer 5.0, it is displayed as a collapsible hierarchy of elements and child elements This information would be far better displayed as a table, with carefully chosen fonts and color schemes to make the user interface visually attractive You can think of an XML document as a tree structure Therefore, when converting XML from one format to another, the source tree containing the data must be converted into a result tree that represents a different format XSL style sheets provide a way of achieving this conversion An XSL style sheet defines the rules that should be applied to the parts of the XML source tree in order to convert them to the equivalent parts of the XML result tree Module 4: Displaying an XML Document Using XSL Transforming XML Trees Slide Objective To describe the process of transforming an XML document from one format to another ! XML document Lead-in Transforming an XML document into another format requires an XSL processor The XSL processor applies the given style sheet to the XML document and creates a new XML tree as a result Using XSL and XSL processor for transforming XML XSL processor XML document (different format) + XSL style sheet ! XSL processor HTML document (XML-conformant) Attaching an XSL style sheet to an XML document The Extensible Stylesheet Language was first defined in August 1997 as a style language for XML The aim of XSL is to provide a simple but powerful style-sheet syntax One of the main uses of XSL is to define how XML documents should appear in a presentation medium such as a browser XSL also enables the transformation of an XML document from one format to another To transform a given XML document into a different format, XSL requires an XSL processor that will apply an XSL style sheet to the XML document An XSL processor is built into the Microsoft® XML COM component Using XSL style sheets An XSL style sheet contains rules that determine how an XSL processor converts the XML source tree into the XML result tree Each rule consists of a pattern that identifies the elements or attributes to be transformed, and a template that is used to define the result of the transformation To attach an XSL style sheet to an XML document, add a processing instruction such as the following to the XML document: This will cause an XML-aware application such as Internet Explorer 5.0 to invoke the XSL processor and pass to the processor the XML source document together with the style sheet The XSL processor translates the XML document into a different form of XML The output generated from an XSL style sheet can be any document, such as an XML or EDI document When transforming an XML document into HTML, bear in mind that the HTML must be XML-compliant For example, each start tag must have a corresponding end tag, and case sensitivity rules Module 4: Displaying an XML Document Using XSL If more than one style sheet is attached to a document, only the first one is used All subsequent style sheets are ignored Using XSL to transform XML into different XML XSL also has the potential to help build sophisticated e-commerce solutions To understand the role of XSL in e-commerce, consider the following example Companies A and B constantly exchange data Both of these companies produce widgets, and need to describe their customer orders Each widget has a part number, and therefore each order consists of a series of part numbers and associated quantities So far, the two companies’ business models are identical However, when writing its XML-aware applications, Company A has decided to represent both the part number and the quantity as separate elements, leading to the following type of nested hierarchy: 12980-235 200 In contrast, Company B has decided to represent all of this order-item information in a single element and to use attributes for the part number and quantity as follows: Not only are the formats of these two XML documents different, but the names of the attributes used by Company B also differ from the names of the elements used by Company A In such a scenario, you can use XSL to transform the XML document of Company A into the format used by Company B, or vice versa Note A word of caution with regard to XSL Transformation (XSLT) and XSL: The draft standards are in a constant state of flux, and syntax that is valid today might be invalid in a few months’ time The discussions in this course are based on the fifth draft standard of the XSLT specification, dated July 1999, which is available at the Web site http://www.w3.org/TR/WD-xslt The latest draft specification for XSL is draft three, dated 21 April 1999, which is available at the Web site http://www.w3.org/TR/WD-xsl For the purposes of this course, we will concentrate on XSLT as implemented in the Microsoft XML COM component At the time of this writing, few applications implement the formatting objects as specified by the W3C Subsequent references to XSL will implicitly refer to features provided by XSLT 40 Module 4: Displaying an XML Document Using XSL Creating New Nodes Slide Objective To look at the need for creating new nodes, and to introduce the XSL syntax for creating new elements ! Mapping XML to XML may not be straightforward $ Lead-in Mapping between the XML source tree and the XML output tree is not always straightforward Sometimes it becomes necessary to create new XML nodes dynamically $ ! Single element maps to multiple elements Dynamic naming of elements and attributes in output tree Output template may need to create new nodes dynamically $ xsl:attribute $ xsl:comment $ xsl:element $ xsl:pi XSL style sheets can be used to generate entirely new XML elements, attributes, processing instructions, and so on This can be extremely useful if you need to translate an XML document into a different format This situation might easily arise in an e-commerce scenario, where a company interacts with many other companies (for example, suppliers, brokers, regulatory bodies, and so on) By including certain XSL instructions in the output template, new nodes can be created in the XML output tree XSL elements XSL defines four elements that can be used to generate new XML nodes The following table lists the XSL elements and their functions XSL element Function xsl:attribute Creates a new XML Attribute node xsl:comment Creates a new XML Comment node xsl:element Creates a new XML Element node xsl:pi Creates a new XML ProcessingInstruction node Module 4: Displaying an XML Document Using XSL 41 The following example shows a style sheet that uses these elements to create new nodes: version="1.0" New dialect of employee info

.htm Delivery Tip You can also use an element to create the tag This is quite a complex style sheet Its basic parts are: ! The first template rule matches the root and generates an XML declaration in the form of a processing instruction It also adds an XML comment The rest of the output template creates a single node called and then processes all elements in the source tree ! The second rule finds all employees whose salary is greater than or equal to 50000 and processes any templates that match the element It encloses each employee’s name in a

element ! The third rule matches all elements and creates an element for each one It then uses the element to attach an HREF attribute to this element The content of the HREF attribute is dynamically assigned to the value of the name with an “.htm” extension appended 42 Module 4: Displaying an XML Document Using XSL If the style sheet is applied to the following XML document: Paul Roberts 23500 Freestone 51000 Philips 45000 Smith 72000 Then the following XML document is output from the XSL processor:

Freestone

Smith

When viewed in the browser, this XML looks like the following illustration Module 4: Displaying an XML Document Using XSL 43 Demonstration: Adding Attributes to an Element Slide Objective To demonstrate adding attributes to an element dynamically Lead-in In this demonstration, you will learn how to build a new XML document with a different grammar by using an XSL style sheet Delivery Tip Open the file employees.xml from the folder \InetPub\WWWRoot\1905\D emoCode\Mod04 and attach the highearners.xsl style sheet Open highearners.xsl Describe how it generates a new grammar of XML In the template rule for elements, describe how adds an HREF attribute to the element in order to form a hyperlink to another page View employees.xml in the browser The original XML document has been transformed into a new grammar Note: the hyperlinks won’t work because the files they link to not exist In this demonstration, you will learn how to use an XSL style sheet to transform one XML grammar into another The example uses an XML document containing employee details, finds those earning more than $50000, and constructs a new document with a different grammar 44 Module 4: Displaying an XML Document Using XSL Lab 4: Displaying an XML Document Using XSL Slide Objective To introduce the lab Lead-in In this lab, you will create an XSL style sheet to transform XML data into an HTML table in the LitWare Books Web application Explain the lab objectives In this lab, you will create an XSL style sheet to transform XML data into an HTML table in the LitWare Books Web application Objectives After completing this lab, you will be able to: ! Create an XSL style sheet ! Use XSL to transform XML into HTML Prerequisites Before working on this lab, you must have: ! Familiarity with the format of XML elements ! The ability to add content to an ASP page ! Basic knowledge of HTML table and hyperlink tags Scenario In this lab, you will create an XSL style sheet to transform XML data into an HTML table in the LitWare Books Web application Starter and solution files There are starter and solution files associated with this lab The starter files are in the folder \Labs\Lab04\Starter and the solution files for this lab are in the folder \Labs\Lab04\Solution Estimated time to complete this lab: 60 minutes Module 4: Displaying an XML Document Using XSL 45 Exercise 1: Creating and Using a Style Sheet In this exercise, you will create an XSL style sheet named QueryResult.xsl that will be used to format XML data returned to the user in response to a query for book information ! Create the QueryResult.xsl style sheet Create a new file named QueryResult.xsl in the folder \InetPub\WWWRoot\LitWare Add an XML declaration and the root element for the style sheet Create a template rule for the root element of the XML document Define the template rule so that it creates and tags Within these tags, display the text value of all elements in the XML document To view an example of what your XSL style sheet should look like, see Appendix A or the Student CD-ROM Save QueryResult.xsl ! Link the XML data to the style sheet Open Query.asp in the folder \InetPub\WWWRoot\LitWare Locate the code that generates the XML data that is returned to the Web browser, beginning with the following XML processing instruction: After this statement, add another XML processing instruction to link the XML document to the QueryResult.xsl style sheet To view an example of what your processing instruction should look like, see Appendix A or the Student CD-ROM Save your changes to Query.asp Test your changes: a Go to the home page of the LitWare Books Web site 46 Module 4: Displaying an XML Document Using XSL b In the query-by-author form, enter the text Lars%, and then click Query The LitWare Books database contains several books written by Lars Peterson The style sheet should render the data in the browser as follows Module 4: Displaying an XML Document Using XSL 47 Exercise 2: Creating an HTML Table in the Style Sheet In this exercise, you will modify QueryResult.xsl so that it builds an HTML table from the book information in the XML data Each row in the HTML table will represent a different book ! Create the HTML table structure In QueryResult.xsl, locate the statement in the template rule for the root XML element, and replace it with the following and tags: a Within these tags, apply the template for the elements, sorted in title order Create a template rule for the elements of the XML document Within this rule, create and tags so that each book is displayed in a different row of the table Within the and tags, create five sets of and tags This will create five columns per row, ready to hold the , , , , and element values, respectively Within the and tags, output the value of the following elements in turn: a b c d e To view an example of what your XSL style sheet should look like, see Appendix A or the Student CD-ROM Save your changes to QueryResult.xsl Test your changes: a Go to the home page of the LitWare Books Web site 48 Module 4: Displaying an XML Document Using XSL b In the query-by-author form, enter the text Lars%, and then click Query The style sheet should render the XML data as a table with the following appearance Notice that only the first author of each book is displayed ! Display all authors for each book Create a template rule for the elements of the XML document that displays the text value of the element In the template rule for the elements, change the output template to call for the elements instead of calling To view an example of what your XSL style sheet should look like, see Appendix A or the Student CD-ROM Save your changes to QueryResult.xsl Test your changes: a In the query-by-author form, enter the text Lars%, and then click Query The style sheet should render the XML data as a table with the following appearance Notice that all of the authors are now displayed Module 4: Displaying an XML Document Using XSL 49 Exercise 3: Creating Hyperlinks to a Details Page for Each Book In this exercise, you will modify QueryResult.xsl so that it creates an HTML hyperlink for each book in the XML data QueryResult.xsl will take the element for each book and convert it into an HTML element with the following appearance: Details This is a hyperlink to an ASP page named Details.asp, the purpose of which is to return complete details for a specific book ! Create the HTML hyperlink element In QueryResult.xsl, create a template rule for the elements of the XML document In the template rule, the following: a Create an HTML anchor element using the instruction: Details b Create an HREF attribute of the HTML anchor element by using the instruction Use to specify the value for the HREF attribute In the template rule for the elements, change the output template to call for the elements instead of calling To view an example of what your XSL style sheet should look like, see Appendix A or the Student CD-ROM Save your changes to QueryResult.xsl 50 Module 4: Displaying an XML Document Using XSL ! Test the hyperlink capabilities Copy the file Details.asp from the folder \Labs\Lab04\Starter to the folder \InetPub\WWWRoot\LitWare In Notepad or Microsoft Visual InterDev®, open Details.asp to familiarize yourself with its contents Currently, the ASP simply confirms the ISBN parameter that was passed into it Test the hyperlink capabilities of the LitWare Books Web application: a Go to the home page of the LitWare Books Web site b In the query-by-author form, enter the text Lars%, and then click Query The style sheet should render the XML data as a table The final column should contain hyperlinks to Details.asp, specifying the ISBN of the book in each case c Click one of the Details hyperlinks This goes to Details.asp, passing in the ISBN for the selected book Details.asp responds by displaying the book’s ISBN in the browser as follows Module 4: Displaying an XML Document Using XSL 51 Exercise (Optional): Displaying Authors on Separate Lines In this exercise, you will extend QueryResult.xsl so that it displays multiple authors on separate lines, rather than displaying all the authors in a continuous stream This will entail differentiating between the first author of a book and the other authors The first author can be displayed as is, but subsequent authors must be prefixed with a element to cause a line break between separate authors ! Create separate rows for each author of a book In QueryResult.xsl, locate the template rule for the element Modify this template rule so that it first tests whether this is the first author of the book or a subsequent author If it is a subsequent author, insert a HTML element to generate a line break To view an example of what your XSL style sheet should look like, see Appendix A or the Student CD-ROM Save your changes to QueryResult.xsl Test your changes: a Go to the home page of the LitWare Books Web site b In the query-by-author form, enter the text Lars%, and then click Query The style sheet should render the XML data as a table with the following appearance 52 Module 4: Displaying an XML Document Using XSL Review Slide Objective To reinforce module objectives by reviewing key points ! Describe how XSL transforms an XML tree into a different XML result tree ! Write an XSL style sheet with template rules and action ! Use to apply additional templates ! Use XSL’s path and filter capabilities for pattern matching ! Achieve complex styling with XSL language constructs ! Generate new nodes in the XML result tree Lead-in The review questions cover some of the key concepts taught in the module Summary In this module, you learned how to write XSL style sheets in order to transform XML documents into a different format such as an HTML document or a different XML grammar XML style sheets are themselves XML documents, and consists of a series of rules for matching elements/attributes in the source XML document The output template for these rules defines the required transformation The construct is used to invoke secondary template rules XSL also defines a number of language constructs: , , , , and XSL also allows new XML elements, attributes, and comments to be added, using constructs such as , , , and so on Review Questions How you attach an XSL style sheet to an XML document? What template rule must every style sheet contain? A rule for the root element Module 4: Displaying an XML Document Using XSL 53 What is the syntax for creating a template rule that would match a element in a element? How you recursively apply template rules to elements in the source XML document? Place an element in the output template How you output the value of an element? Use an element in the output template How can you change the order in which elements are processed in the source XML document? Add an order-by attribute to the or elements THIS PAGE INTENTIONALLY LEFT BLANK ... 4, ? ?Displaying an XML Document Using XSL? ?? ! Lab 4, ? ?Displaying an XML Document Using XSL? ?? iv Module 4: Displaying an XML Document Using XSL Preparation Tasks To prepare for this module, you should:... 44 Module 4: Displaying an XML Document Using XSL Lab 4: Displaying an XML Document Using XSL Slide Objective To introduce the lab Lead-in In this lab, you will create an XSL style sheet to transform... the XML result tree Module 4: Displaying an XML Document Using XSL Transforming XML Trees Slide Objective To describe the process of transforming an XML document from one format to another ! XML

Ngày đăng: 09/12/2013, 17:15

Từ khóa liên quan

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

  • Đang cập nhật ...

Tài liệu liên quan