Introduction to XSLT Transparency No. 1 Introduction to XSLT Cheng-Chia Chen.

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 XSLT. What is XML? Design Goals of XML XML Format XML Declaration ElementsAttributes.
+ XSL eXtensible Stylesheet Language. + 2 XML Lecture Adapted from the work of Prof Mark Baker ACET, University of Reading.
Introduction to C Programming
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.
1 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) [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.
XML Language Family Detailed Examples Most information contained in these slide comes from: These slides are intended.
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.
September 15, 2003Houssam Haitof1 XSL Transformation Houssam Haitof.
17 Apr 2002 XML Stylesheets Andy Clark. What Is It? Extensible Stylesheet Language (XSL) Language for document transformation – Transformation (XSLT)
Introduction to XPath Bun Yue Professor, CS/CIS UHCL.
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.
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.
CSE3201/CSE4500 Information Retrieval Systems
XP New Perspectives on XML Tutorial 6 1 TUTORIAL 6 XSLT Tutorial – Carey ISBN
XP 1 CREATING AN XML DOCUMENT. XP 2 INTRODUCING XML XML stands for Extensible Markup Language. A markup language specifies the structure and content of.
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,
XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL.
ECA 228 Internet/Intranet Design I XSLT Example. ECA 228 Internet/Intranet Design I 2 CSS Limitations cannot modify content cannot insert additional text.
JSTL, XML and XSLT An introduction to JSP Standard Tag Library and XML/XSLT transformation for Web layout.
CITA 330 Section 6 XSLT. Transforming XML Documents to XHTML Documents XSLT is an XML dialect which is declared under namespace "
XSLT part of XSL (Extensible Stylesheet Language) –includes also XPath and XSL Formatting Objects used to transform an XML document into: –another XML.
XSLT Kanda Runapongsa Dept. of Computer Engineering Khon Kaen University.
Transforming Documents „a how-to of transforming xml documents“ Lecture on Walter Kriha.
XPath Aug ’10 – Dec ‘10. XPath   XML Path Language   Technology that allows to select a part or parts of an XML document to process   XPath was.
 2002 Prentice Hall, Inc. All rights reserved. 1 Chapter 12 – XSL: Extensible Stylesheet Language Transformations (XSLT) Outline 12.1Introduction 12.2Setup.
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.
XP New Perspectives on XML, 2 nd Edition Tutorial 8 1 TUTORIAL 8 CREATING ELEMENT GROUPS.
1 Copyright (c) [2000]. Roger L. Costello. All Rights Reserved. Using XSLT and XPath to Transform XML Documents Roger L. Costello XML Technologies.
Dr. Chunbo Chu Week 3. XML Not a replacement for HTML. XML and HTML were designed with different goals: XML was designed to transport and store data,
 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.
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.
XSLT: How Do We Use It? Nancy Hallberg Nikki Massaro Kauffman.
Grouping Robin Burke ECT 360. Outline Extra credit Numbering, revisited Grouping: Sibling difference method Uniquifying in XPath Grouping: Muenchian method.
XSLT I Robin Burke ECT 360. Outline History / Terminology XSLT processing XSLT syntax XPath XSLT basics Lab.
1 Whitespace Handling Roger L. Costello XML Technologies.
ACG 4401 XSLT Extensible Stylesheet Language for Transformations Presenting XML and XBRL.
CITA 330 Section 5 XPath. XSL XSL (Extensible Stylesheet Language) is the standard language for writing stylesheets to transform XML documents among different.
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.
1999 costello1 XSL by Example (part 2) Roger L. Costello
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.
Rendering XML Documents ©NIITeXtensible Markup Language/Lesson 5/Slide 1 of 46 Objectives In this session, you will learn to: * Define rendering * Identify.
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:
Using XSLT and XPath to Transform XML Documents
Introduction to Scripting
ACG 4401 XSLT Extensible Stylesheet Language for Transformations
XML WITH CSS.
Presentation transcript:

Introduction to XSLT Transparency No. 1 Introduction to XSLT Cheng-Chia Chen

Introduction to XSLT Transparency No. 2 What is XSLT ? XSLT is a language for transforming XML documents into other [XML] documents. XML syntax Possible output formats: XML HTML TEXT XSL Processor XSL XML HTML (or XML or text)

Introduction to XSLT Transparency No. 3 Outlines Transform XML Documents into HTML Documents Transform XML Documents into other XML Documents Transform XML Documents into Texts. Note: Most slide materials adapted from Roger L. CostelloRoger L. Costello

Introduction to XSLT Transparency No. 1 Transform XML Documents into HTML Documents

Introduction to XSLT Transparency No. 5 HTML Generation Use XSL to generate HTML documents. XSLT viewed as a tool to fill in the content of an HTML document with data extracted from an XML Document. XSLT provides elements (tags) for extracting the XML data

Introduction to XSLT Transparency No. 6 Enhancing HTML Documents with XML Data XML Document HTML Document (with embedded XSL elements) XSL element XML data

Introduction to XSLT Transparency No. 7 Enhancing HTML Documents with the Following XML Data Jeff lightgrey FitnessCenter.xml

Introduction to XSLT Transparency No. 8 HTML Document in an XSL Template <xsl:stylesheet xmlns:xsl=" version="1.0"> Welcome Welcome!

Introduction to XSLT Transparency No. 9 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

Introduction to XSLT Transparency No. 10 Extracting the Member Name <xsl:stylesheet xmlns:xsl=" version="1.0"> Welcome Welcome !

Introduction to XSLT Transparency No. 11 Extracting a Value from an XML Document, Navigating the XML Document Extracting values: use the XSL element Navigating: … is an XPath expression with matched node as context node. Recall what the location path : /FitnessCenter/Member/Name means: "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."

Introduction to XSLT Transparency No. 12 The tree view of fitnessCenter.xml Document / PI Element FitnessCenter Element Member Element Member Element Member Element Name Element Phone Element Phone Element FavoriteColor... Text Jeff Text Text Text lightgrey...

Introduction to XSLT Transparency No. 13 Extract the FavoriteColor and use it as the bgcolor <xsl:stylesheet xmlns:xsl=" version="1.0"> Welcome Welcome !

Introduction to XSLT Transparency No. 14 Notes Attribute values cannot contain " “ The following is NOT valid: <Body bgcolor="<xsl:value-of select='/FitnessCenter/Member/FavoriteColor'/>"> To extract the value of an XML element and use it as an attribute value you must use curly braces: 1.Evaluate the expression within the curly braces. 2.Assign the value to the attribute.

Introduction to XSLT Transparency No. 15 Extract the Home Phone Number <xsl:stylesheet xmlns:xsl=" version="1.0"> Welcome Welcome ! Your home phone number is:

Introduction to XSLT Transparency No. 16 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=" … ' …' …"

Introduction to XSLT Transparency No. 17 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.

Introduction to XSLT Transparency No. 18 Fruit Color Apple Red Banana Yellow Fruit Color Apple Red Banana Yellow

Introduction to XSLT Transparency No. 19 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.

Introduction to XSLT Transparency No. 20 Welcome Welcome ! Your phone numbers are: Type Number

Introduction to XSLT Transparency No. 21 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. - ->

Introduction to XSLT Transparency No. 22 Special Offer to Platinum Members Enhance the example to provide a special offer to "platinum" members. We need to check if the "level" attribute on the Member element equals "platinum".

Introduction to XSLT Transparency No. 23 Welcome Welcome ! Our special offer to platinum members today is... Your phone numbers are: Type Number

Introduction to XSLT Transparency No. 24 Conditional Processing Use the IfTruePart element to perform conditional processing. Note: It is needless to add in the value of test attribute extra { and }, which must appear in

Introduction to XSLT Transparency No. 25 Accessing Multiple Parts of the XML Document 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.

Introduction to XSLT Transparency No. 26 Welcome Welcome ! Our special offer to platinum members today is... Your phone numbers are: Name Type Number

Introduction to XSLT Transparency No. 27 Getting the Name when accessing the Phone Member Phone Phone Name Jeff Notice how we access the Name with respect to the Phone element Via the location path../Name We can access elements in other parts of the XML tree via the “../” operator... Name

Introduction to XSLT Transparency No. 28 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

Introduction to XSLT Transparency No. 29 Other ways to Access the XML Data (cont.) <!- - Process all Name elements which have FitnessCenter as an ancestor - ->

Introduction to XSLT Transparency No. 30 Enhanced XML Document Jeff lightgrey David lightblue Roger lightyellow Note that each Member now has a unique id (the id attribute)

Introduction to XSLT Transparency No. 31 Review - HTML Hyperlinking … Click Here... This creates an internal hyperlink (the source anchor links to the target anchor).

Introduction to XSLT Transparency No. 32 Hyperlink Name to Home Phone Problem: 1. create an HTML document that has two tables: - a Member Name table, and - a Member home Phone number table. 2. Hyperlink the Member's Name to his/her Phone.

Introduction to XSLT Transparency No. 33 Name Home Phone Number

Introduction to XSLT Transparency No. 34 Numbering There is an XSL element that returns a number corresponding to the element's position in the set of selected nodes.  Output: 1. Jeff 2. David 3. Roger

Introduction to XSLT Transparency No. 35 Start Numbering from 0 How would you start the numbering from zero, rather than one?

Introduction to XSLT Transparency No. 36 format attribute of xsl:number 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 A.1, A.2, A.3, 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,...

Introduction to XSLT Transparency No. 37 format attribute of xsl:number. Output: A. Jeff B. David C. Roger

Introduction to XSLT Transparency No. 38 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

Introduction to XSLT Transparency No. 39 Sorting Note: 1. Sort occurs before iteration. 2. I.e. 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

Introduction to XSLT Transparency No. 40 concat() function concat(destination string, strings to add) Note: if you want to concatenate more than one string to the destination string then simply add more arguments

Introduction to XSLT Transparency No. 41 Output: Welcome Jeff! Welcome David! Welcome Roger!

Introduction to XSLT Transparency No. 42 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

Introduction to XSLT Transparency No. 43 Member's Phone Numbers: Name Type Number

Introduction to XSLT Transparency No. 44 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

Introduction to XSLT Transparency No. 45 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 declaration is visible to the following siblings and its descendants. This region is the scope of the variable binding.

Introduction to XSLT Transparency No. 46 The name variable's scope Do Lab2, Part 1 Member's Phone Numbers: Name Type Number …

Introduction to XSLT Transparency No. 47 Global Variables Are variables declared outside of template elements. <xsl:stylesheet mlns:xsl=" version="1.0"> Value of Pi The value of pi = …

Introduction to XSLT Transparency No. 48 Problem Create a variable, names, to contain a list of the Member Names, with each name separated by a slash. How would you create such a variable? First attempt: Member's Names: Output: A parameter or variable with the same name already exists in the current scope.

Introduction to XSLT Transparency No. 49 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”.

Introduction to XSLT Transparency No. 50 Problem - Solution Member's Names: / Output: Member's Names: Jeff/David/Roger

Introduction to XSLT Transparency No. 51 contains() function contains(string to be tested, test string) returns true if string to be tested contains test string $greeting contains ‘welcome’

Introduction to XSLT Transparency No. 52 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.

Introduction to XSLT Transparency No. 53 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

Introduction to XSLT Transparency No. 54 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

Introduction to XSLT Transparency No. 55 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]”.

