Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Using XSLT and XPath Roger L. Costello (XML Technologies) With changes and additions by.

Similar presentations


Presentation on theme: "1 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Using XSLT and XPath Roger L. Costello (XML Technologies) With changes and additions by."— Presentation transcript:

1 1 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Using XSLT and XPath Roger L. Costello (XML Technologies) With changes and additions by Thomas Krichel http://openlib.org/home/krichel

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

3 3 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Thomas acknowledgement Roger L. Costello Mitre Corp.

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

5 5 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Doing it using Internet Explorer First, download the latest version of Internet Explorer (at this time it is 6.0) Write an XSL stylesheet stylish.xsl Write an XML file, and refer to the xsl stylesheet with a processing instruction Note: this does not work with other browsers!

6 6 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. General stuff Part 0

7 7 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. XML tree XSL has a model of XML as a tree. XSL tree model is similar to the DOM model. As the processor does its job it looks at elements of the input tree and transforms them to the output tree. The processor only writes the file to the tree at the end. End points in the tree are called nodes.

8 8 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. in the general section we examine how XSL looks at an XML document. In fact it builds a tree. and then we look at a very simple way to look at what the stylesheet does. After that we have Roger showing us the details.

9 9 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Seven types of nodes root node: contains all the elements in the document. Not to be confused with the document element of XML. element node: contains an element text node: contain an as-large-as-possible area of text. attribute node: contains attribute name and value comment node: contains a comment processing instruction (p-i) node namespace node: each element node has one namespace node for every namespace declaration

10 10 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. properties of nodes: name This is empty for the root, text and comment nodes. for elments and attribute node, it is the name as it appears in the xml file, expanded by namespace declarations. for p-i nodes, it is the target for a namespace node, it is the prefix

11 11 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. properties of nodes: string value for text nodes: the text for comment nodes: the text of the comment for p-i nodes: the data part of the p-i. for an attribute node: the value of the attribute for a root node: the concatenation of all the string values of all element and text children. for a namespace node: the URI of the namespace

12 12 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. properties of nodes: base URI for all nodes: the URI of the XML source document where the node has been found Only of interest for elements and p-i nodes for the root node: the URI of the document for attribute, text and comment nodes: the base URI of its parent node

13 13 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. properties of nodes: children for element nodes: all the element nodes, text nodes, p-i nodes and comment nodes between its start and end tags. for root nodes: all the element nodes, text nodes, p-i nodes and comment nodes that are not children of some other node.

14 14 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. parent node for all nodes except root nodes: the parent of the node. attribute nodes and namespace nodes have an element node as parent node, but are not considered to be its child.

15 15 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. property of nodes: attribute element: one to many attributes that the element has other nodes: empty Now we look at what XSL does

16 16 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Different formats… is the default used for everything else. Final formatting may be up to formatting objects, anyway. Your stylesheet processor may have more formats, but they will be vendor-specific.

17 17 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. templates set rules do some stuff This is a rule that says, if you find a node that matches the expression expression, then go ahead and do some stuff. It is called a template. The fact that a rule is written down down does not imply that it is applied.

18 18 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. applying templates says: apply all template rules on the current node and on all its child nodes.

19 19 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Default, built-in rules for the nodes root: on all children element: to the current node and all its children attribute: copy the value as text to the output text: copy the text to the output comment, p-i, namespace: do nothing

20 20 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Generating HTML part I

21 21 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

22 22 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

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

24 24 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Embed HTML Document in an XSL Template Welcome Welcome! FitnessCenter.xsl (see html-example01)

25 25 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Note The HTML is embedded within an XSL template, which is an XML document. T he HTML must be well formed. 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.

26 26 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Extracting the Member Name <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> Welcome Welcome ! (see html-example02)

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

28 28 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 555-1234 Text 555-4321 Text lightgrey

29 29 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Use FavoriteColor as the bgcolor <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> Welcome Welcome ! (see html-example03)

30 30 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Using an extracted value as attribute 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.

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

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

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

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

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

36 36 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Welcome Welcome ! Your phone numbers are: Type Number (see html-example05) (leaving out xsl surroundings…)

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

38 38 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

39 39 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Welcome Welcome ! Your phone numbers are: Name Type Number (see html-example07) Adding the name

40 40 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Getting the Name when accessing the Phone Member Phone 555-1234 Phone 555-4321 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.

41 41 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Conditional Processing 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". Use the element to perform conditional processing. Do Lab1, Part 4

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

43 43 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. Let us enlarge our document to contain a couple of more members.

44 44 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Jeff 555-1234 555-4321 lightgrey David 383-1234 383-4321 lightblue Roger 888-1234 888-4321 lightyellow

