760 likes | 804 Vues
Final Project. Pre-presentation: 10 minutes. 6/3 Class hours change to ?. XSL: eXtensible Stylesheet Language Transformations (XSLT 2.0). Version 2.0 W3C Working Draft 4 April 2005 A language for transforming XML documents into other text-based (including XML) documents.
E N D
Final Project • Pre-presentation: 10 minutes • 6/3 Class hours change to ?
XSL: eXtensible Stylesheet Language Transformations (XSLT 2.0) Version 2.0 W3C Working Draft 4 April 2005 A language for transforming XML documents into other text-based (including XML) documents
eXtensible Stylesheet Language (XSL) • Introduction • XSLT Processor • XSLT Transformation • How To? • Elements (partial) Introduction • Conditional Processing • Copying Nodes • Combining Stylesheets • Variables • XSLT Elements Reference • Summary
Why XSL? “Why two Style Sheet languages?” • A need for an XML-based Stylesheet Language: The World Wide Web Consortium (W3C) started to develop XSL • CSS =HTML Style Sheets • The unique features are that CSS can be used to style HTML documents • Adding styles to HTML elements are simple • Telling a browser to display an element in a special font or color, is easy with CSS.
Why XSL? “Why two Style Sheet languages?” • XSL = XML Style Sheets • XSL is able to transform documents • XML does not use predefined tags (we can use any tag-names we like), and the meanings of these tags are not well understood. • XSL describes how the XML document should be displayed! • "Using XSL and CSS Together“ • by Håkon Lie and Bert Bos. [from the W3C 'Style' Page] • The two languages complement each other and can be used together. • W3C will work hard to ensure that interoperable implementations of the formatting model is available
eXtensible Stylesheet Language (XSL) • XSL is a family of recommendations for defining XML document transformation and presentation. • It consists of three parts: • XSL Transformations (XSLT):Week11 • A language for transforming XML documents • XML Path Language (XPath):Week10 • An expression language used by XSLT to access or refer to parts of an XML document. • A language for navigating XML documents • XSL Formatting Objects (XSL-FO):Week12 • A language for formatting XML documents
eXtensible Stylesheet Language (XSL) • XSL is a language that transforms XML into other documents, • Filters and sorts XML data • Formats data based on the data value
Introduction • What is XSLT? • XSLT stands for eXtensible Stylesheet Language Transformation (XSLT) • XSLT is the most important part of the XSL standards • A language for transforming XML documents into other text-based (including XML, HTML, text) documents • XSLT uses XPath tonavigate nodesin XML documents • XSLT is a W3C Standard
Introduction (cont.) • With XSLT you can add/remove elements and attributes to/from the output file. • You can • Rearrange and sort elements, • Perform tests and make decisions about which elements to hide and display, and a lot more. • The transformation process is that XSLT transforms an XML source tree into an XML result tree.
Introduction (cont.) • XSLT Uses XPath • XSLT uses XPath to find information in an XML document. • XPath is used to navigate through elements and attributes in XML documents. • How Does it Work? • In the transformation process, XSLT uses XPath to define parts of the source document that should matchone or more predefined templates. • When a match is found, XSLT will transform the matching part of the source document into the result document
XSLT Processor • XSLT processor • Microsoft Internet Explorer 5, not compatiblewithW3C standard ( IE 6 and up support) • 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
XSLT Processor (cont.) • Not all Internet browsers have full support for XML and XSLT. • Firefox 1.0.2 • This web browser has support for XML and XSLT (and CSS). • Mozilla 1.8 • Mozilla includes Expat for XML parsing and has support to display XML + CSS. Mozilla also has some support for Namespaces. • Mozilla 1.8 is available with an XSLT implementation. • Netscape 8 • Netscape 8 is based on the Mozilla engine. Same XML / XSLT support as Mozilla. • Opera 8 • This web browser supports XML
XSLT - Transformation • Correct Style Sheet Declaration • The root element that declares the document to be an XSL style sheet is <xsl:stylesheet> or <xsl:transform>. • Note:<xsl:stylesheet> and <xsl:transform> are completely synonymous and either can be used! • The correct way to declare an XSL style sheet according to the W3C XSLT Recommendation is: or:
XSLT – Transformation (cont.) or: • Note: The xmlns:xsl="http://www.w3.org/1999/XSL/Transform" identifies the official W3C XSL recommendation namespace. • If you use this namespace, you must also include the attribute version="1.0". • Note: If you are using IE 6.0 or Netscape 6 you should use one of the codes above.
XSLT – Transformation (cont.) • Incorrect Style Sheet Declaration • This was the correct way to declare an XSL style sheet according to the W3C XSL Working Draft: Note:The declaration above is OUTDATED, but if you are usingIE 5you should use the (incorrect) code above
Top-Level Elements in xsl:stylesheet • The xsl:stylesheet element may contain the following types of elements. • An element occurring as a child of an xsl:stylesheet element is called a top-level element. xsl:import xsl:include xsl:strip-space xsl:preserve-space xsl:output xsl:key xsl:decimal-format xsl:namespace-alias xsl:attribute-set xsl:variable xsl:param xsl:template
XSLT Transformation – How to? • Start with your XML Document • We want to transform the following XML document ("cdcatalog.xml") into HTML:
XSLT Transformation – How to? (cont.) • Create an XSL Style Sheet • Then create an XSL Style Sheet ("cdcatalog.xsl") with a transformation template
XSLT Transformation – How to? (cont.) • Link the XSL Style Sheet to the XML Document • Finally, add an XSL Style Sheet reference to your XML document (“ex.xml") <?xml version="1.0" encoding="ISO-8859-1"?> <?xml-stylesheet type="text/xsl" href=“ex.xsl"?> …………….... Result: If you have an XSLT compliant browser it will nicely transform your XML into HTML!
xsl:apply-template • The apply-template element is always found inside a template body. • It defines a set of nodes to be processed. • In this process, it then may find any 'sub'template rules to process (child elements of element in context). • If you want the apply-template instruction to only process certain child elements, you can define a select attribute, to only process specific nodes. • In the following example, only the 'name' and 'address' elements in the root document element will be processed: <xsl:template match="/"> <xsl:apply-templates select="person/name | person/address"/> </xsl:template>
Conditional Processing • There are two instructions in XSLT that support conditional processing in a template: • xsl:if The instruction provides simple if-then conditionality • xsl:choose The instruction supports selection of one choice when there are several possibilities.
Conditional Processing with <xsl:if> <!-- Category: instruction --><xsl:iftest = boolean-expression> <!-- Content: template --></xsl:if> • If the result (boolean expression) is true, then the content template is instantiated; otherwise, nothing is created • <xsl:template match="namelist/name"> • <xsl:apply-templates/> • <xsl:if test="not(position()=last())"> • , • </xsl:if> • </xsl:template> <xsl:template match="item"> <tr> <xsl:if test="position() mod 2 = 0"> <xsl:attribute name="bgcolor"> Yellow </xsl:attribute> </xsl:if> <xsl:apply-templates/> </tr> </xsl:template> The names in a group of names are formatted as a comma separated list
Conditional Processing with <xsl:choose> • The <xsl:choose> element is used in conjunction with <xsl:when> and <xsl:otherwise> to express multiple conditional tests <!-- Category: instruction --><xsl:choose> <!-- Content: (xsl:when+, xsl:otherwise?) --></xsl:choose> <xsl:when test = boolean-expression> <!-- Content: template --></xsl:when> <xsl:otherwise> <!-- Content: template --></xsl:otherwise>
Conditional Processing with <xsl:choose> (cont.) ………………………………… <xsl:for-each select="catalog/cd"> <tr> <td><xsl:value-of select="title"/></td> <xsl:choose> <xsl:when test="price > 10"> <td bgcolor="#ff00ff"> <xsl:value-of select="artist"/></td> </xsl:when> <xsl:otherwise> <td><xsl:value-of select="artist"/></td> </xsl:otherwise> </xsl:choose> </tr> </xsl:for-each> ………………………………….. • Where to put the Choose Condition • To insert a conditional choose test against the content of the XML file, simply add the <xsl:choose>, <xsl:when>, and <xsl:otherwise> elements to your XSL document like this
1 <?xml version = "1.0"?> 2 3 <!-- Fig. 12.11 : conditional.xsl --> 4 <!-- xsl:choose, xsl:when, and xsl:otherwise --> 5 6 <xsl:stylesheet version = "1.0" 7 xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"> 8 9 <xsl:template match = "/"> 10 <html> 11 12 <body> 13 Appointments 14 <br/> 15 <xsl:apply-templates select = "planner/year"/> 16 </body> 17 18 </html> 19 </xsl:template> 20 21 <xsl:template match = "year"> 22 <strong>Year:</strong> 23 <xsl:value-of select = "@value"/> 24 <br/> 25 <xsl:for-each select = "date/note"> 26 <xsl:sort select = "../@day" order = "ascending" 27 data-type = "number"/> Fig. 12.11 Using conditional elements.
28 <strong> 29 Day: 30 <xsl:value-of select = "../@day"/>/ Allow conditional statements 31 <xsl:value-of select = "../@month"/> 32 </strong> 33 Content of when element used if condition is met 34 <xsl:choose> 35 36 <xsl:when test = 37 "@time > '0500' and @time < '1200'"> 38 Morning (<xsl:value-of select = "@time"/>): Content of otherwise element used if no conditions are met 39 </xsl:when> 40 41 <xsl:when test = 42 "@time > '1200' and @time < '1700'"> 43 Afternoon (<xsl:value-of select = "@time"/>): 44 </xsl:when> 45 46 <xsl:when test = 47 "@time > '1200' and @time < '1700'"> 48 Evening (<xsl:value-of select = "@time"/>): 49 </xsl:when> 50 51 <xsl:when test = 52 "@time > '1200' and @time < '1700'"> 53 Night (<xsl:value-of select = "@time"/>): 54 </xsl:when> 55 56 <xsl:otherwise> 57 Entire day: 58 </xsl:otherwise> Fig. 12.11 Using conditional elements (Part 2).Allow conditional statementsContent of when element used if condition is metContent of otherwise element used if no conditions are met
59 60 </xsl:choose> 61 62 <xsl:value-of select = "."/> 63 64 <xsl:if test = ". = ''"> 65 n/a 66 </xsl:if> 67 68 <br/> 69 </xsl:for-each> 70 71 </xsl:template> 72 73 </xsl:stylesheet> Fig. 12.11 Using conditional elements (Part 3).
Creating Elements • The xsl:element element allows an element to be created with a computed name • The content of the xsl:element element is a template for the attributes and children of the created element <!-- Category: instruction --><xsl:element name = { qname } namespace = { uri-reference } use-attribute-sets = qnames> <!-- Content: template --></xsl:element>
Creating Attributes • The xsl:attribute element is used to add attributes to result elements whether created by literal result elements in the stylesheet or by instructions such as xsl:element • Instantiating an xsl:attribute element adds an attribute node to the containing result element node. • The content of the xsl:attribute element is a template for the value of the created attribute <!-- Category: instruction --><xsl:attribute name = { qname } namespace = { uri-reference}> <!-- Content: template --></xsl:attribute>
1 <?xml version = "1.0"?> 2 3 <!-- Fig. 12.4 : games.xml --> 4 <!-- Sports Database --> 5 6 <sports> 7 8 <game title = "cricket"> 9 <id>243</id> 10 11 <para> 12 More popular among commonwealth nations. 13 </para> 14 </game> 15 16 <game title = "baseball"> 17 <id>431</id> 18 19 <para> 20 More popular in America. 21 </para> 22 </game> 23 24 <game title = "soccer"> 25 <id>123</id> 26 27 <para> 28 Most popular sport in the world. 29 </para> 30 </game> 31 32 </sports> Fig. 12.4 XML document containing a list of sports.
1 <?xml version = "1.0"?> 2 3 <!-- Fig. 12.5 : elements.xsl --> Use attribute match to select XML document root 4 <!-- Using xsl:element and xsl:attribute --> Element apply-templates applies XSLT document templates to specific nodes 5 match element sports and apply templates to sport node’s child nodes 6 <xsl:stylesheet version = "1.0" 7 xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"> Create element title 8 Create attribute id for element title 9 <xsl:template match = "/"> Create comment with element para comments 10 <xsl:apply-templates/> 11 </xsl:template> 12 13 <xsl:template match = "sports"> 14 <sports> 15 <xsl:apply-templates/> 16 </sports> 17 </xsl:template> 18 19 <xsl:template match = "game"> 20 <xsl:element name = "{@title}"> 21 22 <xsl:attribute name = "id"> 23 <xsl:value-of select = "id"/> 24 </xsl:attribute> 25 26 <comment> 27 <xsl:value-of select = "para"/> 28 </comment> 29 30 </xsl:element> 31 </xsl:template> 32 33 </xsl:stylesheet> Fig. 12.5 Using XSLT to create elements and attributes.Use attribute match to select XML document rootElement apply-templates applies XSLT document templates to specific nodesmatch element sports and apply templates to sport node’s child nodesCreate element titleCreate attribute id for element titleCreate comment with element para comments
1 <?xml version = "1.0" encoding = "UTF-8"?> 2 <sports> 3 4 <cricket id = "243"> 5 <comment> 6 More popular among commonwealth nations. 7 </comment> 8 </cricket> 9 10 <baseball id = "431"> 11 <comment> 12 More popular in America. 13 </comment> 14 </baseball> 15 16 <soccer id = "123"> 17 <comment> 18 Most popular sport in the world. 19 </comment> 20 </soccer> 21 22 </sports> Fig. 12.7 Output of transformation.
Copying Nodes • Duplicate nodes from sources tree to result tree • Elementcopy • Produce copy of context node (current node) • Place copy in result tree • No child nodes or attributes are duplicated <!-- Category: instruction --><xsl:copy use-attribute-sets = qnames> <!-- Content: template --></xsl:copy>
Copying Nodes • Duplicate nodes from sources tree to result tree • Element copy-of • Performs copy of subtree • Duplicates children (i.e., text, processing instructions, etc.) • Duplicates attributes
1 <?xml version = "1.0"?> 2 3 <!-- Fig. 12.12: copyIntro.xsl --> 4 <!-- xsl:copy example using Intro.xml --> 5 6 <xsl:stylesheet version = "1.0" 7 xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"> 8 9 <xsl:template match = "myMessage"> Apply templates to element’s child nodes Replace element content with text 10 11 <xsl:copy> 12 <xsl:apply-templates/> 13 </xsl:copy> 14 15 </xsl:template> 16 17 <xsl:template match = "message"> 18 19 <xsl:copy> 20 How about 'Hi World' for a change! 21 </xsl:copy> 22 23 </xsl:template> 24 25 </xsl:stylesheet> Fig. 12.12 Using the XSLT element copy.Apply templates to element’s child nodesReplace element content with text
1 <?xml version = "1.0" encoding = "UTF-8"?> 2 <myMessage> 3 <message> 4 How about 'Hi World' for a change! 5 </message> 6 </myMessage> Fig. 12.13 Resulting transformation.
1 <?xml version = "1.0"?> 2 3 <!-- Fig. 12.14 : usingCopyOf.xsl --> 4 <!-- xsl:copy-of example using intro.xml --> Create comment in resulting XML document 5 Duplicate nodes selected by attribute select into resulting XML document 6 <xsl:stylesheet version = "1.0" 7 xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"> 8 9 <xsl:template match = "myMessage"> 10 11 <xsl:comment> 12 The following XML tree has been copied into output. 13 </xsl:comment> 14 15 <xsl:copy-of select = "."/> 16 </xsl:template> 17 18 </xsl:stylesheet> Fig. 12.14 xsl:copy-ofelement.Create comment in resulting XML documentDuplicate nodes selected by attribute select into resulting XML document
1 <?xml version = "1.0" encoding = "UTF-8"?> 2 <!-- The following XML tree has been copied into output. --> 3 <myMessage> 4 <message>Welcome to XSLT!</message> 5 </myMessage> Fig. 12.15 Output of the copy-of transformation.
Combining Stylesheets • XSLT document can import others • Element import • local templates have higher precedence as included templates • The xsl:import element is only allowed as a top-level element • Element include • Included templates have same precedence as local templates • The inclusion works at the XML tree level. • The resource located by the href attribute value is parsed as an XML document <xsl:import href = uri-reference /> <!-- Category: top-level-element --><xsl:include href = uri-reference />
1 <?xml version = "1.0"?> 2 3 <!-- Fig. 12.16 : usage2.xsl --> 4 <!-- xsl:import example --> 5 6 <xsl:stylesheet version = "1.0" 7 xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"> 8 9 <xsl:template match = "book"> 10 <html> 11 12 <body> 13 <xsl:apply-templates/> 14 </body> 15 </html> 16 17 </xsl:template> 18 19 <xsl:template match = "title"> 20 <xsl:value-of select = "."/> 21 </xsl:template> 22 23 <xsl:template match = "author"> 24 <br/> 25 26 <p>Author: 27 <xsl:value-of select = "lastName"/>, 28 <xsl:value-of select = "firstName"/> 29 </p> Fig. 12.16 XSLT document being imported.
30 31 </xsl:template> 32 33 <xsl:template match = "*|text()"/> Template to match any text and leftover element nodes 34 35 </xsl:stylesheet> Fig. 12.16 XSLT document being imported (Part 2).Template to match any text and leftover element nodes
1 <?xml version = "1.0"?> 2 3 <!-- Fig. 12.17 : usage1.xsl --> 4 <!-- xsl:import example using usage.xml --> Use templates in usage2.xsl 5 Template for element title used instead of one defined in usage2.xsl 6 <xsl:stylesheet version = "1.0" 7 xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"> 8 9 <xsl:import href = "usage2.xsl"/> 10 11 <!-- This template has higher precedence over the 12 templates being imported --> 13 <xsl:template match = "title"> 14 15 <h2> 16 <xsl:value-of select = "."/> 17 </h2> 18 19 </xsl:template> 20 21 </xsl:stylesheet> Fig. 12.17 Importing another XSLT document.Use templates in usage2.xslTemplate for element title used instead of one defined in usage2.xsl
1 <html> 2 <body> 3 <h2>Deitel's XML Primer</h2> 4 <br> 5 <p> 6 Author: Deitel, Paul 7 </p> 8 </body> 9 </html> Fig. 12.18 Resulting HTML document using XSLT import.
1 <?xml version = "1.0"?> 2 3 <!-- Fig. 12.19 : book.xsl --> 4 <!-- xsl:include example using usage.xml --> 5 6 <xsl:stylesheet version = "1.0" 7 xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"> 8 9 <xsl:template match = "/"> Include author.xsl and chapters.xsl, but templates in stylesheets have same precedence as local templates 10 11 <html> 12 <body> 13 <xsl:apply-templates select = "book"/> 14 </body> 15 </html> 16 17 </xsl:template> 18 19 <xsl:template match = "book"> 20 21 <h2> 22 <xsl:value-of select = "title"/> 23 </h2> 24 25 <xsl:apply-templates/> 26 </xsl:template> 27 28 <xsl:include href = "author.xsl"/> 29 <xsl:include href = "chapters.xsl"/> 30 31 <xsl:template match = "*|text()"/> 32 33 </xsl:stylesheet> 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 <?xml version = "1.0"?> 2 3 <!-- Fig. 12.20 : author.xsl --> 4 <!-- xsl:include example using usage.xml --> 5 6 <xsl:stylesheet version = "1.0" 7 xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"> 8 9 <xsl:template match = "author"> 10 11 <p>Author: 12 <xsl:value-of select = "lastName"/>, 13 <xsl:value-of select = "firstName"/> 14 </p> 15 16 </xsl:template> 17 18 </xsl:stylesheet> Fig. 12.20 XSLT document for rendering the author’s name.
1 <?xml version = "1.0"?> 2 3 <!-- Fig. 12.21 : chapters.xsl --> 4 <!-- xsl:include example using usage.xml --> 5 6 <xsl:stylesheet version = "1.0" 7 xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"> 8 9 <xsl:template match = "chapters"> 10 Chapters: 11 12 <ul> 13 <xsl:apply-templates select = "chapter"/> 14 </ul> 15 </xsl:template> 16 17 <xsl:template match = "chapter"> 18 19 <li> 20 <xsl:value-of select = "."/> 21 </li> 22 23 </xsl:template> 24 25 </xsl:stylesheet> Fig. 12.21 XSLT document for rendering chapter names.
1 <html> 2 <body> 3 <h2>Deitel’s XML Primer</h2> 4 <p>Author: 5 Deitel, Paul</p> 6 7 Chapters: 8 <ul> 9 <li>Easy XML</li> 10 <li>XML Elements?</li> 11 </ul> 12 </body> 13 </html> Fig. 12.22 Output of an XSLT document using element include. Output from Fig. 12.22
Variables and Parameters • Variables for processing information: variable andparameter • A variable is a name that may be bound to a value • The value of a variable is bound (the value of the variable) can be an object of any of the types that can be returned by expressions • The difference is that the value specified on the xsl:param variable is only a default value for the binding <!-- Category: top-level-element --><!-- Category: instruction --><xsl:variablename =qname select = expression> <!-- Content: template --></xsl:variable> <!-- Category: top-level-element --><xsl:paramname = qname select = expression> <!-- Content: template --></xsl:param>
1 <?xml version = "1.0"?> 2 3 <!-- Fig. 12.23 : variables.xsl --> 4 <!-- using xsl:variables --> Create element variable with attribute name to store number of pages in book 5 6 <xsl:stylesheet version = "1.0" 7 xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"> Element value-of uses dollar sign ($) to reference and output variable sum 8 9 <xsl:template match = "/"> 10 11 <total> 12 Number of pages = 13 <xsl:variable name = "sum" 14 select = "sum(book/chapters/*/@pages)"/> 15 <xsl:value-of select = "$sum"/> 16 </total> 17 18 </xsl:template> 19 20 </xsl:stylesheet> Fig. 12.23 Example for xsl:variable.Create element variable with attribute name to store number of pages in bookElement value-of uses dollar sign ($) to reference and output variable sum