Introduction to XSLT Transparency No. 56 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’

Introduction to XSLT Transparency No. 57 string-length() function string-length(string) returns the length of the string string-length(‘ ’) returns 10

Introduction to XSLT Transparency No. 58 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.

Introduction to XSLT Transparency No. 59 Boolean and Relational Operators Boolean operators: not, and, or Relational operators:, =, =, != The less 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:

Introduction to XSLT Transparency No. 60 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.

Introduction to XSLT Transparency No. 61 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

Introduction to XSLT Transparency No. 62 Enhanced XML Document Jeff lightgrey 340 David lightblue 500 Roger lightyellow 340 Note that each Member now has MembershipFee element

Introduction to XSLT Transparency No. 63 Compute Membership Revenue Membership Fee Revenue:

Introduction to XSLT Transparency No. 64 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.

Introduction to XSLT Transparency No. 65 Coloring alternate rows Member Names: yellow For each even row of the table, the TR value will be:

Introduction to XSLT Transparency No. 66 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

Introduction to XSLT Transparency No. 67 Selecting all Elements/Attributes For each attribute do... For each child element do...

Introduction to XSLT Transparency No. 68 Getting the Name of the Element/Attribute using the name() Function Attribute = Element = name(node) returns the name of "node"

Introduction to XSLT Transparency No. 69 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: <xsl:sort order lang data-type case-order

