Copyright c 2001 Roger L Costello All Rights

  • Slides: 106
Download presentation
Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Using XSLT and XPath to

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Using XSLT and XPath to Enhance HTML Documents Roger L. Costello XML Technologies 1

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Acknowledgement • I wish to

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Acknowledgement • I wish to thank David Jacobs for showing me a new way of looking at HTML and XSLT/XPath • Many of the examples that I use in this tutorial come straight from David's excellent paper, Rescuing XSLT from Niche Status (see http: //www. xfront. com) 2

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 3 History XSL (low-precision graphics,

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 3 History XSL (low-precision graphics, e. g. , HTML, text, XML) (high-precision graphics, e. g. , PDF) XQuery XLink/ XPointer XSLT XML Schemas XPath XSLT XSL

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 4 Note • For brevity,

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 4 Note • For brevity, instead of using the term XSLT/XPath, I will simply call it XSL.

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 5 Multiple Output Formats •

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 5 Multiple Output Formats • XSL may be used to generate either HTML, XML, or text XSL XML XSL Processor HTML (or XML or text)

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. xalan/xt/saxon • • • xalan:

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. xalan/xt/saxon • • • xalan: A free XSL processor, implemented in Java, from Apache (http: //www. apache. org/) xt: A free XSL processor, implemented in Java, from James Clark (http: //www. jclark. com/) saxon: A free XSL processor, implemented in Java, from Michael Kay (http: //users. iclway. co. uk/mhkay/saxon XML XSL xalan/xt/saxon HTML (or XML or text) Invoking from a DOS command line: run-xalan Fitness. Center. xml Fitness. Center. xsl Fitness. Center. html run-xt Fitness. Center. xml Fitness. Center. xsl Fitness. Center. html run-saxon Fitness. Center. xml Fitness. Center. xsl Fitness. Center. html 6

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 7 Styling XML Documents using

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 7 Styling XML Documents using IE 6 or Netscape 7 • • Put a stylesheet PI at the top of your XML document. Now you can simply drop the XML document into the browser and the XML will be automatically styled using the stylesheet referenced in the stylesheet PI. <? xml version="1. 0"? > <? xml-stylesheet type="text/xsl" href="Fitness. Center. xsl"? > <Fitness. Center> <Member level="platinum"> <Name>Jeff</Name> <Phone type="home">555 -1234</Phone> <Phone type="work">555 -4321</Phone> <Favorite. Color>lightgrey</Favorite. Color> </Member> </Fitness. Center> Add this stylesheet PI to the top of your XML document

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. HTML Generation • We will

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 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 8

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 9 Enhancing HTML Documents with

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 9 Enhancing HTML Documents with XML Data XML Document XML data XSL Processor XSL element XML data HTML Document (with embedded XSL elements)

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Enhancing HTML Documents with the

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Enhancing HTML Documents with the Following XML Data <? xml version="1. 0"? > <? xml-stylesheet type="text/xsl" href="Fitness. Center. xsl"? > <Fitness. Center> <Member level="platinum"> <Name>Jeff</Name> <Phone type="home">555 -1234</Phone> <Phone type="work">555 -4321</Phone> <Favorite. Color>lightgrey</Favorite. Color> </Member> </Fitness. Center> Fitness. Center. xml 10

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 11 Embed HTML Document in

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 11 Embed HTML Document in an XSL Template <? xml version="1. 0"? > <xsl: stylesheet xmlns: xsl="http: //www. w 3. org/1999/XSL/Transform" version="1. 0"> <xsl: output method="html"/> <xsl: template match="/"> <HTML> <HEAD> <TITLE>Welcome</TITLE> </HEAD> <BODY> Welcome! </BODY> </HTML> </xsl: template> </xsl: stylesheet> Fitness. Center. xsl (see html-example 01) Note how we have the HTML document embedded within an XSL template

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 12 Note • The HTML

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 12 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.

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Extracting the Member Name <?

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Extracting the Member Name <? xml version="1. 0"? > <xsl: stylesheet xmlns: xsl="http: //www. w 3. org/1999/XSL/Transform" version="1. 0"> <xsl: output method="html"/> <xsl: template match="/"> <HTML> <HEAD> <TITLE>Welcome</TITLE> </HEAD> <BODY> Welcome <xsl: value-of select="/Fitness. Center/Member/Name"/>! </BODY> </HTML> </xsl: template> </xsl: stylesheet> (see html-example 02) 13

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 14 Note • Notice how

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 14 Note • Notice how we have enhanced the HTML document by using data from the XML document!

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Extracting a Value from an

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Extracting a Value from an XML Document, Navigating the XML Document 15 • Extracting values: – use the <xsl: value-of select="…"/> 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 /Fitness. Center/Member/Name "Start from the top of the XML document, go to the Fitness. Center element, from there go to the Member element, and from there go to the Name element. "

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 16 Document / PI <?

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 16 Document / PI <? xml version=“ 1. 0”? > Element Fitness. Center Element Member Element Name Text Jeff Element Phone Element Favorite. Color Text 555 -1234 Text 555 -4321 Text lightgrey

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Extract the Favorite. Color and

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Extract the Favorite. Color and use it as the bgcolor <? xml version="1. 0"? > <xsl: stylesheet xmlns: xsl="http: //www. w 3. org/1999/XSL/Transform" version="1. 0"> <xsl: output method="html"/> <xsl: template match="/"> <HTML> <HEAD> <TITLE>Welcome</TITLE> </HEAD> <BODY bgcolor="{/Fitness. Center/Member/Favorite. Color}"> Welcome <xsl: value-of select="/Fitness. Center/Member/Name"/>! </BODY> </HTML> </xsl: template> </xsl: stylesheet> (see html-example 03) 17

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 18 Note Attribute values cannot

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 18 Note Attribute values cannot contain "<" nor ">" - Consequently, the following is NOT valid: <Body bgcolor="<xsl: value-of select='/Fitness. Center/Member/Favorite. Color'/>"> To extract the value of an XML element and use it as an attribute value you must use curly braces: <Body bgcolor="{/Fitness. Center/Member/Favorite. Color}"> Evaluate the expression within the curly braces. Assign the value to the attribute.

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Extract the Home Phone Number

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Extract the Home Phone Number <? xml version="1. 0"? > <xsl: stylesheet xmlns: xsl="http: //www. w 3. org/1999/XSL/Transform" version="1. 0"> <xsl: output method="html"/> <xsl: template match="/"> <HTML> <HEAD> <TITLE>Welcome</TITLE> </HEAD> <BODY bgcolor="{/Fitness. Center/Member/Favorite. Color}"> Welcome <xsl: value-of select="/Fitness. Center/Member/Name"/>! <BR/> Your home phone number is: <xsl: value-of select="/Fitness. Center/Member/Phone[@type='home']"/> </BODY> </HTML> </xsl: template> </xsl: stylesheet> (see html-example 04) 19

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 20 Note In this example

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 20 Note In this example we want "the Phone element where the value of its type attribute equals 'home' ": <xsl: value-of select="/Fitness. Center/Member/Phone[@type='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=" … ' …' …"

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Review - HTML Table <table

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Review - HTML Table <table border=“ 1” width=“ 100%”> <th> </tr> </th> <td> </tr> </td> <td> <tr> <td> </tr> </td> </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. 21

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. <table border=“ 1” width=“ 75%”>

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. <table border=“ 1” width=“ 75%”> <tr> <th>Fruit</th> <th>Color</th> </tr> <td>Papaya</td> <td>Red</td> </tr> <td>Banana</td> <td>Yellow</td> </tr> </table> 22 Fruit Color Papaya Red Banana Yellow

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Create a Table of Phone

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 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. 23

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 24 <? xml version="1. 0"?

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 24 <? xml version="1. 0"? > <xsl: stylesheet xmlns: xsl="http: //www. w 3. org/1999/XSL/Transform" version="1. 0"> <xsl: output method="html"/> <xsl: template match="/"> <HTML> <HEAD> <TITLE>Welcome</TITLE> </HEAD> <BODY bgcolor="{/Fitness. Center/Member/Favorite. Color}"> Welcome <xsl: value-of select="/Fitness. Center/Member/Name"/>! <BR/> Your phone numbers are: <TABLE border="1" width="25%"> <TR><TH>Type</TH><TH>Number</TH></TR> <xsl: for-each select="/Fitness. Center/Member/Phone"> <TR> <TD><xsl: value-of select="@type"/></TD> <TD><xsl: value-of select=". "/></TD> </TR> </xsl: for-each> </TABLE> </BODY> </HTML> </xsl: template> </xsl: stylesheet> (see html-example 05)

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Iterating through XML Elements <xsl:

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Iterating through XML Elements <xsl: for-each select="/Fitness. Center/Member/Phone"> <!- - 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. - -> </xsl: for-each> 25

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 26 Absolute Path versus Relative

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 26 Absolute Path versus Relative Path <xsl: value-of select="/Fitness. Center/Member/Phone[@type='home']"/> This is an absolute x. Path expression (we start from the top of the XML tree and navigate down the tree) <xsl: value-of select="@type"/> This is a relative x. Path expression (relative to where we currently are located, give me the value of the type attribute) Do Lab 1, Parts 1 -3

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Special Offer to Platinum Members

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 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". 27

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. <HTML> <HEAD> <TITLE>Welcome</TITLE> </HEAD> <BODY

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. <HTML> <HEAD> <TITLE>Welcome</TITLE> </HEAD> <BODY bgcolor="{/Fitness. Center/Member/Favorite. Color}"> Welcome <xsl: value-of select="/Fitness. Center/Member/Name"/>! <BR/> <xsl: if test="/Fitness. Center/Member/@level='platinum'"> Our special offer to platinum members today is. . . <BR/> </xsl: if> Your phone numbers are: <TABLE border="1" width="25%"> <TR><TH>Type</TH><TH>Number</TH></TR> <xsl: for-each select="/Fitness. Center/Member/Phone"> <TR> <TD><xsl: value-of select="@type"/></TD> <TD><xsl: value-of select=". "/></TD> </TR> </xsl: for-each> </TABLE> </BODY> </HTML> (see html-example 06) 28

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 29 Conditional Processing • Use

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 29 Conditional Processing • Use the <xsl: if test="…"/> element to perform conditional processing. Do Lab 1, Part 4

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Accessing Multiple Parts of the

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 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. 30

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. <HTML> <HEAD> <TITLE>Welcome</TITLE> </HEAD> <BODY

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. <HTML> <HEAD> <TITLE>Welcome</TITLE> </HEAD> <BODY bgcolor="{/Fitness. Center/Member/Favorite. Color}"> Welcome <xsl: value-of select="/Fitness. Center/Member/Name"/>! <BR/> <xsl: if test="/Fitness. Center/Member/@level='platinum'"> Our special offer to platinum members today is. . . <BR/> </xsl: if> Your phone numbers are: <TABLE border="1" width="25%"> <TR><TH>Name</TH><TH>Type</TH><TH>Number</TH></TR> <xsl: for-each select="/Fitness. Center/Member/Phone"> <TR> <TD><xsl: value-of select=". . /Name"/></TD> <TD><xsl: value-of select="@type"/></TD> <TD><xsl: value-of select=". "/></TD> </TR> </xsl: for-each> </TABLE> </BODY> </HTML> (see html-example 07) 31

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 32 Getting the Name when

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 32 Getting the Name when accessing the Phone Member 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 Name Jeff Phone 555 -1234 555 -4321 Bottom line: we can access elements in other parts of the XML tree via the “. . /” operator.

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 33 Other ways to Access

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 33 Other ways to Access the XML Data Note: Assume that there are multiple Members <xsl: value-of select="/Fitness. Center/Member[1]/Name"/> "Select the Name of the first Member" <xsl: value-of select="/Fitness. Center/Member[position()=1]/Name"/> "Select the Name of the first Member" <xsl: value-of select="/Fitness. Center/Member[last()]/Name"/> "Select the Name of the last Member" <xsl: for-each select="/Fitness. Center/Member[not(position()=last())]"> <!- - Process all Members but the last - -> </xsl: for-each>

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Other ways to Access the

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Other ways to Access the XML Data (cont. ) <xsl: for-each select="/Fitness. Center/Member[position() != last())]"> <!- - Process all Members but the last - -> </xsl: for-each> <xsl: for-each select="/Fitness. Center/Member[position() > 1]"> <!- - Process all Members but the first - -> </xsl: for-each> <xsl: for-each select="/Fitness. Center//Name"> <!- - Process all Name elements which have Fitness. Center as an ancestor - -> </xsl: for-each> 34

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Other ways to Access the

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Other ways to Access the XML Data (cont. ) <!- - Iterate through a list of Member nodes - -> <xsl: for-each select="/Fitness. Center/Member"> <!- - Output the Name of the "current" Member - -> <xsl: value-of select=". /Name"/> </xsl: for-each> <!- - Since a specific Member is not specified, all Member nodes - -> <!- - all selected. That is, the Name node within each Member - -> <!-- node is selected. --> <xsl: for-each select="/Fitness. Center/Member/Name"> <xsl: value-of select=". "/> </xsl: for-each> 35

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 36 Nodelist This x. Path

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 36 Nodelist This x. Path expression: /Fitness. Center/Member selects a list of nodes (a list of Member nodes). This list of nodes is called a "nodelist".

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 37 Enhanced XML Document <?

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 37 Enhanced XML Document <? xml version="1. 0"? > <Fitness. Center> <Member id="1" level="platinum"> <Name>Jeff</Name> <Phone type="home">555 -1234</Phone> <Phone type="work">555 -4321</Phone> <Favorite. Color>lightgrey</Favorite. Color> </Member> <Member id="2" level="gold"> <Name>David</Name> <Phone type="home">383 -1234</Phone> <Phone type="work">383 -4321</Phone> <Favorite. Color>lightblue</Favorite. Color> </Member> <Member id="3" level="platinum"> <Name>Roger</Name> <Phone type="home">888 -1234</Phone> <Phone type="work">888 -4321</Phone> <Favorite. Color>lightyellow</Favorite. Color> </Member> </Fitness. Center> Note that each Member now has a unique id (the id attribute)

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Review - HTML Hyperlinking <A

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Review - HTML Hyperlinking <A name="Anna. And. The. King"></A> … <A href="#Anna. And. The. King">Click Here</A>. . . This creates an internal hyperlink (the source "anchor" links to the target anchor). 38

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Hyperlink Name to Home Phone

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Hyperlink Name to Home Phone • Problem: create an HTML document that has two tables - a Member Name table, and a Member home Phone number table. • Hyperlink the Member's Name to his/her Phone. 39

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. <TABLE border="1" width="25%"> <TR><TH>Name</TH></TR> <xsl:

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. <TABLE border="1" width="25%"> <TR><TH>Name</TH></TR> <xsl: for-each select="/Fitness. Center/Member"> <TR> <TD> <A href="#{@id}"> <xsl: value-of select="Name"/> </A> </TD> </TR> </xsl: for-each> </TABLE> <BR/><BR/><BR/> <TABLE border="1" width="25%"> <TR><TH>Home Phone Number</TH></TR> <xsl: for-each select="/Fitness. Center/Member"> <TR> <TD> <A name="{@id}"> <xsl: value-of select="Phone[@type='home']"/> </A> </TD> </TR> </xsl: for-each> </TABLE> 40 (see html-example 08) Do Lab 1, Parts 5 -6

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 41 Numbering • There is

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 41 Numbering • There is an XSL element that returns a number corresponding to the element's position in the set of selected nodes <xsl: for-each select="/Fitness. Center/Member"> <xsl: number value="position()" format="1"/> <xsl: text>. </xsl: text> <xsl: value-of select="Name"/> <BR/> </xsl: for-each> (see html-example 09) Output: 1. Jeff 2. David 3. Roger

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Start Numbering from 0 •

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Start Numbering from 0 • How would you start the numbering from zero, rather than one? <xsl: number value="position() - 1" format="1"> 42

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. format attribute of xsl: number

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 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, 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, III, … – format=“i” generates: i, iii, . . . 43

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 44 format attribute of xsl:

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 44 format attribute of xsl: number <xsl: for-each select="/Fitness. Center/Member"> <xsl: number value="position()" format="A"/> <xsl: text>. </xsl: text> <xsl: value-of select="Name"/> <BR/> </xsl: for-each> Output: A. Jeff B. David C. Roger

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 45 Sorting • There is

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 45 Sorting • There is an XSL element that sorts the elements that you extract from the XML document <xsl: for-each select="/Fitness. Center/Member"> <xsl: sort select="Name" order="ascending"/> <xsl: value-of select="Name"/> <BR/> </xsl: for-each> (see html-example 10) "For each Member, sort the Name elements" Output: David Jeff Roger

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 46 Sorting <xsl: for-each select="/Fitness.

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 46 Sorting <xsl: for-each select="/Fitness. Center/Member"> <xsl: sort select="Name" order="ascending"/> <xsl: value-of select="Name"/> <BR/> </xsl: for-each> 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.

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. concat() function • concat(destination string,

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. concat() function • concat(destination string, string to add) • Note: if you want to concatenate more than one string to the destination string then simply add more arguments 47

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. <xsl: for-each select="/Fitness. Center/Member"> <xsl:

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. <xsl: for-each select="/Fitness. Center/Member"> <xsl: value-of select="concat('Welcome ', Name, '!')"/> <BR/> </xsl: for-each> Output: Welcome Jeff! Welcome David! Welcome Roger! 48

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 49 xsl: variable • This

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 49 xsl: variable • This XSL element allows you to create a variable to hold a value (which could be a string or a subtree of the XML document). • The variable is referenced by $variable-name <xsl: variable name=“hello” select=“'Hello World'”/> This creates a variable called hello, that has a value which is the literal string, ‘Hello World’. We could use this variable as follows: hello Hello World Value = <xsl: value-of select=“$hello”/> This will output: Value = Hello World

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Member's Phone Numbers: <TABLE border="1"

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Member's Phone Numbers: <TABLE border="1" width="25%"> <TR><TH>Name</TH><TH>Type</TH><TH>Number</TH></TR> <xsl: for-each select="/Fitness. Center/Member"> <xsl: variable name="name" select="Name"/> <xsl: for-each select="Phone"> <TR> <TD><xsl: value-of select="$name"/></TD> <TD><xsl: value-of select="@type"/></TD> <TD><xsl: value-of select=". "/></TD> </TR> </xsl: for-each> </TABLE> (see html-example 12) 50

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 51 xsl: variable <xsl: variable

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 51 xsl: variable <xsl: variable name=“member” select=“Member[1]”/> This creates a variable called member, that has a value which is a subtree. We could use this variable as follows: Member Name = <xsl: value-of select=“$member/Name”/> Home Phone = <xsl: value-of select=“$member/Phone[@type='home']”/> This will result in generating: Name = Jeff Home Phone = 555 -1234 Name Jeff Phone 555 -1234 Phone 555 -4321 . . .

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 52 xsl: variable • A

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 52 xsl: variable • A variable is “write once, read many”. – That is, you can assign a variable a value only once, but then you can retrieve the value of the variable many times. • A variable has a scope limited to the XSL element that it is nested within. Its scope starts where it is defined and extends to the end of the XSL element that it is nested within.

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 53 Member's Phone Numbers: <TABLE

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 53 Member's Phone Numbers: <TABLE border="1" width="25%"> <TR><TD>Name</TD><TD>Type</TD><TD>Number</TD></TR> <xsl: for-each select="/Fitness. Center/Member"> <xsl: variable name="name" select="Name"/> <xsl: for-each select="Phone"> <TR> <TD><xsl: value-of select="$name"/></TD> <TD><xsl: value-of select="@type"/></TD> <TD><xsl: value-of select=". "/></TD> </TR> </xsl: for-each> </TABLE> The name variable's life ends here Do Lab 2, Part 1

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Global Variables • You can

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Global Variables • You can create a variable outside of <xsl: template match="/">. Then, the variable is global. <? xml version="1. 0"? > <xsl: stylesheet xmlns: xsl="http: //www. w 3. org/1999/XSL/Transform" version="1. 0"> <xsl: output method="html"/> <xsl: variable name="pi" select="'3. 142857'"/> <xsl: template match="/" > <HTML> <HEAD> <TITLE>Value of Pi</TITLE> </HEAD> <BODY> The value of pi = <xsl: value-of select="$pi"/> </BODY> </HTML> </xsl: template> 54

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 55 Problem • Suppose that

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 55 Problem • Suppose that we want to create a variable, names, and we want this variable to contain a list of the Member Names, with each name separated by a slash. How would you create such a variable? Here’s what you might attempt to do: Member's Names: <xsl: variable name="names" select="/Fitness. Center/Member[1]/Name"/> <xsl: for-each select="/Fitness. Center/Member[position() > 1]"> <xsl: variable name="names" select="concat($names, '/')"/> <xsl: variable name="names" select="concat($names, Name)"/> </xsl: for-each> <xsl: value-of select="$names"/> Output: Jeff (see html-example 13)

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 56 Let’s add some statements

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 56 Let’s add some statements to trace this example <xsl: variable name="names" select="/Fitness. Center/Member[1]/Name"/> <xsl: value-of select="$names"/> <BR/> <xsl: for-each select="/Fitness. Center/Member[position() > 1]"> <xsl: variable name="names" select="concat($names, '/')"/> <xsl: value-of select="$names"/> <BR/> <xsl: variable name="names" select="concat($names, Name)"/> <xsl: value-of select="$names"/> <BR/> </xsl: for-each> <xsl: value-of select="$names"/> Output: (see html-example 14) Jeff/David Jeff/ <--- Why did we loose the previous Name? That name went out of scope. Jeff/Roger Jeff Obviously, this approach doesn’t work. So how do we do it?

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 57 Here’s what we would

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 57 Here’s what we would like to do names Open up the names box Jeff … / … David … / … Roger <xsl: for-each select="/Fitness. Center/Member"> Add this iteration’s Name and a slash to the open names box </xsl: for-each> Iterate through each name, adding into the open box names Close the box Jeff/David/Roger

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Problem - Solution In all

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Problem - Solution In all previous examples of creating a variable we declared the name of the variable and then had a select attribute which gave the variable its value. We can omit the select attribute: <xsl: variable name=“names”> - Do stuff in here. All output will go into the names “box”. </xsl: variable> 58

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 59 Problem - Solution Member's

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 59 Problem - Solution Member's Names: <xsl: variable name="names"> <xsl: value-of select="/Fitness. Center/Member[1]/Name"/> <xsl: for-each select="/Fitness. Center/Member[position() > 1]"> <xsl: text>/</xsl: text> <xsl: value-of select="Name"/> </xsl: for-each> </xsl: variable> <xsl: value-of select="$names"/> (see html-example 15) Output: Member's Names: Jeff/David/Roger

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 60 contains() function • contains(string

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 60 contains() function • contains(string to be tested, test string) returns true if string to be tested contains test string <xsl: if test=“contains($greeting, ‘welcome’)”> $greeting contains ‘welcome’ </xsl: if> Do Lab 2, Part 2

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 61 xsl: choose • xsl:

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 61 xsl: choose • xsl: choose allows you to elegantly express multiple conditional tests. Here’s the structure: <xsl: choose> <xsl: when test='something> [action] </xsl: when> <xsl: when test='something'> [action] </xsl: when> <xsl: otherwise> [action] </xsl: otherwise> </xsl: choose> The first xsl: when statement that evaluates to true is executed. If none evaluates to true then the xsl: otherwise statement is executed.

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Implementing an if-then-else • There

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Implementing an if-then-else • There is no if-then-else element in XSL. • However, there is an elegant way to do it: <xsl: choose> <xsl: when test="contains($member-list, 'Jeff')"> <xsl: text>Jeff is a member</xsl: text> </xsl: when> <xsl: otherwise> <xsl: text>No member by the name Jeff</xsl: text> </xsl: otherwise> </xsl: choose> 62

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 63 substring-before() String Function Here’s

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 63 substring-before() String Function Here’s the form of this string function: substring-before(string, pattern) Example: <xsl: variable name="phone" select="Phone"/> <xsl: value-of select="substring-before($phone, '-')"/> “Get the contents of Phone and put it into the variable called ‘phone’. Then extract from the content of ‘phone’ the string before the '-' (i. e. , the telephone exchange)”. phone 555 -1234 substring-before($phone, ‘-’) 555

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 64 substring-after() String Function Here’s

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 64 substring-after() String Function Here’s the form of this string function: substring-after(string, pattern) Example: <xsl: variable name="phone" select="Phone"/> <xsl: value-of select="substring-after($phone, '-')"/> “Get the contents of Phone and put it into the variable called ‘phone’. Then extract from the content of ‘phone’ the string after the '-'”. phone 555 -1234 substring-after($phone, ‘-’) 1234

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. starts-with() String Function Here’s the

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. starts-with() String Function Here’s the form of this string function: starts-with(string, pattern) Example: <xsl: if test="starts-with(Phone, '555')"> [action] </xsl: if> “If the Phone starts with the string, ‘ 555’ then do [action]”. 65

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. substring() function • substring(string, i,

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. substring() function • substring(string, i, len? ) returns the substring of string that starts at the ith position and has length, len. The length argument (len) is optional. If not present then this function returns the substring starting at the ith position all the way to the end of the string. Note: the first character is at position 1 (not 0 as with some languages) substring(‘ 1234567890’, 2, 5) returns ‘ 23456’ 66

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. string-length() function • string-length(string) returns

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. string-length() function • string-length(string) returns the length of the string-length(‘ 1234567890’) returns 10 67

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 68 translate() function translate(string, from-pattern,

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 68 translate() function translate(string, from-pattern, to-pattern) Example. translate(“Hello”, “ABCDEFGHIJKLMNOPQRSTUVWXYZ”, “abcdefghijklmnopqrstuvwxyz”); this will convert Hello to hello (i. e. , convert to lower case) A better approach to the above problem is: <xsl: variable name="upper. Case. Chars" select=" 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' "/> <xsl: variable name="lower. Case. Chars" select=" 'abcdefghijklmnopqrstuvwxyz' "/> translate(“Hello”, $upper. Case. Chars, $lower. Case. Chars) Do Lab 2, Part 3 Note: need to put the string within (single) quotes, otherwise the XSL Processor will try to interpret it as an XML element.

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Boolean and Relational Operators •

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Boolean and Relational Operators • Boolean operators: not, and, or • Relational operators: <, >, =, <=, >=, != • The less than and greater than signs are reserved symbols, so they need to be escaped when you use them. Thus, the relational operators will appear in your XSL code like this: Want this: < > = <= >= != Use this: < > = < = > = != 69

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 70 Arithmetic • The arithmetic

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 70 Arithmetic • The arithmetic operators available: +, -, *, div, mod (remainder from doing a division) – Note: recall that an XML element can have a dash in the name. So, if you want to indicate subtraction, be sure to surround “-” with blank spaces.

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Arithmetic functions • sum(node set)

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Arithmetic functions • sum(node set) this function sums up all the values in the set of nodes • floor(number) returns the largest integer that is not greater than number – Example. floor(2. 5) returns 2 • ceiling(number) returns the smallest integer that is not less than number – Example. Ceiling(2. 5) returns 3 • round(number) returns the integer closest to number – Example. round(2. 3) returns 2 71

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 72 Enhanced XML Document <?

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 72 Enhanced XML Document <? xml version="1. 0"? > <Fitness. Center> <Member id="1" level="platinum"> <Name>Jeff</Name> <Phone type="home">555 -1234</Phone> <Phone type="work">555 -4321</Phone> <Favorite. Color>lightgrey</Favorite. Color> <Membership. Fee>340</Membership. Fee> </Member> <Member id="2" level="gold"> <Name>David</Name> <Phone type="home">383 -1234</Phone> <Phone type="work">383 -4321</Phone> <Favorite. Color>lightblue</Favorite. Color> <Membership. Fee>500</Membership. Fee> </Member> <Member id="3" level="platinum"> <Name>Roger</Name> <Phone type="home">888 -1234</Phone> <Phone type="work">888 -4321</Phone> <Favorite. Color>lightyellow</Favorite. Color> <Membership. Fee>340</Membership. Fee> </Member> </Fitness. Center> Note that each Member now has Membership. Fee element

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Compute Membership Revenue Membership Fee

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Compute Membership Revenue Membership Fee Revenue: <xsl: value-of select="sum(//Membership. Fee)"/> (see html-example 16) 73

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. xsl: attribute • This XSL

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. xsl: attribute • This XSL element is used by nesting it within an output element. It enables you to create an attribute for the output element 74

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Coloring alternate rows Member Names:

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Coloring alternate rows Member Names: <TABLE border="1" width="25%"> <xsl: for-each select="/Fitness. Center/Member"> <TR> <xsl: if test="position() mod 2 = 0"> <xsl: attribute name="bgcolor">yellow</xsl: attribute> </xsl: if> <TD><xsl: value-of select="Name"/></TD> </TR> </xsl: for-each> </TABLE> (see html-example 17) For each even row of the table, the TR value will be: <TR bgcolor="yellow"> 75

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 76 count() function count(set of

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 76 count() function count(set of node) returns an integer representing the number of nodes (i. e. , XML elements) in the set. Example. Number of members = <xsl: value-of select="count(//Member)"/> Output: Number of members = 5 Do Lab 2, Part 4

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 77 Selecting all Elements/Attributes <xsl:

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 77 Selecting all Elements/Attributes <xsl: for-each select="/Fitness. Center/Member"> <xsl: for-each select="@*">. . . </xsl: for-each> <xsl: for-each select="*">. . . </xsl: for-each> For each attribute do. . . For each child element do. . .

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Getting the Name of the

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Getting the Name of the Element/Attribute using the name() Function <xsl: for-each select="/Fitness. Center/Member"> <xsl: for-each select="@*"> Attribute = <xsl: value-of select="name(. )"/> </xsl: for-each> <xsl: for-each select="*"> Element = <xsl: value-of select="name(. )"/> </xsl: for-each> (see html-example 19) name(node) returns the name of "node" 78

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. When to use Curly Braces?

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. When to use Curly Braces? • “When I assign an attribute a value, when do I use curly braces and when do I not use them? ” Use curly braces for these attributes: - the attribute of a literal result element (where you literally type what should be output) Example: <a href=“#{@id}”> - the name attribute of xsl: attribute Example: <xsl: attribute name =“{@value}”> - the name attribute of xsl: pi Example: <xsl: pi name =“{@value}”> - the name attribute of xsl: element Example: <xsl: element name =“{@value}”> - the optional attributes of xsl: sort: Example: <xsl: sort order =“{@value}”> lang =“{@value}”> data-type =“{@value}”> case-order =“{@value}”> 79

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. document( ) Function • This

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. document( ) Function • This function enables you to access other XML documents (besides the XML document that you specify when you invoke the XSL Processor). • The format for using the document() function is: document(url), where url is a URL to another XML document 80

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Fitness Centers Merger • Another

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Fitness Centers Merger • Another fitness center has just merged with us. They have an xml document (Fitness. Center 2. xml) containing their Members. • You are to create an XSL-enhanced HTML document that creates a single table comprised of all the Members from both fitness clubs. 81

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 82 <TABLE border="1" width="75%"> <TR><TH>Name</TH><TH>Phone(home)</TH>.

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 82 <TABLE border="1" width="75%"> <TR><TH>Name</TH><TH>Phone(home)</TH>. . . </TR> <xsl: for-each select="/Fitness. Center/Member"> <TR> <TD><xsl: value-of select="Name"/></TD> <TD><xsl: value-of select="Phone[@type='home']"/></TD> <TD><xsl: value-of select="Phone[@type='work']"/></TD> <TD><xsl: value-of select="Favorite. Color"/></TD> </TR> </xsl: for-each> <xsl: variable name="fitness. Center 2" select="document('file: //localhost/xml-course/. . . /Fitness. Center 2. xml')"/> <xsl: for-each select="$fitness. Center 2/Fitness. Center/Member"> <TR> <TD><xsl: value-of select="Name"/></TD> <TD><xsl: value-of select="Phone[@type='home']"/></TD> <TD><xsl: value-of select="Phone[@type='work']"/></TD> <TD><xsl: value-of select="Favorite. Color"/></TD> </TR> </xsl: for-each> Do Lab 3, </TABLE> Part 1 (see html-example 20)

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Parameterized Processing • You can

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Parameterized Processing • You can create a subroutine (called a named template), and you can pass to it parameters. 83

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. <xsl: template match="/"> <HTML> <HEAD>

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. <xsl: template match="/"> <HTML> <HEAD> <TITLE>Fitness Center</TITLE> </HEAD> <BODY> <xsl: call-template name="display. Name. With. Font"> <xsl: with-param name="font. Face" select="'Impact'"/> <xsl: with-param name="name" select="/Fitness. Center/Member[1]/Name"/> </xsl: call-template> <BR/>. . . </BODY> </HTML> </xsl: template> <xsl: template name="display. Name. With. Font"> <xsl: param name="font. Face" select="'Braggadocio'"/> <!-- default font --> <xsl: param name="name"/> <FONT face="{$font. Face}"> <xsl: value-of select="$name"/> </FONT> </xsl: template> (see html-example 21) 84

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Call by Reference • How

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Call by Reference • How do we create a named template that returns a value? • Example: create a named template which, when passed a number, it returns the number div 2. 85

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 86 <xsl: template match="/"> <HTML>

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 86 <xsl: template match="/"> <HTML> <HEAD> <TITLE>Fitness Center</TITLE> </HEAD> <BODY> 16 / 2 = <xsl: variable name="result"> <xsl: call-template name="Num. Div 2"> <xsl: with-param name="N" select="16"/> </xsl: call-template> </xsl: variable> <xsl: value-of select="$result"/> </BODY> </HTML> </xsl: template> <xsl: template name="Num. Div 2"> <xsl: param name="N"/> <xsl: value-of select="$N div 2"/> </xsl: template> (see html-example 22)

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 87 Problem: Determine if all

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 87 Problem: Determine if all <Number> values are <= 100 <? xml version="1. 0"? > <Number. List> <Number>23</Number> <Number>41</Number> <Number>70</Number> <Number>103</Number> <Number>99</Number> <Number>6</Number> </Number. List> (see html-example 22 -1)

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. <xsl: template match="/"> <xsl: text>All

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. <xsl: template match="/"> <xsl: text>All numbers in the list are less than or equal to 100: </xsl: text> <xsl: variable name="result"> <xsl: call-template name="All. Less. Than 100"> <xsl: with-param name="number. List" select="Number. List/Number"/> </xsl: call-template> </xsl: variable> <xsl: value-of select="$result"/> </xsl: template> <xsl: template name="All. Less. Than 100"> <xsl: param name="number. List"/> <xsl: choose> <xsl: when test="not($number. List)"> <xsl: text>true</xsl: text> </xsl: when> <xsl: otherwise> <xsl: choose> <xsl: when test="$number. List[1] > 100"> <xsl: text>false</xsl: text> </xsl: when> <xsl: otherwise> <xsl: call-template name="All. Less. Than 100"> <xsl: with-param name="number. List" select="$number. List[position() > 1]"/> </xsl: call-template> </xsl: otherwise> </xsl: choose> </xsl: template> 88 Pass to the named template a list of <Number> nodes, i. e. , a nodelist. This is a recursive routine If the nodelist is empty then return true Check the first node on the list. If it's greater than 100, then return false (and we're done). Otherwise, recurse over the remaining nodes. Do Lab 3, Part 2

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. generate-id() • Use this function

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. generate-id() • Use this function to generate a unique string for a node Example. generate-id(/Fitness. Center/Member[1]) will return a unique id for the first Member 89

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Using generate-id() to Uniquely Identify

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Using generate-id() to Uniquely Identify Elements • In html-example 08 we created two tables - a table containing the Members Names, and a separate table containing home Phone numbers. Each Name was hyperlinked to his/her home Phone. We used the id attribute on each Member element to link the two tables together. • Suppose there is no id attribute. We can use generate-id() to create a unique identifier. 90

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. <TABLE border="1" width="25%"> <TR><TH>Name</TH></TR> <xsl:

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. <TABLE border="1" width="25%"> <TR><TH>Name</TH></TR> <xsl: for-each select="/Fitness. Center/Member"> <TR> <TD> <A href="#{generate-id(. )}"> <xsl: value-of select="Name"/> </A> </TD> </TR> </xsl: for-each> </TABLE> <BR/><BR/><BR/> <TABLE border="1" width="25%"> <TR><TH>Home Phone Number</TH></TR> <xsl: for-each select="/Fitness. Center/Member"> <TR> <TD> <A name="{generate-id(. )}"> <xsl: value-of select="Phone[@type='home']"/> </A> </TD> </TR> </xsl: for-each> </TABLE> 91 (see html-example 23)

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Same (XML) Data, Multiple Views

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Same (XML) Data, Multiple Views • In html-example 24 I show to create an HTML document that allows a client to view an XML document in different forms. • Look at html-example 24. With the buttons on the left side of the screen we can select which view is desired. 92

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 93 Multiple Stylesheets to provide

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 93 Multiple Stylesheets to provide the different views Fitness. Center. xml Show. Members. xsl Show. Platinum. Members. xsl Show. Gold. Members. xsl Show. All. xsl

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 94 Fitness. Center. html -

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 94 Fitness. Center. html - comprised of two frames Show. Members Show. Platinum. Members Show. Gold. Members Show. All Raw. XML Body. html Controls. html Fitness. Center. html

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 95 Fitness. Center. html <HTML>

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 95 Fitness. Center. html <HTML> <HEAD> <TITLE>Welcome</TITLE> </HEAD> <FRAMESET cols="20%, 80%"> <FRAME src="controls. html" name="controls. Frame"> <FRAME src="body. html" name="body. Frame"> </FRAMESET> </HTML> Two columns. The first column is 20% of the width of the screen. The second column is 80% of the width of the screen. Fitness. Center. html NOTE: do NOT have a <BODY> element (it won't work if you do)

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 96 Body. html <HTML> <HEAD>

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 96 Body. html <HTML> <HEAD> <TITLE>Fitness Center</TITLE> </HEAD> <BODY> </HTML> Body. html This document is very simple - it's empty! The body will be filled in with the HTML that is generated by styling the XML document

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 97 Controls. html (tracing through

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 97 Controls. html (tracing through the actions that occur when a user selects "Show. Gold. Members") Press Show Gold Members button Invoke the Javascript function, Show. Gold. Members() Load Show. Gold. Members. xsl Transform (the previously loaded) Fitness. Center. xml using Show. Gold. Members. xsl Set the body of Body. html to the generated html

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Controls. html (code to Show.

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Controls. html (code to Show. Gold. Members) <FORM name="control. Form"> <INPUT type="button" value="Show Gold Members" onclick="Show. Gold. Members </FORM> This is preloading the XML document <SCRIPT language="JScript" defer="true"> var xml = new Active. XObject("Msxml 2. DOMDocument. 3. 0"); xml. async = false; xml. load("Fitness. Center. xml"); function Show. Gold. Members() { Load the XSL document Transform the XML document using the stylesheet, and assign the body of Body. html to the generated html. var xsl = new Active. XObject("Msxml 2. DOMDocument. 3. 0"); xsl. async = false; xsl. load("Show. Gold. Members. xsl"); parent. body. Frame. document. body. inner. HTML = xml. transform. Node(xsl); } </SCRIPT> 98

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Using SAXON in a Browser

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Using SAXON in a Browser (rather the XSL Processor built into IE) • The last example utilized the XSL Processor built into IE (msxml) to do the transformations. • Suppose the you would like to use a different XSL Processor, e. g. , SAXON? – Why would you want to do this? Answer: SAXON has several capabilities that msxml does not have. • SAXON comes with a Java applet that you can use to do XSL processing within a browser. • See html-example-24 -a for details on how to use SAXON's Java applet to implement the multi-button example. • See html-example-24 -b for an example of how to use SAXON as the XSL Processor within a browser. 99

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 100 Inserting spaces into HTML

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 100 Inserting spaces into HTML output <xsl: text disable-output-escaping="yes"><![CDATA[  ]]></xsl: text> Put one character references for each space required

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Debugging your Stylesheets using xsl:

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Debugging your Stylesheets using xsl: message • xsl: message is used to display a message, and (optionally) terminate execution of the stylesheet. • The message is sent to the screen, not to the output file. • This provides a very nice way to monitor the flow of your stylesheet, without impacting the output file. 101

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 102 Example using xsl: message

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 102 Example using xsl: message <? xml version="1. 0"? > <xsl: stylesheet xmlns: xsl="http: //www. w 3. org/1999/XSL/Transform" version="1. 0"> <xsl: output method="html"/> <xsl: template match="/"> <HTML> <BODY> <xsl: for-each select="Fitness. Center/Member"> <xsl: if test="Membership. Fee < 0"> <xsl: message terminate="yes"> <xsl: text>Invalid Membership. Fee</xsl: text> </xsl: message> </xsl: if> </xsl: for-each> <xsl: text>All the Membership. Fee elements are valid</xsl: text> </BODY> </HTML> </xsl: template> </xsl: stylesheet> See html-example 26 Two possible values for terminate - yes, or no. terminate="yes" means that you want the message output to the screen and then the program stopped. terminate="no" means that you want the message output to the screen and the program to continue executing.

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. system-property() function • The system-property(property

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. system-property() function • The system-property(property name) function enables you to obtain information about the XSL Processor that you are using: – xsl: vendor - if you specify this as the value for property name then it will return the name of the XSL Processor vendor – xsl: vendor - this provides the URL to the vendor's web site – xsl: version - this indicates what version of the XSL spec is implemented. 103

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 104 Recommended Practice <? xml

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 104 Recommended Practice <? xml version="1. 0"? > <xsl: stylesheet xmlns: xsl="http: //www. w 3. org/1999/XSL/Transform" version="1. 0"> <xsl: output method="html"/> <xsl: template match="/"> <xsl: message> XSLT Processor: <xsl: value-of select="system-property('xsl: vendor')"/> </xsl: message> <HTML> <HEAD> <TITLE>Welcome</TITLE> </HEAD> <BODY> Welcome! </BODY> </HTML> </xsl: template> </xsl: stylesheet> See html-example 27 It is very good practice to add this to the start of every stylesheet. This will give you a message indicating which XSL Processor you are using. When might you be uncertain which XSL Processor is being used? Java comes bundled with xalan. Suppose that you want to write a Java program which uses Saxon instead. You will definitely want to use the above to ensure that you are invoking Saxon and not the built-in xalan. (I know. I have made the mistake of thinking that I was using Saxon when in fact I was using xalan. )

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 105 Embedded Stylesheets • You

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 105 Embedded Stylesheets • You can embed a stylesheet within an XML document. <? xml version="1. 0"? > <!DOCTYPE Fitness. Center [ <!ATTLIST xsl: stylesheet id ID #REQUIRED> ]> <? xml-stylesheet type="text/xml" href="#embed"? > <Fitness. Center> <Member level="platinum"> <Name>Jeff</Name> <Phone type="home">555 -1234</Phone> <Phone type="work">555 -4321</Phone> <Favorite. Color>lightgrey</Favorite. Color> </Member>. . . <xsl: stylesheet id="embed" xmlns: xsl="http: //www. w 3. org/1999/XSL/Transform" version="1. 0">. . . </xsl: stylesheet> </Fitness. Center> Stylesheet embedded within the XML document

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 106 Embedded Stylesheets You must

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 106 Embedded Stylesheets You must indicate that the id attribute is of type ID. <? xml version="1. 0"? > <!DOCTYPE Fitness. Center [ <!ATTLIST xsl: stylesheet id ID #REQUIRED> ]> <? xml-stylesheet type="text/xml" href="#embed"? > <Fitness. Center> <Member level="platinum"> <Name>Jeff</Name> <Phone type="home">555 -1234</Phone> <Phone type="work">555 -4321</Phone> <Favorite. Color>lightgrey</Favorite. Color> </Member>. . . <xsl: stylesheet id="embed" xmlns: xsl="http: //www. w 3. org/1999/XSL/Transform" version="1. 0"> The stylesheet PI references the embeded stylesheet (as indicated by the"#" sign) Add an id attribute to the xsl: stylesheet element. . </xsl: stylesheet> </Fitness. Center> See html-example 28 (Note: not all XSL Processors support embedded stylesheets)