2002 Prentice Hall, Inc. All rights reserved. 1 Chapter 12 – XSL: Extensible Stylesheet Language Transformations (XSLT) Outline 12.1Introduction 12.2Setup 12.3Templates 12.4Creating Elements and Attributes 12.5Iteration and Sorting 12.6Conditional Processing 12.7Copying Nodes 12.8Combining Stylesheets 12.9Variables 12.10Case Study: XSLT and XPath
2002 Prentice Hall, Inc. All rights reserved Introduction Extensible Stylesheet Language (XSL) –Used to format XML documents –Consist of two parts XSL Transformation Language (XSLT) –Transform XML document from one form to another –Use XPath to match nodes XSL formatting objects –Alternative to CSS
2002 Prentice Hall, Inc. All rights reserved Setup XSLT processor –Microsoft Internet Explorer 5 –Apache’s Xalan –Java 2 Standard Edition required Download at Installation instructions – –Xalan required Download at xml.apache.org/xalan
2002 Prentice Hall, Inc. All rights reserved Templates XSLT document –XML document with root element stylesheet –template element Matches specific XML document nodes Uses XPath expression in attribute match
2002 Prentice Hall, Inc. All rights reserved Templates (cont.) XSLT –Two trees of nodes Source tree corresponds to original XML document Result tree contains nodes produced by transformation –Fig transforms intro.xml into HTML document
2002 Prentice Hall, Inc. All rights reserved. Outline 6 Fig. 12.1Simple Template. Root element stylesheet XSLT namespace Use XPath expression in template element to match any myMessage element nodes template element contents are placing in resulting tree when myMessage element node is matched <xsl:stylesheet version = "1.0" 7 xmlns:xsl = " Root element stylesheet XSLT namespace template element contents are placing in resulting tree when myMessage element node is matched Use XPath expression in template element to match any myMessage element nodes
2002 Prentice Hall, Inc. All rights reserved. Outline 7 Fig. 12.2Input XML document. Element stylesheet attaches style sheet to XML document Welcome to XSLT! 10 Element stylesheet attaches style sheet to XML document
2002 Prentice Hall, Inc. All rights reserved. Outline 8 Fig. 12.3Internet Explorer 5 displaying the results of an XSL transformation. 1 Welcome to XSLT!
2002 Prentice Hall, Inc. All rights reserved Creating Elements and Attributes Creation of elements and attributes
2002 Prentice Hall, Inc. All rights reserved. Outline 10 Fig. 12.4XML document containing a list of sports More popular among commonwealth nations More popular in America Most popular sport in the world
2002 Prentice Hall, Inc. All rights reserved. Outline 11 Fig. 12.5Using XSLT to create elements and attributes. Use attribute match to select XML document root Element apply- templates applies XSLT document templates to specific nodes match element sports and apply templates to sport node’s child nodes Create element title Create attribute id for element title Create comment with element para comments <xsl:stylesheet version = "1.0" 7 xmlns:xsl = " Use attribute match to select XML document root Element apply-templates applies XSLT document templates to specific nodes match element sports and apply templates to sport node’s child nodes Create element title Create attribute id for element title Create comment with element para comments
2002 Prentice Hall, Inc. All rights reserved. 12 Fig Default XSLT Templates
2002 Prentice Hall, Inc. All rights reserved. Outline 13 Fig. 12.7Output of transformation More popular among commonwealth nations More popular in America Most popular sport in the world
2002 Prentice Hall, Inc. All rights reserved Iteration and Sorting XSLT allows –Iteration through node set Element for-each –Sorting node set Element sort –Attribute ascending (i.e., A-Z) –Attribute descending (i.e., Z-A)
2002 Prentice Hall, Inc. All rights reserved. Outline 15 Fig. 12.8Book table of contents as XML Deitel's XML Primer Paul 13 Deitel Welcome 18 Easy XML 19 XML Elements? 20 Entities
2002 Prentice Hall, Inc. All rights reserved. Outline 16 Fig. 12.9Transforming XML data into HTML. Element for-each applies element contents to each preface element node Use element sort to sort nodes (selected by for- each ) in ascending order <xsl:stylesheet version = "1.0" 7 xmlns:xsl = " ISBN by, Element for-each applies element contents to each preface element node Use element sort to sort nodes (selected by for- each ) in ascending order
2002 Prentice Hall, Inc. All rights reserved. Outline 17 Fig Transforming XML data into HTML (Part 2). Iterate and sort chapter elements Iterate and sort appendix elements Preface ( 37 pages ) Chapter ( 51 pages ) Appendix 61 Iterate and sort chapter elementsIterate and sort appendix elements
2002 Prentice Hall, Inc. All rights reserved. Outline 18 Fig Transforming XML data into HTML (Part 3) ( 65 pages )
2002 Prentice Hall, Inc. All rights reserved. Outline 19 Fig Output of the transformation ISBN X - Deitel's XML Primer Deitel's XML Primer 8 by Deitel, Paul Preface 1 13 Welcome ( 2 pages ) Chapter 1 18 Easy XML ( 4 pages ) Chapter 2 23 XML Elements? ( 2 pages ) Appendix 1 28 Entities ( 9 pages )
2002 Prentice Hall, Inc. All rights reserved. 20 Output from Fig
2002 Prentice Hall, Inc. All rights reserved Conditional Processing Perform conditional processing –Such as if statement –Use element choose Allows alternate conditional statements Similar to switch statement Has child elements when and otherwise –when element content used if condition is met –otherwise element content used if no conditions in when condition are met
2002 Prentice Hall, Inc. All rights reserved. Outline 22 Fig Using conditional elements <xsl:stylesheet version = "1.0" 7 xmlns:xsl = " Appointments Year: <xsl:sort select = order = "ascending" 27 data-type = "number"/>
2002 Prentice Hall, Inc. All rights reserved. Outline 23 Fig Using conditional elements (Part 2). Allow conditional statements Content of when element used if condition is met Content of otherwise element used if no conditions are met Day: 30 / <xsl:when test = 37 > '0500' < '1200'"> 38 Morning ( ): <xsl:when test = 42 > '1200' < '1700'"> 43 Afternoon ( ): <xsl:when test = 47 > '1200' < '1700'"> 48 Evening ( ): <xsl:when test = 52 > '1200' < '1700'"> 53 Night ( ): Entire day: 58 Allow conditional statements Content of when element used if condition is met Content of otherwise element used if no conditions are met
2002 Prentice Hall, Inc. All rights reserved. Outline 24 Fig Using conditional elements (Part 3) n/a
2002 Prentice Hall, Inc. All rights reserved. 25 Output from Fig
2002 Prentice Hall, Inc. All rights reserved Copying Nodes Duplicate nodes from sources tree to result tree –Element copy Produce copy of context node Place copy in result tree No child nodes or attributes are duplicated –Element copy-of Performs copy of subtree Duplicates children (i.e., text, processing instructions, etc.)
2002 Prentice Hall, Inc. All rights reserved. Outline 27 Fig Using the XSLT element copy. Apply templates to element’s child nodes Replace element content with text <xsl:stylesheet version = "1.0" 7 xmlns:xsl = " How about 'Hi World' for a change! Replace element content with text Apply templates to element’s child nodes
2002 Prentice Hall, Inc. All rights reserved. Outline 28 Fig Resulting transformation How about 'Hi World' for a change! 5 6
2002 Prentice Hall, Inc. All rights reserved. Outline 29 Fig xsl:copy- of element. Create comment in resulting XML document Duplicate nodes selected by attribute select into resulting XML document <xsl:stylesheet version = "1.0" 7 xmlns:xsl = " The following XML tree has been copied into output Create comment in resulting XML document Duplicate nodes selected by attribute select into resulting XML document
2002 Prentice Hall, Inc. All rights reserved. Outline 30 Fig Output of the copy-of transformation Welcome to XSLT! 5
2002 Prentice Hall, Inc. All rights reserved Combining Stylesheets XSLT document can import others –Element import local templates have higher precedence as included templates –Element include Included templates have same precedence as local templates
2002 Prentice Hall, Inc. All rights reserved. Outline 32 Fig XSLT document being imported <xsl:stylesheet version = "1.0" 7 xmlns:xsl = " Author: 27, 28 29
2002 Prentice Hall, Inc. All rights reserved. Outline 33 Fig XSLT document being imported (Part 2). Template to match any text and leftover element nodes Template to match any text and leftover element nodes
2002 Prentice Hall, Inc. All rights reserved. Outline 34 Fig Importing another XSLT document. Use templates in usage2.xsl Template for element title used instead of one defined in usage2.xsl <xsl:stylesheet version = "1.0" 7 xmlns:xsl = " <!-- This template has higher precedence over the 12 templates being imported --> Use templates in usage2.xsl Template for element title used instead of one defined in usage2.xsl
2002 Prentice Hall, Inc. All rights reserved. Outline 35 Fig Resulting HTML document using XSLT import Deitel's XML Primer Author: Deitel, Paul 7 8 9
2002 Prentice Hall, Inc. All rights reserved. Outline 36 Fig Combining stylesheets using xsl:include. Include author.xsl and chapters.xsl, but templates in stylesheets have same precedence as local templates <xsl:stylesheet version = "1.0" 7 xmlns:xsl = " Include author.xsl and chapters.xsl, but templates in stylesheets have same precedence as local templates
2002 Prentice Hall, Inc. All rights reserved. Outline 37 Fig XSLT document for rendering the author’s name <xsl:stylesheet version = "1.0" 7 xmlns:xsl = " Author: 12,
2002 Prentice Hall, Inc. All rights reserved. Outline 38 Fig XSLT document for rendering chapter names <xsl:stylesheet version = "1.0" 7 xmlns:xsl = " Chapters:
2002 Prentice Hall, Inc. All rights reserved. Outline 39 Fig Output of an XSLT document using element include. Output from Fig Deitel’s XML Primer 4 Author: 5 Deitel, Paul 6 7 Chapters: 8 9 Easy XML 10 XML Elements?
2002 Prentice Hall, Inc. All rights reserved Variables Variables for processing information –Element variable
2002 Prentice Hall, Inc. All rights reserved. Outline 41 Fig Example for xsl:variable. Create element variable with attribute name to store number of pages in book Element value-of uses dollar sign ( $ ) to reference and output variable sum <xsl:stylesheet version = "1.0" 7 xmlns:xsl = " Number of pages = 13 <xsl:variable name = "sum" 14 select = Create element variable with attribute name to store number of pages in book Element value-of uses dollar sign ( $ ) to reference and output variable sum
2002 Prentice Hall, Inc. All rights reserved. Outline 42 Fig Output of transformation. 1 2 Number of pages = 17
2002 Prentice Hall, Inc. All rights reserved Case Study: XSLT and XPath XPath Expression –locates elements, attributes and text in XML document –Consider more substantial example Mark up information about Deitel complete training course
2002 Prentice Hall, Inc. All rights reserved. Outline 44 Fig XML document containing information about complete training courses and Web- based training. Root element product contains complete- TrainingCourses Each complete- TrainingCourse element contains title, book and cd The Complete C++ Training Course: Third Edition C++ How to Program: Third Edition C++ Multimedia Cyber Classroom: Third Edition The Complete Java 2 Training Course: Third Edition Root element product contains completeTrainingCourses Each completeTrainingCourse element contains title, book and cd
2002 Prentice Hall, Inc. All rights reserved. Outline 45 Fig XML document containing information about complete training courses and Web- based training (Part 2). Root element product also contains books Java How to Program: Third Edition Java Multimedia Cyber Classroom: Third Edition The Complete XML Training Course 43 XML How to Program 44 XML Multimedia Cyber Classroom Getting Started with Microsoft Visual C with an Introduction to MFC C How to Program Root element product also contains books
2002 Prentice Hall, Inc. All rights reserved. Outline 46 Fig XML document containing information about complete training courses and Web- based training (Part 3). Root element product also contains webBased- Training Introduction to Java programming 61 Advanced C++ programming 62 Programming COM+ with Visual Basic Root element product also contains webBasedTraining
2002 Prentice Hall, Inc. All rights reserved. Outline 47 Fig XSL stylesheet for transforming axes.xml into HTML. Select all product children of root node Select node descendents of completeTraining- Courses elements <xsl:stylesheet version = "1.0" 7 xmlns:xsl = " Axes example node { font-family: monospace; 18 font-weight: bold; } Decendents of completeTrainingCourse are: <xsl:apply-templates select = 28 "completeTrainingCourses//node()"/> Select all product children of root node Select node descendents of completeTrainingCourses elements
2002 Prentice Hall, Inc. All rights reserved. Outline 48 Fig XSL stylesheet for transforming axes.xml into HTML (Part 2). Apply templates to add string-values for webBasedTraining nodes to HTML document 31 Self for 32 webBasedTraining is <xsl:apply-templates 35 select = "webBasedTraining"/> Parent of 39 C How to Program is <xsl:value-of select = 44 "name(//parent::node()[. = 'C How to Program'])"/> Child of 50 title 51 element for the second completeTrainingCourse is <xsl:value-of select = 57 "//child::completeTrainingCourse[ 2 ]/title"/> Apply templates to add string- values for webBasedTraining nodes to HTML document
2002 Prentice Hall, Inc. All rights reserved. Outline 49 Fig XSL stylesheet for transforming axes.xml into HTML (Part 3) Is product the ancestor of 65 XML How to Program ? <xsl:if test = "name(//node()[. = 68 'XML How to Program']/ancestor::product) = 'product'"> 69 Yes First subject preceding Programming COM+ with Visual Basic 78 is <xsl:value-of select = 82 "//subject[. = 'Programming COM+ with Visual Basic' 83 ]/preceding-sibling::subject[ 1 ]"/> First subject following 89
2002 Prentice Hall, Inc. All rights reserved. Outline 50 Fig XSL stylesheet for transforming axes.xml into HTML (Part 4). Retrieve string-values for title, book and cd elements and add them to HTML document Add string-values for subject element to HTML document Introduction to Java programming 92 is <xsl:value-of select = 96 "//subject[. = 'Introduction to Java programming' 97 ]/following-sibling::subject[ 1 ]"/> Retrieve string-values for title, book and cd elements and add them to HTML document Add string-values for subject element to HTML document
2002 Prentice Hall, Inc. All rights reserved. 51 Fig HTML page generated by transforming axes.xml with axes.xsl