Introduction to XSLT Transparency No. 70 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

Introduction to XSLT Transparency No. 71 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.

Introduction to XSLT Transparency No. 72 Name Phone(home)... <xsl:variable name="FitnessCenter2" select="document('file://localhost/xml-course/.../FitnessCenter2.xml')"/>

Introduction to XSLT Transparency No. 73 Parameterized Processing You can create a subroutine (called a named template), and you can pass to it parameters.

Introduction to XSLT Transparency No. 74 Fitness Center <xsl:with-param name="name" select="/FitnessCenter/Member[1]/Name"/>...

Introduction to XSLT Transparency No. 75 Call by Reference How do we create a named template that returns a value? Example: create a named template which, when passed a number, returns the number div 2.

Introduction to XSLT Transparency No. 76 Fitness Center 16 / 2 =

Introduction to XSLT Transparency No. 77 generate-id() Use this function to generate a unique string for a node in the source document tree. Example. generate-id(/FitnessCenter/Member[1]) will return a unique id for the first Member Note: for the same node e, generate-id(e) will always produce the same id.

Introduction to XSLT Transparency No. 78 Using generate-id() to Uniquely Identify Elements In previous example we created two tables –previous example 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.

Introduction to XSLT Transparency No. 79 Name Home Phone Number

Introduction to XSLT Transparency No. 1 Use XSLT to transform XML Documents