45 45 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Access repeated XML elements "Select the Name of the first Member" "Select the Name of the first Member" "Select the Name of the last Member"

46 46 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. <!- - Process all Name elements which have FitnessCenter as an ancestor - -> Access repeated XML elements

47 47 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).

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

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

50 50 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

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

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

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

54 54 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)

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

56 56 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 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 a literal string. We could use this variable as follows: Value = This will output: Value = Hello World

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

58 58 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 = 555-1234 Member Phone 555-1234 Phone 555-4321 Name Jeff

59 59 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. concat() function concat(destination_string, string_to_add) Output: Welcome Jeff! Welcome David! Welcome Roger!

60 60 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Scope of xsl:variable A variable is write once, read many time. 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.

61 61 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

62 62 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Global Variables <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> Value of Pi The value of pi =

63 63 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? Heres what you might attempt to do: Member's Names: Output: Jeff (see html-example13)

64 64 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Lets add some statements to trace this example (see html-example14)

65 65 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Output of the previous example Jeff Jeff/ Jeff/David Jeff/ The previous name went out of scope. Jeff/Roger Jeff

66 66 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Heres what we would like to do names Open up the names box Add this iterations 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

67 67 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Output to a variable 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.

68 68 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Example for output to a variable Member's Names: / (see html-example15) Output: Member's Names: Jeff/David/Roger

69 69 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. xsl:choose xsl:choose allows you to elegantly express multiple conditional tests. Heres 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.

70 70 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

71 71 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. substring-after() works too substring-before(string, pattern) returns the part of the string string before the occurrence of the pattern 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()

72 72 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. starts-with() string function Heres the form of this string function: starts-with(string, pattern) It tests if the string string starts with the pattern pattern. It returns a Boolean (i.e. true or false). Example: action If the Phone starts with the string, 555 then do action.

73 73 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). Example: substring(1234567890, 2, 5) returns 23456

74 74 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. string-length() function string-length(string) returns the length of the string string Example: string-length(1234567890) returns 10

75 75 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: <xsl:variable name="lowerCaseChars" select=" 'abcdefghijklmnopqrstuvwxyz' "/> 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

76 76 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. = = != < > = <= >= != Want this: Use this:

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

78 78 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

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

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

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

82 82 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

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

84 84 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Namecalling with the name() Function Attribute = Element = (see html-example19) name(node) returns the name of node

85 85 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Use Curly Braces if the attribute is 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:

86 86 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

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

88 88 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

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

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

91 91 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

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

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

94 94 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Using XSLT and XPath to Transform XML Documents Part II

95 95 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Transformation Language XSL may be used as a transformation language --> it may be used to transform an XML document into another XML document (perhaps the new one is the same, minus company sensitive data) Transformation Engine (XSL Processor) XSL XML

96 96 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Example: Filter Gold Members Jeff 555-1234 555-4321 lightgrey David 383-1234 383-4321 lightblue Roger 888-1234 888-4321 lightyellow Jeff 555-1234 555-4321 lightgrey Roger 888-1234 888-4321 lightyellow

97 97 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. XML Transformations - all about (Template) Rules Hey xsl processor, when you encounter the root element (e.g., FitnessCenter) do [action1] Hey xsl processor, when you encounter the Member element do [action2] And so forth

98 98 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. XML Transformations - all about (Template) Rules Each template rule has two parts: –A pattern or matching part, that identifies the XML node in the source document to which the action part is to be applied. Matching information is contained in an attribute. –An action part that details the transformation of the node

99 99 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. XSL Document Structure [action] [action] [action]...

100 100 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Template Rules Template rules take the following general form: [action]

101 101 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Template Rules (Example) Hey XSL processor, as you parse through the XML document and you get to a element use this template rule. Go to each of my children (the Member children) and apply the template rules to them.

102 102 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Terminology In FitnessCenter.xml we have (snippet): Jeff 555-1234 555-4321 lightgrey... Member is a child element of the FitnessCenter element. Name, Phone, Phone, and FavoriteColor are children elements of the Member element. Member is a parent of Name. FitnessCenter and Member are ancestors of Name.

103 103 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. xsl:element Suppose that you are writing a stylesheet to generate an XML document. Obviously, you will need your stylesheet to output elements. –xsl:element is used to create elements [contents of the new element] [contents of the new element] creates

104 104 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Identity Transformation For our first example, lets create a stylesheet which simply creates an XML document that is a copy of the input XML document

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

106 106 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> Cont. -->

107 107 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. (see xml-example01)

108 108 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Jeff 555-1234 555-4321 lightgrey David 383-1234 383-4321 lightblue Roger 888-1234 888-4321 lightyellow Note that we've lost the attribute on the Member element

