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

Slides:



Advertisements
Similar presentations
XML-XSL Introduction SHIJU RAJAN SHIJU RAJAN Outline Brief Overview Brief Overview What is XML? What is XML? Well Formed XML Well Formed XML Tag Name.
Advertisements

1 Roger L. Costello 16 June 2010 XQuery
1 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Using XSLT and XPath Roger L. Costello (XML Technologies) With changes and additions by.
Advanced XSLT. Branching in XSLT XSLT is functional programming –The program evaluates a function –The function transforms one structure into another.
1 XSLT – eXtensible Stylesheet Language Transformations Modified Slides from Dr. Sagiv.
Introduction to C Programming
16 HTML Tables and Frames Section 16.1 Create a basic table using HTML Define borders Merge cells Align content in tables Section 16.2 Create a frames-based.
Introduction to XSLT Transparency No. 1 Introduction to XSLT Cheng-Chia Chen.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
XSL Transformations (XSLT) Meghasyam Bokam April’1 st, 2002.
A note on generating text with the xsl:value-of instruction.
Introduction to XSLT Transparency No. 1 Introduction to XSLT Cheng-Chia Chen.
1 Copyright (c) [2000]. Roger L. Costello. All Rights Reserved. Using XSLT and XPath to Transform XML Documents Roger L. Costello XML Technologies.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
1 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Using XSLT and XPath to Enhance HTML Documents Roger L. Costello XML Technologies.
Introduction to C Programming
XML Technologies and Applications Rajshekhar Sunderraman Department of Computer Science Georgia State University Atlanta, GA 30302
1 Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. Using Extension Elements and Extension Functions with XSLT and XPath Roger L. Costello.
17 Apr 2002 XML Stylesheets Andy Clark. What Is It? Extensible Stylesheet Language (XSL) Language for document transformation – Transformation (XSLT)
1 Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. Using Extension Elements and Extension Functions with XSLT and XPath Roger L. Costello.
Using XSLT and XPath to Enhance HTML Documents Reference: Roger L. Costello
XSLT Part 3B. id() The id() function returns a node-set containing the node or nodes with a given ID attribute. An ID attribute in this context is any.
ECA 228 Internet/Intranet Design I Intro to XSL. ECA 228 Internet/Intranet Design I XSL basics W3C standards for stylesheets – CSS – XSL: Extensible Markup.
Computer Science 1000 Spreadsheets II Permission to redistribute these slides is strictly prohibited without permission.
CSE3201/CSE4500 XPath. 2 XPath A locator for elements or attributes in an XML document. XPath expression gives direction.
XML for E-commerce III Helena Ahonen-Myka. In this part... n Transforming XML n Traversing XML n Web publishing frameworks.
XSLT for Data Manipulation By: April Fleming. What We Will Cover The What, Why, When, and How of XSLT What tools you will need to get started A sample.
XP New Perspectives on XML Tutorial 6 1 TUTORIAL 6 XSLT Tutorial – Carey ISBN
WORKING WITH XSLT AND XPATH
1 CIS336 Website design, implementation and management (also Semester 2 of CIS219, CIS221 and IT226) Lecture 6 XSLT (Based on Møller and Schwartzbach,
ECA 228 Internet/Intranet Design I XSLT Example. ECA 228 Internet/Intranet Design I 2 CSS Limitations cannot modify content cannot insert additional text.
CITA 330 Section 6 XSLT. Transforming XML Documents to XHTML Documents XSLT is an XML dialect which is declared under namespace "
XSLT Kanda Runapongsa Dept. of Computer Engineering Khon Kaen University.
Transforming Documents „a how-to of transforming xml documents“ Lecture on Walter Kriha.
ACG 6415 XSLT Presenting XML and XBRL. Re-Purpose  The main benefit of XML / XBRL Reusability of Data contained in Instance Document We need a method.
1 Overview of XSL. 2 Outline We will use Roger Costello’s tutorial The purpose of this presentation is  To give a quick overview of XSL  To describe.
XP New Perspectives on XML, 2 nd Edition Tutorial 8 1 TUTORIAL 8 CREATING ELEMENT GROUPS.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
 2000 Deitel & Associates, Inc. All rights reserved. Outline 8.1Introduction 8.2A Simple Program: Printing a Line of Text in a Web Page 8.3Another JavaScript.
1 Copyright (c) [2000]. Roger L. Costello. All Rights Reserved. Using XSLT and XPath to Transform XML Documents Roger L. Costello XML Technologies.
XSLT. XSLT stands for Extensible Stylesheet Language Transformations XSLT is used to transform XML documents into other kinds of documents. XSLT can produce.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
XP New Perspectives on XML, 2 nd Edition Tutorial 7 1 TUTORIAL 7 CREATING A COMPUTATIONAL STYLESHEET.
ACG 4401 XSLT Extensible Stylesheet Language for Transformations Presenting XML and XBRL.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Part:2.  Keywords are words with special meaning in JavaScript  Keyword var ◦ Used to declare the names of variables ◦ A variable is a location in the.
XP 1 XSLT II Robin Burke ECT 360. XP 2 Outline Conditionals Numbering Functions and operators Variables and parameters Named and recursive templates.
XPath --XML Path Language Motivation of XPath Data Model and Data Types Node Types Location Steps Functions XPath 2.0 Additional Functionality and its.
XP 1 XSLT II Robin Burke ECT 360. XP 2 Homework #4 Forgot we hadn't covered "choose" becomes extra credit My books.xml didn't include descriptions.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
ACG 4401 XSLT Extensible Stylesheet Language for Transformations Presenting XML and XBRL.
Displaying Data with XSLT ©NIITeXtensible Markup Language/Lesson 6/Slide 1 of 45 Objectives In this lesson, you will learn to: * Perform conditional formatting.
5 Copyright © 2004, Oracle. All rights reserved. Navigating XML Documents by Using XPath.
REEM ALMOTIRI Information Technology Department Majmaah University.
XML Schema – XSLT Week 8 Web site:
1 XSL Transformations (XSLT). 2 XSLT XSLT is a language for transforming XML documents into XHTML documents or to other XML documents. XSLT uses XPath.
XML Notes taken from w3schools. What is XML? XML stands for EXtensible Markup Language. XML was designed to store and transport data. XML was designed.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
1 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Using XSLT and XPath to Enhance HTML Documents Roger L. Costello XML Technologies.
CH 15 XSL Transformations 1. Objective What is XSL? Overview of XSL transformations Understanding XSL templates Computing the value of a node with xsl:value-of.
1 XSLT XSLT (extensible stylesheet language – transforms ) is another language to process XML documents. Originally intended as a presentation language:
Chapter 6 JavaScript: Introduction to Scripting
“Under the hood”: Angry Birds Maze
Using XSLT and XPath to Transform XML Documents
Using XSLT and XPath to Enhance HTML Documents
Introduction to Scripting
ACG 4401 XSLT Extensible Stylesheet Language for Transformations
WEB PROGRAMMING JavaScript.
T. Jumana Abu Shmais – AOU - Riyadh
Web Programming and Design
Presentation transcript:

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

2 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

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

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

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

6 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. xalan/xt/saxon xalan: A free XSL processor, implemented in Java, from Apache ( xt: A free XSL processor, implemented in Java, from James Clark ( saxon: A free XSL processor, implemented in Java, from Michael Kay ( xalan/xt/saxon XML XSL HTML (or XML or text) Invoking from a DOS command line: run-xalan FitnessCenter.xml FitnessCenter.xsl FitnessCenter.html run-xt FitnessCenter.xml FitnessCenter.xsl FitnessCenter.html run-saxon FitnessCenter.xml FitnessCenter.xsl FitnessCenter.html

7 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Styling XML Documents using IE6 or Netscape7 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. Jeff lightgrey Add this stylesheet PI to the top of your XML document

8 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

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

10 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Enhancing HTML Documents with the Following XML Data Jeff lightgrey FitnessCenter.xml

11 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Embed HTML Document in an XSL Template <xsl:stylesheet xmlns:xsl=" version="1.0"> Welcome Welcome! FitnessCenter.xsl (see html-example01) Note how we have the HTML document embedded within an XSL template

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

13 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Extracting the Member Name <xsl:stylesheet xmlns:xsl=" version="1.0"> Welcome Welcome ! (see html-example02)

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

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

16 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Document / PI Element FitnessCenter Element Member Element Name Element Phone Element Phone Element FavoriteColor Text Jeff Text Text Text lightgrey

17 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Extract the FavoriteColor and use it as the bgcolor <xsl:stylesheet xmlns:xsl=" version="1.0"> Welcome Welcome ! (see html-example03)

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

19 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Extract the Home Phone Number <xsl:stylesheet xmlns:xsl=" version="1.0"> Welcome Welcome ! Your home phone number is: (see html-example04)

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

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

22 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Fruit Color Papaya Red Banana Yellow Fruit Color Papaya Red Banana Yellow

23 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.

24 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. <xsl:stylesheet xmlns:xsl=" version="1.0"> Welcome Welcome ! Your phone numbers are: Type Number (see html-example05)

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

26 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 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) Do Lab1, Parts 1-3

27 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".

28 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Welcome Welcome ! Our special offer to platinum members today is... Your phone numbers are: Type Number (see html-example06)

29 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Conditional Processing Use the element to perform conditional processing. Do Lab1, Part 4

30 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.

31 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Welcome Welcome ! Our special offer to platinum members today is... Your phone numbers are: Name Type Number (see html-example07)

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

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

34 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Other ways to Access the XML Data (cont.) <!- - Process all Name elements which have FitnessCenter as an ancestor - ->

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

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

37 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Enhanced XML Document Jeff lightgrey David lightblue Roger lightyellow Note that each Member now has a unique id (the id attribute)

38 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Review - HTML Hyperlinking … Click Here... This creates an internal hyperlink (the source "anchor" links to the target anchor).

39 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.

40 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Name Home Phone Number (see html-example08) Do Lab1, Parts 5-6

41 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Numbering There is an XSL element that returns a number corresponding to the element's position in the set of selected nodes (see html-example09). Output: 1. Jeff 2. David 3. Roger

42 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Start Numbering from 0 How would you start the numbering from zero, rather than one?

43 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, 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,...

44 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. format attribute of xsl:number. Output: A. Jeff B. David C. Roger

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

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

47 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

48 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Output: Welcome Jeff! Welcome David! Welcome Roger!

49 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Hello World 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 This creates a variable called hello, that has a value which is the literal string, ‘Hello World’. We could use this variable as follows: Value = This will output: Value = Hello World hello

50 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Member's Phone Numbers: Name Type Number (see html-example12)

51 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. xsl:variable This creates a variable called member, that has a value which is a subtree. We could use this variable as follows: Name = Home Phone = This will result in generating: Name = Jeff Home Phone = Member... Name Phone Jeff

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

53 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Member's Phone Numbers: Name Type Number The name variable's life ends here Do Lab2, Part 1

54 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Global Variables You can create a variable outside of. Then, the variable is global. <xsl:stylesheet xmlns:xsl=" version="1.0"> Value of Pi The value of pi =

55 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 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: Output: Jeff (see html-example13)

56 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Let’s add some statements to trace this example Output: Jeff Jeff/ 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? (see html-example14)

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

58 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: - Do stuff in here. All output will go into the names “box”.

59 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Problem - Solution Member's Names: / (see html-example15) Output: Member's Names: Jeff/David/Roger

60 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. contains() function contains(string to be tested, test string) returns true if string to be tested contains test string $greeting contains ‘welcome’ Do Lab2, Part 2

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

62 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: Jeff is a member No member by the name Jeff

63 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. substring-before() String Function Here’s the form of this string function: substring-before(string, pattern) Example: “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)” substring-before($phone, ‘-’) 555 phone

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

65 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: [action] “If the Phone starts with the string, ‘555’ then do [action]”.

66 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(‘ ’, 2, 5) returns ‘23456’

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

68 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 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: translate(“Hello”, $upperCaseChars, $lowerCaseChars) Note: need to put the string within (single) quotes, otherwise the XSL Processor will try to interpret it as an XML element. Do Lab2, Part 3

69 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:

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

71 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

72 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Enhanced XML Document Jeff lightgrey 340 David lightblue 500 Roger lightyellow 340 Note that each Member now has MembershipFee element

73 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Compute Membership Revenue Membership Fee Revenue: (see html-example16)

74 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

75 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Coloring alternate rows Member Names: yellow (see html-example17) For each even row of the table, the TR value will be:

76 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 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 = Output: Number of members = 5 Do Lab2, Part 4

77 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Selecting all Elements/Attributes For each attribute do... For each child element do...

78 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Getting the Name of the Element/Attribute using the name() Function Attribute = Element = (see html-example19) name(node) returns the name of "node"

79 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: - the name attribute of xsl:attribute Example: - the name attribute of xsl:pi Example: - the name attribute of xsl:element Example: - the optional attributes of xsl:sort: Example: lang data-type case-order

80 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

81 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 (FitnessCenter2.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.

82 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Name Phone(home)... <xsl:variable name="fitnessCenter2" select="document('file://localhost/xml-course/.../FitnessCenter2.xml')"/> (see html-example20) Do Lab3, Part 1

83 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.

84 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Fitness Center <xsl:with-param name="name" select="/FitnessCenter/Member[1]/Name"/>... (see html-example21)

85 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.

86 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Fitness Center 16 / 2 = (see html-example22)

87 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Problem: Determine if all values are <= (see html-example22-1)

88 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. All numbers in the list are less than or equal to 100: true false Do Lab3, Part 2 Pass to the named template a list of 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.

89 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(/FitnessCenter/Member[1]) will return a unique id for the first Member

90 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Using generate-id() to Uniquely Identify Elements In html-example08 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.

91 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Name Home Phone Number (see html-example23)

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

93 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Multiple Stylesheets to provide the different views FitnessCenter.xml ShowMembers.xsl ShowPlatinumMembers.xslShowGoldMembers.xslShowAll.xsl

94 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. FitnessCenter.html - comprised of two frames FitnessCenter.html Controls.html Body.html ShowMembers ShowPlatinumMembers ShowGoldMembers ShowAll RawXML

95 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. FitnessCenter.html Welcome FitnessCenter.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. NOTE: do NOT have a element (it won't work if you do)

96 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Body.html Fitness Center 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

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

98 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Controls.html (code to ShowGoldMembers) <INPUT type="button" value="Show Gold Members" onclick="ShowGoldMembers var xml = new ActiveXObject("Msxml2.DOMDocument.3.0"); xml.async = false; xml.load("FitnessCenter.xml"); function ShowGoldMembers() { var xsl = new ActiveXObject("Msxml2.DOMDocument.3.0"); xsl.async = false; xsl.load("ShowGoldMembers.xsl"); parent.bodyFrame.document.body.innerHTML = xml.transformNode(xsl); } This is preloading the XML document Load the XSL document Transform the XML document using the stylesheet, and assign the body of Body.html to the generated html.

99 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Inserting spaces into HTML output Put one character references for each space required