Introduction to XSLT Transparency No. 81 Note All the xsl functionality that we have learned at previous slides are applicable in transforming XML documents

Introduction to XSLT Transparency No. 82 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 (XSLT Processor) XSL XML

Introduction to XSLT Transparency No. 83 Example: Filter Gold Members Jeff lightgrey David lightblue Roger lightyellow Jeff lightgrey Roger lightyellow

Introduction to XSLT Transparency No. 84 A pseudo code for XSLT Process model: process(Node: node): tree-fragment rules = match(node); // find rules matching this node rule = rules.best(); // find the best among all rules result = rule.appliedTo(node) // this may recursively call process() return result. main() { return process( root-node ) }.

Introduction to XSLT Transparency No. 85 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

Introduction to XSLT Transparency No. 86 XSLT Document Structure [action] [action] [action]...

Introduction to XSLT Transparency No. 87 Template Rules Template rules take the following general form: [ action ]

Introduction to XSLT Transparency No. 88 Template Rules (Example) “Use this rule when the processor parse through the XML document and get to a element.” “Go to each of my children (the Member children) and apply the template rules to them.”

Introduction to XSLT Transparency No. 89 Terminology In FitnessCenter.xml we have (snippet): Jeff 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.”

Introduction to XSLT Transparency No. 90 xsl:element Used to compute/create output elements. xsl:element is used to create elements [contents of the new element] [contents of the new element] creates

Introduction to XSLT Transparency No. 91 Identity Transformation Create a stylesheet which simply creates an XML document that is a copy of the input XML document

Introduction to XSLT Transparency No. 92 Document / PI Element FitnessCenter Element Member Element Member Element Member Element Name Element Phone Element Phone Element FavoriteColor... Text Jeff Text Text Text lightgrey...

Introduction to XSLT Transparency No. 93 <xsl:stylesheet xmlns:xsl=" version="1.0"> default selection

Introduction to XSLT Transparency No. 94

Introduction to XSLT Transparency No. 95 Jeff lightgrey David lightblue Roger lightyellow Note that we've lost the attribute on the Member element The output

Introduction to XSLT Transparency No. 96 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 Member’s Attribute:

Introduction to XSLT Transparency No. 97 Jeff lightgrey David lightblue Roger lightyellow

