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