Using XSLT and XPath to Enhance HTML Documents Reference: Roger L. Costello
Multiple Output Formats XSL may be used to generate either HTML, XML, or text XSL Processor XSL XML HTML (or XML or text)
HTML Generation We will first use XSL to generate HTML documents When generating HTML, XSL should be viewed as a tool to enhance HTML documents. –That is, the HTML documents may be enhanced by extracting data out of XML documents –XSL provides elements (tags) for extracting the XML data, thus allowing us to enhance HTML documents with data from an XML document
Enhancing HTML Documents with XML Data XML Document HTML Document (with embedded XSL elements) XSL element XML data XSL Processor XML data
Enhancing HTML Documents with the Following XML Data Jeff lightgrey FitnessCenter.xml
Embed HTML Document in an XSL Template <xsl:stylesheet xmlns:xsl=" version="1.0"> Welcome Welcome! Note how we have the HTML document embedded within an XSL template
Note The HTML is embedded within an XSL template, which is an XML document –Consequently, the HTML must be well formed, i.e., every start tag must have an end tag Because the HTML is embedded within an XSL template, we are able to add XSL elements to the HTML, allowing us to extract data out of XML documents Let's customize the HTML welcome page by putting in the member's name. This is achieved by extracting the name from the XML document. We use an XSL element to do this.
Extracting the Member Name <xsl:stylesheet xmlns:xsl=" version="1.0"> Welcome Welcome !
Extracting a Value from an XML Document, Navigating the XML Document Extracting values: –use the XSL element Navigating: –The slash ("/") indicates parent/child relationship –A slash at the beginning of the path indicates that it is an absolute path, starting from the top of the XML document /FitnessCenter/Member/Name "Start from the top of the XML document, go to the FitnessCenter element, from there go to the Member element, and from there go to the Name element."
Document / PI Element FitnessCenter Element Member Element Name Element Phone Element Phone Element FavoriteColor Text Jeff Text Text Text lightgrey
Extract the FavoriteColor and use it as the bgcolor <xsl:stylesheet xmlns:xsl=" version="1.0"> Welcome Welcome !
Note Attribute values cannot contain " " - Consequently, the following is NOT valid: "> To extract the value of an XML element and use it as an attribute value you must use curly braces: Evaluate the expression within the curly braces. Assign the value to the attribute.
Extract the Home Phone Number <xsl:stylesheet xmlns:xsl=" version="1.0"> Welcome Welcome ! Your home phone number is:
Note In this example we want "the Phone element where the value of its type attribute equals 'home' ": The expression within […] is called a "predicate". Its purpose is to filter. Note the use of the single quotes within the double quotes. select=" … ' …' …"
Review - HTML Table This will create a table with 3 rows - the first row contains a header for each column. The next two rows contains the table data.
Fruit Color Papaya Red Banana Yellow Fruit Color Papaya Red Banana Yellow
Create a Table of Phone Numbers Suppose that a Member has an arbitrary number of phone numbers (home, work, cell, etc). Create an HTML table comprised of the phone numbers. On each row of the table put the type (home, work, cell, etc) in one column and the actual phone number in the next column.
<xsl:stylesheet xmlns:xsl=" version="1.0"> Welcome Welcome ! Your phone numbers are: Type Number
Iterating through XML Elements <!- - Within here we are at one of the Phone elements. Thus, in <xsl:value-of select="path", the value for path is relative to where we are in the XML document. The "." refers to the Phone element that we are currently positioned at. - ->
Absolute Path versus Relative Path This is an absolute xPath expression (we start from the top of the XML tree and navigate down the tree) This is a relative xPath expression (relative to where we currently are located, give me the value of the type attribute)
Special Offer to Platinum Members Let's further enhance our example to provide a special offer to "platinum" members. We need to check to see if the "level" attribute on the Member element equals "platinum".
Welcome Welcome ! Our special offer to platinum members today is... Your phone numbers are: Type Number
Conditional Processing Use the element to perform conditional processing.
Accessing Multiple Parts of the XML Document Let's enhance the table to contain three columns - the name of the Member, the type of the phone (home, work, cell, etc), and the actual phone number.
Welcome Welcome ! Our special offer to platinum members today is... Your phone numbers are: Name Type Number
Getting the Name when accessing the Phone Member Phone Phone Name Jeff Notice how when in the for-each loop we need to access the Name which is "up and over" with respect to the Phone element Bottom line: we can access elements in other parts of the XML tree via the “../” operator.
Other ways to Access the XML Data "Select the Name of the first Member" "Select the Name of the first Member" "Select the Name of the last Member" Note: Assume that there are multiple Members
Other ways to Access the XML Data (cont.) <!- - Process all Name elements which have FitnessCenter as an ancestor - ->
Enhanced XML Document Jeff lightgrey David lightblue Roger lightyellow Note that each Member now has a unique id (the id attribute)
Name Home Phone Number
Numbering There is an XSL element that returns a number corresponding to the element's position in the set of selected nodes. Output: 1. Jeff 2. David 3. Roger
Start Numbering from 0 How would you start the numbering from zero, rather than one?
format attribute of xsl:number In the previous example we saw how to generate numbers, and we saw that the generated numbers were 1, 2, 3, etc. With the format attribute we can specify the format of the generated number, i.e., 1, 2, 3 or I, II, III, or A, B, C, or … –format=“1” generates the sequence: 1, 2, 3, … –format=“01” generates: 01, 02, 03, … –format=“A” generates: A, B, C, … –format=“a” generates: a, b, c, … –format=“I” generates: I, II, III, … –format=“i” generates: i, ii, iii,...
format attribute of xsl:number. Output: A. Jeff B. David C. Roger
Sorting There is an XSL element that sorts the elements that you extract from the XML document "For each Member, sort the Name elements" Output: David Jeff Roger (see html-example10)
Sorting The set of Member elements selected by xsl:for-each is sorted using the Name child element. This occurs prior to the first iteration of the loop. After the set of Member elements are sorted then the looping begins.