Introduction to XSLT Transparency No. 98 Generalize previous identity stylesheet only work for FitnessCenter XML documents. Make a stylesheet which does an identity transformation on any XML document.

Introduction to XSLT Transparency No. 99 <xsl:stylesheet xmlns:xsl=" version="1.0">

Introduction to XSLT Transparency No. 100 Default Template Rules Every xsl document has two default template rules 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.”

Introduction to XSLT Transparency No. 101 Multiple Applicable Rules Suppose we are finding rules to be applied to a node. But now there are two rules matched: // the default rule Which one will the processor choose to 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.

Introduction to XSLT Transparency No. 102 Smallest Identity Transformation Stylesheet Now that we know about the default template rules, we can further reduce the size of the stylesheet.

Introduction to XSLT Transparency No. 103 <xsl:stylesheet xmlns:xsl=" version="1.0">

Introduction to XSLT Transparency No. 104 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

Introduction to XSLT Transparency No. 105 "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)."

Introduction to XSLT Transparency No. 106 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

Introduction to XSLT Transparency No. 107 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

Introduction to XSLT Transparency No. 108 Jeff lightgrey David lightblue Roger lightyellow ***David*** Note that the names here are processed differently than the name in the GoldMembers section

Introduction to XSLT Transparency No. 109

Introduction to XSLT Transparency No. 110 *** ***

Introduction to XSLT Transparency No. 111 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

Introduction to XSLT Transparency No. 112 <xsl:stylesheet xmlns:xsl=" version="1.0"> <xsl:stylesheet xmlns:xsl=" 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

Introduction to XSLT Transparency No. 113 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.

Introduction to XSLT Transparency No. 1 Transform XML Documents into Text Files

Introduction to XSLT Transparency No. 115 Problem Create a stylesheet that creates a text file containing each member's data. One line per member Member data separated by a slash delimiter Jeff/ / /lightgrey David/ / /lightblue Roger/ / /lightyellow

Introduction to XSLT Transparency No. 116 <xsl:stylesheet xmlns:xsl=" version="1.0">

Introduction to XSLT Transparency No. 1 More Examples

Introduction to XSLT Transparency No. 118 Examples transform some data represented in XML using three different XSLT stylesheets to produce three different representations of the data, HTML, SVG and VRML. The input data:

Introduction to XSLT Transparency No. 119 transforms the data into HTML using simplified syntax: <html xsl:version="1.0" xmlns:xsl=" lang="en"> Sales Results By Division Division Revenue Growth Bonus

Introduction to XSLT Transparency No. 120 <xsl:sort select="revenue" data-type="number" order="descending"/> color:red

Introduction to XSLT Transparency No. 121 The HTML output <meta http-equiv="Content-Type" content="text/html; charset=iso "> Sales Results By Division Division Revenue Growth Bonus North West South 4 3 4

Introduction to XSLT Transparency No. 122 Transform the data into SVG: <xsl:stylesheet version="1.0“ xmlns:xsl=" xmlns=" Revenue Division

Introduction to XSLT Transparency No. 123 <rect x="{$pos}" y="{150-$height}" width="20" height="{$height}"/>

Introduction to XSLT Transparency No. 124 The SVG output <svg width="3in" height="3in" xmlns=" Revenue Division North 10 South 4 West 6

Introduction to XSLT Transparency No. 125 Transforms the data into VRML <xsl:stylesheet version="1.0“ xmlns:xsl=" <xsl:output method="text" encoding="UTF-8" media-type="model/vrml"/> #VRML V2.0 utf8 # externproto definition of a single bar element EXTERNPROTO bar [ field SFInt32 x field SFInt32 y field SFInt32 z field SFString name ] "

Introduction to XSLT Transparency No. 126 # inline containing the graph axes Inline { url " l" } bar { x y z name " " }

Introduction to XSLT Transparency No. 127 The VRML Output #VRML V2.0 utf8 # externproto definition of a single bar element EXTERNPROTO bar [ field SFInt32 x field SFInt32 y field SFInt32 z field SFString name ] " # inline containing the graph axes Inline {url " } bar {x 10 y 9 z 7 name "North" } bar {x 4 y 3 z 4 name "South" } bar {x 6 y -1.5 z 2 name "West" }