Download presentation
Presentation is loading. Please wait.
Published byKelly Cole Modified over 8 years ago
1
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
2
2002 Prentice Hall, Inc. All rights reserved. 2 12.1 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
3
2002 Prentice Hall, Inc. All rights reserved. 3 12.2 Setup XSLT processor –Microsoft Internet Explorer 5 –Apache’s Xalan –Java 2 Standard Edition required Download at www.java.sun.com/j2se Installation instructions –www.deitel.com/faq/java3install.htm –Xalan required Download at xml.apache.org/xalan
4
2002 Prentice Hall, Inc. All rights reserved. 4 12.3 Templates XSLT document –XML document with root element stylesheet –template element Matches specific XML document nodes Uses XPath expression in attribute match
5
2002 Prentice Hall, Inc. All rights reserved. 5 12.3 Templates (cont.) XSLT –Two trees of nodes Source tree corresponds to original XML document Result tree contains nodes produced by transformation –Fig. 12.1 transforms intro.xml into HTML document
6
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 1 2 3 4 5 6<xsl:stylesheet version = "1.0" 7 xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"> 8 9 10 11 12 13 14 15 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
7
2002 Prentice Hall, Inc. All rights reserved. Outline 7 Fig. 12.2Input XML document. Element stylesheet attaches style sheet to XML document 1 2 3 4 5 6 7 8 9 Welcome to XSLT! 10 Element stylesheet attaches style sheet to XML document
8
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!
9
2002 Prentice Hall, Inc. All rights reserved. 9 12.4 Creating Elements and Attributes Creation of elements and attributes
10
2002 Prentice Hall, Inc. All rights reserved. Outline 10 Fig. 12.4XML document containing a list of sports. 1 2 3 4 5 6 7 8 9 243 10 11 12 More popular among commonwealth nations. 13 14 15 16 17 431 18 19 20 More popular in America. 21 22 23 24 25 123 26 27 28 Most popular sport in the world. 29 30 31 32
11
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 1 2 3 4 5 6<xsl:stylesheet version = "1.0" 7 xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"> 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 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
12
2002 Prentice Hall, Inc. All rights reserved. 12 Fig. 12.6 Default XSLT Templates
13
2002 Prentice Hall, Inc. All rights reserved. Outline 13 Fig. 12.7Output of transformation. 1 2 3 4 5 6 More popular among commonwealth nations. 7 8 9 10 11 12 More popular in America. 13 14 15 16 17 18 Most popular sport in the world. 19 20 21 22
14
2002 Prentice Hall, Inc. All rights reserved. 14 12.5 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)
15
2002 Prentice Hall, Inc. All rights reserved. Outline 15 Fig. 12.8Book table of contents as XML. 1 2 3 4 5 6 7 8 9 Deitel's XML Primer 10 11 12 Paul 13 Deitel 14 15 16 17 Welcome 18 Easy XML 19 XML Elements? 20 Entities 21 22 23 24
16
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 1 2 3 4 5 6<xsl:stylesheet version = "1.0" 7 xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"> 8 9 10 11 12 13 14 15 16 17 ISBN - 18 19 20 21 22 23 24 by, 25 26 27 28 29 Element for-each applies element contents to each preface element node Use element sort to sort nodes (selected by for- each ) in ascending order
17
2002 Prentice Hall, Inc. All rights reserved. Outline 17 Fig. 12.9 Transforming XML data into HTML (Part 2). Iterate and sort chapter elements Iterate and sort appendix elements 30 31 32 Preface 33 34 35 36 ( 37 pages ) 38 39 40 41 42 43 44 45 46 Chapter 47 48 49 50 ( 51 pages ) 52 53 54 55 56 57 58 59 60 Appendix 61 Iterate and sort chapter elementsIterate and sort appendix elements
18
2002 Prentice Hall, Inc. All rights reserved. Outline 18 Fig. 12.9 Transforming XML data into HTML (Part 3). 62 63 64 ( 65 pages ) 66 67 68 69 70 71 72 73
19
2002 Prentice Hall, Inc. All rights reserved. Outline 19 Fig. 12.10 Output of the transformation. 1 2 3 ISBN 999-99999-9-X - Deitel's XML Primer 4 5 6 7 Deitel's XML Primer 8 by Deitel, Paul 9 10 11 12 Preface 1 13 Welcome ( 2 pages ) 14 15 16 17 Chapter 1 18 Easy XML ( 4 pages ) 19 20 21 22 Chapter 2 23 XML Elements? ( 2 pages ) 24 25 26 27 Appendix 1 28 Entities ( 9 pages ) 29 30 31 32 33
20
2002 Prentice Hall, Inc. All rights reserved. 20 Output from Fig. 12.10
21
2002 Prentice Hall, Inc. All rights reserved. 21 12.6 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
22
2002 Prentice Hall, Inc. All rights reserved. Outline 22 Fig. 12.11 Using conditional elements. 1 2 3 4 5 6<xsl:stylesheet version = "1.0" 7 xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"> 8 9 10 11 12 13 Appointments 14 15 16 17 18 19 20 21 22 Year: 23 24 25 26 <xsl:sort select = "../@day" order = "ascending" 27 data-type = "number"/>
23
2002 Prentice Hall, Inc. All rights reserved. Outline 23 Fig. 12.11 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 28 29 Day: 30 / 31 32 33 34 35 36 <xsl:when test = 37 "@time > '0500' and @time < '1200'"> 38 Morning ( ): 39 40 41 <xsl:when test = 42 "@time > '1200' and @time < '1700'"> 43 Afternoon ( ): 44 45 46 <xsl:when test = 47 "@time > '1200' and @time < '1700'"> 48 Evening ( ): 49 50 51 <xsl:when test = 52 "@time > '1200' and @time < '1700'"> 53 Night ( ): 54 55 56 57 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
24
2002 Prentice Hall, Inc. All rights reserved. Outline 24 Fig. 12.11 Using conditional elements (Part 3). 59 60 61 62 63 64 65 n/a 66 67 68 69 70 71 72 73
25
2002 Prentice Hall, Inc. All rights reserved. 25 Output from Fig. 12.11
26
2002 Prentice Hall, Inc. All rights reserved. 26 12.7 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.)
27
2002 Prentice Hall, Inc. All rights reserved. Outline 27 Fig. 12.12 Using the XSLT element copy. Apply templates to element’s child nodes Replace element content with text 1 2 3 4 5 6<xsl:stylesheet version = "1.0" 7 xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"> 8 9 10 11 12 13 14 15 16 17 18 19 20 How about 'Hi World' for a change! 21 22 23 24 25 Replace element content with text Apply templates to element’s child nodes
28
2002 Prentice Hall, Inc. All rights reserved. Outline 28 Fig. 12.13 Resulting transformation. 1 2 3 4 How about 'Hi World' for a change! 5 6
29
2002 Prentice Hall, Inc. All rights reserved. Outline 29 Fig. 12.14 xsl:copy- of element. Create comment in resulting XML document Duplicate nodes selected by attribute select into resulting XML document 1 2 3 4 5 6<xsl:stylesheet version = "1.0" 7 xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"> 8 9 10 11 12 The following XML tree has been copied into output. 13 14 15 16 17 18 Create comment in resulting XML document Duplicate nodes selected by attribute select into resulting XML document
30
2002 Prentice Hall, Inc. All rights reserved. Outline 30 Fig. 12.15 Output of the copy-of transformation. 1 2 3 4 Welcome to XSLT! 5
31
2002 Prentice Hall, Inc. All rights reserved. 31 12.8 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
32
2002 Prentice Hall, Inc. All rights reserved. Outline 32 Fig. 12.16 XSLT document being imported. 1 2 3 4 5 6<xsl:stylesheet version = "1.0" 7 xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"> 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 Author: 27, 28 29
33
2002 Prentice Hall, Inc. All rights reserved. Outline 33 Fig. 12.16 XSLT document being imported (Part 2). Template to match any text and leftover element nodes 30 31 32 33 34 35 Template to match any text and leftover element nodes
34
2002 Prentice Hall, Inc. All rights reserved. Outline 34 Fig. 12.17 Importing another XSLT document. Use templates in usage2.xsl Template for element title used instead of one defined in usage2.xsl 1 2 3 4 5 6<xsl:stylesheet version = "1.0" 7 xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"> 8 9 10 11 <!-- This template has higher precedence over the 12 templates being imported --> 13 14 15 16 17 18 19 20 21 Use templates in usage2.xsl Template for element title used instead of one defined in usage2.xsl
35
2002 Prentice Hall, Inc. All rights reserved. Outline 35 Fig. 12.18 Resulting HTML document using XSLT import. 1 2 3 Deitel's XML Primer 4 5 6 Author: Deitel, Paul 7 8 9
36
2002 Prentice Hall, Inc. All rights reserved. Outline 36 Fig. 12.19 Combining stylesheets using xsl:include. Include author.xsl and chapters.xsl, but templates in stylesheets have same precedence as local templates 1 2 3 4 5 6<xsl:stylesheet version = "1.0" 7 xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"> 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 Include author.xsl and chapters.xsl, but templates in stylesheets have same precedence as local templates
37
2002 Prentice Hall, Inc. All rights reserved. Outline 37 Fig. 12.20 XSLT document for rendering the author’s name. 1 2 3 4 5 6<xsl:stylesheet version = "1.0" 7 xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"> 8 9 10 11 Author: 12, 13 14 15 16 17 18
38
2002 Prentice Hall, Inc. All rights reserved. Outline 38 Fig. 12.21 XSLT document for rendering chapter names. 1 2 3 4 5 6<xsl:stylesheet version = "1.0" 7 xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"> 8 9 10 Chapters: 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
39
2002 Prentice Hall, Inc. All rights reserved. Outline 39 Fig. 12.22 Output of an XSLT document using element include. Output from Fig. 12.22 1 2 3 Deitel’s XML Primer 4 Author: 5 Deitel, Paul 6 7 Chapters: 8 9 Easy XML 10 XML Elements? 11 12 13
40
2002 Prentice Hall, Inc. All rights reserved. 40 12.9 Variables Variables for processing information –Element variable
41
2002 Prentice Hall, Inc. All rights reserved. Outline 41 Fig. 12.23 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 1 2 3 4 5 6<xsl:stylesheet version = "1.0" 7 xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"> 8 9 10 11 12 Number of pages = 13 <xsl:variable name = "sum" 14 select = "sum(book/chapters/*/@pages)"/> 15 16 17 18 19 20 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
42
2002 Prentice Hall, Inc. All rights reserved. Outline 42 Fig. 12.24 Output of transformation. 1 2 Number of pages = 17
43
2002 Prentice Hall, Inc. All rights reserved. 43 12.10 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
44
2002 Prentice Hall, Inc. All rights reserved. Outline 44 Fig. 12.25 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 1 2 3 4 5 6 7 8 9 10 11 12 The Complete C++ Training Course: Third Edition 13 14 15 16 C++ How to Program: Third Edition 17 18 19 20 C++ Multimedia Cyber Classroom: Third Edition 21 22 23 24 25 26 27 28 The Complete Java 2 Training Course: Third Edition 29 30 Root element product contains completeTrainingCourses Each completeTrainingCourse element contains title, book and cd
45
2002 Prentice Hall, Inc. All rights reserved. Outline 45 Fig. 12.25 XML document containing information about complete training courses and Web- based training (Part 2). Root element product also contains books 31 32 Java How to Program: Third Edition 33 34 35 36 Java Multimedia Cyber Classroom: Third Edition 37 38 39 40 41 42 The Complete XML Training Course 43 XML How to Program 44 XML Multimedia Cyber Classroom 45 46 47 48 49 50 51 52 Getting Started with Microsoft Visual C++ 53 6 with an Introduction to MFC 54 55 56 C How to Program 57 58 Root element product also contains books
46
2002 Prentice Hall, Inc. All rights reserved. Outline 46 Fig. 12.25 XML document containing information about complete training courses and Web- based training (Part 3). Root element product also contains webBased- Training 59 60 Introduction to Java programming 61 Advanced C++ programming 62 Programming COM+ with Visual Basic 63 64 65 Root element product also contains webBasedTraining
47
2002 Prentice Hall, Inc. All rights reserved. Outline 47 Fig. 12.26 XSL stylesheet for transforming axes.xml into HTML. Select all product children of root node Select node descendents of completeTraining- Courses elements 1 2 3 4 5 6<xsl:stylesheet version = "1.0" 7 xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"> 8 9 10 11 12 13 14 Axes example 15 16 17.node { font-family: monospace; 18 font-weight: bold; } 19 20 21 22 23 Decendents of 24 25 completeTrainingCourse are: 26 27 <xsl:apply-templates select = 28 "completeTrainingCourses//node()"/> 29 30 Select all product children of root node Select node descendents of completeTrainingCourses elements
48
2002 Prentice Hall, Inc. All rights reserved. Outline 48 Fig. 12.26 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 33 34 <xsl:apply-templates 35 select = "webBasedTraining"/> 36 37 38 Parent of 39 C How to Program is 40 41 42 43 <xsl:value-of select = 44 "name(//parent::node()[. = 'C How to Program'])"/> 45 46 47 48 49 Child of 50 title 51 element for the second 52 53 completeTrainingCourse is 54 55 56 <xsl:value-of select = 57 "//child::completeTrainingCourse[ 2 ]/title"/> 58 59 60 Apply templates to add string- values for webBasedTraining nodes to HTML document
49
2002 Prentice Hall, Inc. All rights reserved. Outline 49 Fig. 12.26 XSL stylesheet for transforming axes.xml into HTML (Part 3). 61 62 Is product 63 64 the ancestor of 65 XML How to Program ? 66 67 <xsl:if test = "name(//node()[. = 68 'XML How to Program']/ancestor::product) = 'product'"> 69 Yes 70 71 72 73 74 First subject preceding 75 76 77 Programming COM+ with Visual Basic 78 is 79 80 81 <xsl:value-of select = 82 "//subject[. = 'Programming COM+ with Visual Basic' 83 ]/preceding-sibling::subject[ 1 ]"/> 84 85 86 87 88 First subject following 89
50
2002 Prentice Hall, Inc. All rights reserved. Outline 50 Fig. 12.26 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 90 91 Introduction to Java programming 92 is 93 94 95 <xsl:value-of select = 96 "//subject[. = 'Introduction to Java programming' 97 ]/following-sibling::subject[ 1 ]"/> 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 Retrieve string-values for title, book and cd elements and add them to HTML document Add string-values for subject element to HTML document
51
2002 Prentice Hall, Inc. All rights reserved. 51 Fig. 12.27 HTML page generated by transforming axes.xml with axes.xsl
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.