109 109 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. For each attribute Add an attribute to the element being output. The name of the attribute is the name of the current attribute being processed. The value of the attribute is the value of the current attribute being processed. Getting Members Attribute: (see xml-example02)

110 110 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Jeff 555-1234 555-4321 lightgrey David 383-1234 383-4321 lightblue Roger 888-1234 888-4321 lightyellow

111 111 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Generalize Our identity stylesheet will only work for FitnessCenter XML documents. We can make a stylesheet which does an identity transformation on any XML document.

112 112 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> (see xml-example03)

113 113 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Default Template Rules Every xsl document has two default template rules These rules are applied when the XSL Processor cannot find a template rule to use in your stylesheet Here are the two default template rules: Match on the document or any element. The action is to go to the children and execute their template rules. Match on a text node. The action is to output the value of the text node.

114 114 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Multiple Applicable Rules Suppose that the XSL Processor is processing FitnessCenter and it gets to the element. Why does it use:... and not the default template rule:... ??? After all, both apply. Answer: given two rules that apply, the more specific rule wins. --> Clearly, * is much more general than Member. * matches on any element. Member just matches on the Member element.

115 115 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. (see xml-example04) Now that we know about the default template rules, we can further reduce the size of the stylesheet.

116 116 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. The xsl:apply-templates element (without the select attribute) tells the XSL Processor to apply the template rules to all children (in document order) The xsl: apply-templates element can have a select attribute that tells the XSL Processor to process only the child element that matches pattern. –Thus, the select attribute rule enables us to specify the order in which the children are processed

117 117 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. "Go to the template rule for my Name child element. Then go to the template rule for the work Phone child element." "Go to all the child element nodes (not to any child text nodes)." Do Lab4, Part 1-4

118 118 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. mode Attribute Allows you to create multiple template rules for the same element. Each template rule can process the element differently. So, you can have multiple template rules for the same element. Just give each template rule a different mode

119 119 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Problem Identity transform the FitnessCenter.xml document. However, after you have copied all the Members, follow up with a (new) GoldMembers section, containing the name of each gold member (within stars) The next slide shows what the output XML file should look like

120 120 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Jeff 555-1234 555-4321 lightgrey David 383-1234 383-4321 lightblue Roger 888-1234 888-4321 lightyellow ***David*** (see xml-example05) Note that the names here are processed differently than the name in the GoldMembers section

121 121 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

122 122 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. *** *** Do Lab5, Part 1

123 123 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Stylesheet Reuse via xsl:include and xsl:import The elements xsl:include and xsl:import enable you to reuse other stylesheets. These elements are top-level elements. This means that they must be immediate children of the xsl:stylesheet element (i.e., they cannot be within a template rule) The xsl:include element is basically a macro substitution - the element is replaced by the contents of stylesheet it references

124 124 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">...... Replace the xsl:include element with the contents of the referenced stylesheet (i.e., all the children of xsl:stylesheet) toUpperCase.xsl

125 125 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. xsl:import xsl:import acts just like xsl:include - the stylesheet that it references is macro-substituted. However, there is a difference: –With xsl:include the stuff that is macro-substituted into the stylesheet has the same precedence as the rest of the stylesheet. It is as though you had one stylesheet. –With xsl:import the stuff that is macro-substituted into the stylesheet has lower precedence than the rest of the stylesheet. Also, all xsl:import elements must come first in the stylesheet.

126 126 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Using Long Names for Xpath Expressions Part III

127 127 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Problem: iterate through ancestors Suppose that in your stylesheet you are at an arbitrary node in the XML tree. How would you output the name of all of its ancestor nodes? Will this work: How many of these should we do? We don't know since we are at an arbitrary node!

128 128 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Problem: get the previous sibling and following sibling Suppose that in your stylesheet you are at an arbitrary node in the XML tree. How would you output the name of its previous sibling and following sibling?

129 129 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Long Notation Thus far we have used the abbreviated notation for expressing a path to a node. As we see there are some things for which the abbreviated notation is inadequate The long notation gives you the ability to express things that can't be expressed using the short notation

130 130 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 555-1234 Text 555-4321 Text lightgrey ancestors preceding sibling following siblings If we are currently at the Member's first phone element then: we can select all ancestors by: all preceding siblings by: all following siblings by: ancestor::* preceding-sibling::* following-sibling::* Which yields: Which yields: Which yields: Member Name Phone FitnessCenter FavoriteColor

131 131 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 555-1234 Text 555-4321 Text lightgrey (the other Member elements) descendants If we are currently at the FitnessCenter element then: we can select all descendants by: descendant::* Which yields: Member Name Phone, etc (see axis-example01)

132 132 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Axis ancestor, preceding-sibling, following- sibling, and descendent each provide a different way of navigating the XML tree. They are each called an axis On the next slide, we have a list of axis

133 133 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. ancestor: selects all ancestors ancestor-or-self: selects the current node plus all its ancestors attribute: selects all the attributes child: selects all the children descendant: selects all the descendants descendant-or-self: selects the current node plus all its descendants following: selects everything in the document that follows the current node following-sibling: selects the siblings that follow namespace: selects all the namespaces that are in scope parent: selects the parent preceding: selects everything in the document that precedes the current node preceding-sibling: selects the siblings that precede self: selects the current node (see axis-example02)

134 134 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Member is the starting node Its ancestors are: Its preceding siblings are: Its following siblings are: Example

135 135 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Using XSLT and XPath to Transform XML Documents that contain Namespaces Part IV

136 136 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Problem Suppose that the document that we are processing is using namespaces: Jeff 555-1234 555-4321 lightgrey Note that we have a default namespace declaration. Thus, FitnessCenter, Member, Name, Phone, and FavoriteColor all belong to the http://www.gym.com namespace.

137 137 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> Your name is: (see namespaces-example01) Output: -- empty --

138 138 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Why is the output empty? Your name is: This template does not match any element in the instance document! This template matches on a Member element in no namespace. However, in our instance document the Member element is in the http://www.gym.org namespace, i.e., {http://www.gym.com}Member

139 139 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Namespace Terminology {http://www.gym.com}Member Expanded name = The combination of the namespace URI and the local name Local name Namespace URI

140 140 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Namespace Terminology (cont.) … prefix

141 141 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. local-name() This is a built-in function which returns a string, corresponding to the local name of the element. Local name = Output: Local name = FitnessCenter Local name = Member Local name = Name Local name = Phone Local name = FavoriteColor (see namespaces-example02)

142 142 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. http://www.w3.org/1999/XSL/Transform Your name is: Output: Your name is: Jeff (see namespaces-example03)

143 143 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Alternatively <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:gym="http://www.gym.com" version="1.0"> Your name is: Declare the gym namespace Match on the Member element in the gym namespace Select the Name element in the gym namespace (see namespaces-example04)

144 144 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. namespace-uri() This is a built-in function which returns a string corresponding to the namespace URI of the node. Local name = Namespace URI = Output: Local name = FitnessCenter Namespace URI = http://www.gym.com Local name = Member Namespace URI = http://www.gym.com Local name = Name Namespace URI = http://www.gym.com Local name = Phone Namespace URI = http://www.gym.com... (see namespaces-example05)

145 145 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. name() Revisited We have seen the name() function before. It returns the name of the node. But what name does it return if the node is in a namespace? –Answer: it returns the element name and its prefix (this is called the QName, for Qualified Name)

146 146 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Jeff 555-1234 555-4321 lightgrey Local name = Namespace URI = Name =

147 147 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Output: Local name = FitnessCenter Namespace URI = http://www.gym.com Name = gym:FitnessCenter Local name = Member Namespace URI = http://www.gym.com Name = gym:Member Local name = Name Namespace URI = http://www.gym.com Name = gym:Name Local name = Phone Namespace URI = http://www.gym.com Name = gym:Phone Local name = Phone Namespace URI = http://www.gym.com Name = gym:Phone Local name = FavoriteColor Namespace URI = http://www.gym.com Name = gym:FavoriteColor (see namespaces-example06)

148 148 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Identity transform - copying namespace declarations Recall our identity transform stylesheet: Iterate through each attribute and add them as attributes onto the element.

149 149 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. @* does not select namespace declarations! The @* will only select non-namespace declaration attributes. It will not select namespace declaration attributes <Library xmlns="http://www.library.org" xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance" id="Boston Public Library"> This will be selected by @* These will not be selected by @*

150 150 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Identity transformation for XML documents containing namespaces? So how do we create a stylesheet that can copy over namespace declarations, along with the other attributes? –Answer: use the element This element will copy the current element and all namespace declarations to the output file.

151 151 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> (see namespaces-example07) The problem with this identity transform stylesheet is that it's not set up to allow us to make changes to elements/attributes.

152 152 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> Error! Attempting to create an element in a namespace, but the namespace has not been declared yet!

153 153 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> Simultaneously declare the element and its namespace Simultaneously declare the attribute and its namespace (see namespaces-example08)

154 154 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. The End Thanks for your attention!


Download ppt "1 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Using XSLT and XPath Roger L. Costello (XML Technologies) With changes and additions by."

Similar presentations


Ads by Google