XML For Dummies<br />Book Author : Lucinda Dykes & Ed Tittle<br />Slides Prepared By: Son TN<br />Part 4 : Transforming an...
Contents<br />What is XSL ?<br />The Two Faces of XSL<br />XSL Stylesheets Are XML Documents<br />A Simple Transformation ...
12.1 What is XSL ?<br />XSL is a language for expressing style sheets.<br />An XSL style sheet is, like with CSS, a file t...
12.2 The Two Faces of XSL<br />While XSL originally started out as a single language, development quickly split it into tw...
12.2.1 XSLT<br />
12.2.1.1 What is XSLT ? <br />eXtensibleStylesheet Language for Transformations<br />Language for transforming XML documen...
12.2.1.2 The ways to use XSLT<br />You can use XSLT in a couple of especially intriguing ways:<br />To transform documents...
12.2.1.3 The ins and outs of data exchange<br />It’s entirely possible that two (or more) systems exchanging data don’t us...
12.2.1.4 The role of XPath<br />Without XPath, XSLT simply wouldn’t work.<br />XPath is the mechanism XSLT uses to point t...
12.2.2 XSL-FO<br />
12.2.2.1 Overview<br />The names of most XML elements describe the semantic meaning of the content they contain<br />The c...
12.2.2.2 What is XSL-FO ?<br />XSL-FO is a complete XML application for describing the precise layout of text on a page.<b...
12.2.2.3 Categories of XSL-FO<br />The different XSL-FO formatting objects are organized into these eight categories:<br /...
12.3 XSL Stylesheets Are XML Documents<br />XSLT stylesheet is an XML document, so:<br />The whole stylesheet should be we...
12.4 A Simple Transformation Using XSLT<br />An XSLT stylesheet consists of instructions that tell the computer :<br />How...
12.5 An XSLT Stylesheet for Converting XML to HTML<br />If we open books2.xml in Internet Explorer, the program ignores al...
12.5 An XSLT Stylesheet for Converting XML to HTML (Cont)<br />If you use the following XSLT stylesheet with books2.xml an...
12.5 An XSLT Stylesheet for Converting XML to HTML (Cont)<br />&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;...
12.5.1 The pieces of the stylesheet puzzle <br />XSLT is a robust conversion tool and has many facets.<br />In this sectio...
12.5.1.1 Using patterns<br />Template rules identify the nodes to which they apply by using a pattern. <br />To identify t...
12.5.1.1 Using patterns (Cont)<br />In our XSLT document, this xsl:template element contains the entire HTML document — bu...
12.5.1.2 Using instructions to get results<br />In addition to using a pattern to identify which node(s) in the source doc...
12.5.2 Processing element content<br />You use the xsl:apply-templates instruction when you want to return the content and...
12.5.2.1 Choosing templates<br />What template gets applied here?<br />It depends; the XSLT processor looks for the best t...
12.5.2.2 Adding external CSS<br />As in listing 11.2, A link to an external CSS stylesheet in our XSLT markup<br />&lt;lin...
12.5.2.3 Dealing with repeating elements<br />Our XSLT stylesheet (books2.xslt) deals with repeating elements by using a s...
12.6 Creating an XSLT Stylesheet with XSLT Editors<br />If hand-coding is not your favorite art form, you can also use an ...
12.6 Creating an XSLT Stylesheet with XSLT Editors (Cont)<br />Open books2.xsd (or any schema document) in StyleVision.<br />
12.6 Creating an XSLT Stylesheet with XSLT Editors (Cont)<br />Select the book element from the document diagram, and drag...
12.6 Creating an XSLT Stylesheet with XSLT Editors (Cont)<br />Choose Create Table from the displayed menu.<br />
12.6 Creating an XSLT Stylesheet with XSLT Editors (Cont)<br />In the Create Dynamic Table dialog box, deselect any attrib...
12.6 Creating an XSLT Stylesheet with XSLT Editors (Cont)<br />To add formatting properties, place the cursor inside the t...
12.7 Summary<br />Picking your style of XSL<br />Transforming XML documents with XSLT<br />Using templates for style<br />...
Upcoming SlideShare
Loading in …5
×

Xml For Dummies Chapter 12 Handling Transformations With Xsl it-slideshares.blogspot.com

2,722 views

Published on

it-slideshares.blogspot.com

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,722
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
77
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Xml For Dummies Chapter 12 Handling Transformations With Xsl it-slideshares.blogspot.com

  1. 1. XML For Dummies<br />Book Author : Lucinda Dykes & Ed Tittle<br />Slides Prepared By: Son TN<br />Part 4 : Transforming and Processing XML<br />Chapter 12 : Handling Transformations with XSL<br />
  2. 2. Contents<br />What is XSL ?<br />The Two Faces of XSL<br />XSL Stylesheets Are XML Documents<br />A Simple Transformation Using XSLT<br />An XSLT Stylesheet for Converting XML to HTML<br />Creating an XSLT Stylesheet with XSLT Editors<br />
  3. 3. 12.1 What is XSL ?<br />XSL is a language for expressing style sheets.<br />An XSL style sheet is, like with CSS, a file that describes how to display an XML document of a given type.<br />XSL is also a powerful tool for converting your XML document from one set of markup (vocabulary) to another<br />it’s really just XML.<br />XSL shares the functionality and is compatible with CSS2 (although it uses a different syntax) . It also adds :<br />A transformation language for XML documents: XSLT <br />Advanced styling features, expressed by an XML document type which defines a set of elements called Formatting Objects, and attributes<br />
  4. 4. 12.2 The Two Faces of XSL<br />While XSL originally started out as a single language, development quickly split it into two independent components.<br />A language for &quot;transforming&quot; XML documents by reorganizing and restructuring XML data trees (known as XSLT).<br />An XML vocabulary to handle the formatting and layout of the result (now known as XSL or, sometimes, XSL-FO). <br />
  5. 5. 12.2.1 XSLT<br />
  6. 6. 12.2.1.1 What is XSLT ? <br />eXtensibleStylesheet Language for Transformations<br />Language for transforming XML documents<br />A programming language for XML documents<br />A functional language, based on value substitution<br />Augmented with pattern matching<br />And also template substitution to construct output (based on namespaces<br />Uses XML syntax<br />
  7. 7. 12.2.1.2 The ways to use XSLT<br />You can use XSLT in a couple of especially intriguing ways:<br />To transform documents described using XML elements into HTML or XHTML for display in a Web browser.<br />In XML-based data exchange systems.<br />
  8. 8. 12.2.1.3 The ins and outs of data exchange<br />It’s entirely possible that two (or more) systems exchanging data don’t use the exact same schema or DTD.<br />The following are two solution to deal with this problem :<br />First , for one system to change its internal programming to work with the other’s DTD or schema (this approach isn’t practical)<br />Second , this approach is more practical solution, for each system to support data transformation so that each can continue to use the format it needs.<br />
  9. 9. 12.2.1.4 The role of XPath<br />Without XPath, XSLT simply wouldn’t work.<br />XPath is the mechanism XSLT uses to point to a piece of an XML document so that it can be transformed. If you can’t find it, you can’t change it<br />XPath is a very sophisticated set of rules for identifying the most specific pieces of an XML document.<br />Example :<br />If you want to do something special to every third instance of a list item, you have to be able to point to it and that’s what XPath makes possible.<br />
  10. 10. 12.2.2 XSL-FO<br />
  11. 11. 12.2.2.1 Overview<br />The names of most XML elements describe the semantic meaning of the content they contain<br />The content needs to be formatted and displayed to users<br />There must be a step where formatting information is applied to the XML document and the semantic markup is transformed into presentation markup<br />Cascading Style Sheets (CSS)<br />XSL Formatting Objects (XSL-FO)<br />
  12. 12. 12.2.2.2 What is XSL-FO ?<br />XSL-FO is a complete XML application for describing the precise layout of text on a page.<br />XSL-FO has elements that represent pages, blocks of text on the pages, graphics, horizontal rules, and more<br />XSLT + XSL-FO<br />Most of the time, you write an XSLT stylesheet that transforms your document&apos;s native markup into XSL-FO<br />There is normally a third step in which another processor transforms the XSL-FO into a third format, such as PDF and TeX<br />
  13. 13. 12.2.2.3 Categories of XSL-FO<br />The different XSL-FO formatting objects are organized into these eight categories:<br />Declaration, Pagination, and Layout<br />Block<br />Inline<br />Table<br />List<br />Links and Multi<br />Out-of-line<br />Other<br />Every formatting object has properties that can be applied to it.<br />For example, the object that creates a table (fo:table) can take properties that specify how thick its borders are, what color they are, and so on.<br />
  14. 14. 12.3 XSL Stylesheets Are XML Documents<br />XSLT stylesheet is an XML document, so:<br />The whole stylesheet should be well-formed, the following rule is invalid:<br />Like any XML document, contains XML declaration at the top<br />Its documentelement is &lt;xsl:stylesheet&gt; which contains all the template rules<br />&lt;xsl:template match=&quot;/&quot;&gt;<br /> &lt;trifle&gt;<br /> &lt;piffle&gt;<br /> &lt;flim&gt;<br /> &lt;/piffle&gt;<br /> &lt;/trifle&gt;<br /> &lt;/xsl:template&gt;<br />
  15. 15. 12.4 A Simple Transformation Using XSLT<br />An XSLT stylesheet consists of instructions that tell the computer :<br />How to convert a document described by a particular schema or DTD to a document described by a second, different schema or DTD.<br />The best way to show you how XSLT works is by example.<br />In the following section, we show a simple XSLT stylesheet that transforms books2.xml into an HTML document.<br />
  16. 16. 12.5 An XSLT Stylesheet for Converting XML to HTML<br />If we open books2.xml in Internet Explorer, the program ignores all the elements it doesn’t recognize — which is all of them — and simply displays the text within the markup, as shown in Figure 12-1.<br />
  17. 17. 12.5 An XSLT Stylesheet for Converting XML to HTML (Cont)<br />If you use the following XSLT stylesheet with books2.xml and then open it in Internet Explorer, as shown in Figure 12-2, you have a much more userfriendly and functional document for the Web<br />
  18. 18. 12.5 An XSLT Stylesheet for Converting XML to HTML (Cont)<br />&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<br />&lt;xsl:stylesheet version=&quot;2.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;&gt;<br /> &lt;xsl:template match=&quot;/&quot;&gt;<br /> &lt;html&gt;<br /> &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot; /&gt;<br /> &lt;head&gt;<br /> &lt;title&gt;Books&lt;/title&gt;<br /> &lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;books2_xslt.css&quot;/&gt;<br /> &lt;/head&gt;<br /> &lt;body&gt;<br /> &lt;xsl:for-each select=&quot;books&quot;&gt;<br /> &lt;table&gt;<br /> &lt;tr&gt;<br /> &lt;th&gt;Title&lt;/th&gt;<br /> &lt;th&gt;Author&lt;/th&gt;<br /> &lt;th&gt;Publisher&lt;/th&gt;<br /> &lt;th&gt;Price&lt;/th&gt;<br /> &lt;th&gt;ISBN&lt;/th&gt;<br /> &lt;th&gt;Content Type&lt;/th&gt;<br /> &lt;th&gt;Format&lt;/th&gt;<br /> &lt;/tr&gt;<br /> &lt;xsl:for-each select=&quot;book&quot;&gt;<br /> &lt;tr&gt;<br /> &lt;td&gt;<br /> &lt;xsl:for-each select=&quot;title&quot;&gt;<br /> &lt;xsl:apply-templates /&gt;<br /> &lt;/xsl:for-each&gt;<br /> &lt;/td&gt;<br /> &lt;td&gt;<br /> &lt;xsl:for-each select=&quot;author&quot;&gt;<br /> &lt;xsl:apply-templates /&gt;<br /> &lt;/xsl:for-each&gt;<br /> &lt;/td&gt;<br /> &lt;td&gt;<br /> &lt;xsl:for-each select=&quot;publisher&quot;&gt;<br /> &lt;xsl:apply-templates /&gt;<br /> &lt;/xsl:for-each&gt;<br /> &lt;/td&gt;<br /> &lt;td&gt;<br /> &lt;xsl:for-each select=&quot;price&quot;&gt;<br /> &lt;xsl:apply-templates /&gt;<br /> &lt;/xsl:for-each&gt;<br /> &lt;/td&gt;<br /> &lt;td&gt;<br /> &lt;xsl:for-each select=&quot;isbn&quot;&gt;<br /> &lt;xsl:apply-templates /&gt;<br /> &lt;/xsl:for-each&gt;<br /> &lt;/td&gt;<br /> &lt;td&gt;<br /> &lt;xsl:for-each select=&quot;@contentType&quot;&gt;<br /> &lt;xsl:value-of select=&quot;.&quot; /&gt;<br /> &lt;/xsl:for-each&gt;<br /> &lt;/td&gt;<br /> &lt;td&gt;<br /> &lt;xsl:for-each select=&quot;@format&quot;&gt;<br /> &lt;xsl:value-of select=&quot;.&quot; /&gt;<br /> &lt;/xsl:for-each&gt;<br /> &lt;/td&gt;<br /> &lt;/tr&gt;<br /> &lt;/xsl:for-each&gt;<br /> &lt;/table&gt;<br /> &lt;/xsl:for-each&gt;<br /> &lt;/body&gt;<br /> &lt;/html&gt;<br /> &lt;/xsl:template&gt;<br />&lt;/xsl:stylesheet&gt;<br />Listing 12-1: The books2.xslt Stylesheet<br />
  19. 19. 12.5.1 The pieces of the stylesheet puzzle <br />XSLT is a robust conversion tool and has many facets.<br />In this section, we break up an XSLT stylesheet into its component parts to see what makes it do its stuff.<br />We focus on the most basic structures that make up an XSLT stylesheet .<br />Getting all the pieces of the XSLT puzzle right means creating and using XSLT templates to transform one set of markup (vocabulary) to another.<br />
  20. 20. 12.5.1.1 Using patterns<br />Template rules identify the nodes to which they apply by using a pattern. <br />To identify the node to which the template applies, XSLT uses the match attribute with the xsl:template element to point to a specific node.<br />The value of match is called a pattern.<br />The following is structure of XSLT Document <br />This is followed by a template:<br />&lt;?xml version=”1.0” encoding=”UTF-8”?&gt;<br />&lt;xsl:stylesheet version=”2.0” xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”&gt;<br />This template matches the XML document root (/) to the HTML document root and (as spelled out in Listing 12-1) adds the following HTML markup:<br />&lt;xsl:template match=”/”&gt;<br />&lt;html&gt;<br />&lt;meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8” /&gt;<br />&lt;head&gt;<br />&lt;title&gt;Books&lt;/title&gt;<br />&lt;link rel=”stylesheet” type=”text/css” href=”books2_xslt.css”/&gt;<br />&lt;/head&gt;<br />&lt;body&gt;<br />
  21. 21. 12.5.1.1 Using patterns (Cont)<br />In our XSLT document, this xsl:template element contains the entire HTML document — but we could also use an XSLT stylesheet that uses a separate template element to match each XML element.<br />Because our XML document can include any number of book elements<br /><ul><li>Our XSLT stylesheet uses one xsl:template element that contains instructions for the transformation of repeating XML elements</li></ul>&lt;xsl:template match=”publisher”&gt;<br /> &lt;td&gt;<br /> &lt;xsl:apply templates/&gt;<br /> &lt;/td&gt;<br />&lt;/xsl:template&gt;<br />
  22. 22. 12.5.1.2 Using instructions to get results<br />In addition to using a pattern to identify which node(s) in the source document to transform, the templatespecifies how to transform each node.<br />These instructions guide the XSLT processor through content transformation.<br />We use a value-of instruction with the book attributes to tell the processor to convert the result to text and place it in an HTML table cell<br />&lt;td&gt;<br /> &lt;xsl:for-each select=”@contentType”&gt;<br /> &lt;xsl:value-of select=”.” /&gt;<br /> &lt;/xsl:for-each&gt;<br />&lt;/td&gt;<br />
  23. 23. 12.5.2 Processing element content<br />You use the xsl:apply-templates instruction when you want to return the content and text nodes of the current element and its children — but not the surrounding element tags.<br />&lt;td&gt;<br /> &lt;xsl:for-each select=”price”&gt;<br /> &lt;xsl:apply-templates /&gt;<br /> &lt;/xsl:for-each&gt;<br />&lt;/td&gt;<br />
  24. 24. 12.5.2.1 Choosing templates<br />What template gets applied here?<br />It depends; the XSLT processor looks for the best template to apply and makes a choice:<br />If a select attribute is used with apply-templates, then the processor looks for the best template for that node.<br />If no select attribute is used, the template rule that contains the apply-templates instruction is chosen as the current node.<br />So when you create XSLT style rules, you have to think about not only the way you’re transforming an individual node, but also how to deal with its content.<br />&lt;td&gt;<br /> &lt;xsl:for-each select=”price”&gt;<br /> &lt;xsl:apply-templates /&gt;<br /> &lt;/xsl:for-each&gt;<br />&lt;/td&gt;<br />In this case, the XML price element.(No select attribute is used for apply-template instruction)<br />
  25. 25. 12.5.2.2 Adding external CSS<br />As in listing 11.2, A link to an external CSS stylesheet in our XSLT markup<br />&lt;link rel=”stylesheet” type=”text/css” href=”books2_xslt.css”/&gt;<br /><ul><li> You can include style information directly in the XSLT markup if you choose, but we find it faster and more efficient to use external CSS and XSLT. As Example</li></ul>&lt;td&gt;<br /> &lt;xsl:for-each select=”author”&gt;<br /> &lt;xsl:apply-templates /&gt;<br /> &lt;/xsl:for-each&gt;<br />&lt;/td&gt;<br />&lt;td style=”background-color:#FFFF80; color:#800000;border-color:#800000;<br />border-width:1px;border-style:solid;<br />font-family:Trebuchet MS, Verdana, sans-serif; font-size:small;<br />padding:5px; text-align:left; vertical-align:text-top;”&gt;<br /> &lt;xsl:for-each select=”author”&gt;<br /> &lt;xsl:apply-templates /&gt;<br /> &lt;/xsl:for-each&gt;<br />&lt;/td&gt;<br />td {<br /> background-color: #ffff80;<br /> color: #800000;<br /> text-align: left;<br /> vertical-align: text-top;<br /> padding: 5px;<br /> …<br />}<br />
  26. 26. 12.5.2.3 Dealing with repeating elements<br />Our XSLT stylesheet (books2.xslt) deals with repeating elements by using a single template element that contains all the instructions for transforming each instance (xsl:for-each) of these elements in our XML document. The following is a another way :<br />Here the xsl:for-each element points at a specific element that is repeated — and applies the same transformation to each instance of that element.<br />&lt;xsl:template match=”books/book”&gt;<br /> &lt;ul&gt;<br /> &lt;xsl:for-each select=”publisher”&gt;<br /> &lt;li&gt;<br /> &lt;xsl:apply-templates/&gt;<br /> &lt;/li&gt;<br /> &lt;/xsl:for-each&gt;<br /> &lt;/ul&gt;<br />&lt;/xsl:template&gt;<br />In this template, every publisher element nested within &lt;books&gt;&lt;book&gt; . . . &lt;/book&gt;&lt;/books&gt; is changed to an HTML list item (li) within an unordered list (ul).<br />
  27. 27. 12.6 Creating an XSLT Stylesheet with XSLT Editors<br />If hand-coding is not your favorite art form, you can also use an XSLT editor to create your XSLT stylesheetsinstead.As example :<br />AltovaStyleVision<br />The following is steps how to use Altova Style Vision to deal with books2.xsd file. (Please download books2.xsd file from www.dummies.com/go/xmlfd4e )<br />
  28. 28. 12.6 Creating an XSLT Stylesheet with XSLT Editors (Cont)<br />Open books2.xsd (or any schema document) in StyleVision.<br />
  29. 29. 12.6 Creating an XSLT Stylesheet with XSLT Editors (Cont)<br />Select the book element from the document diagram, and drag it onto the pane on the right, onto the area labeled (contents). A menu appears, as shown in Figure 12-4.<br />
  30. 30. 12.6 Creating an XSLT Stylesheet with XSLT Editors (Cont)<br />Choose Create Table from the displayed menu.<br />
  31. 31. 12.6 Creating an XSLT Stylesheet with XSLT Editors (Cont)<br />In the Create Dynamic Table dialog box, deselect any attributes or children you don’t want to include in the table display and then click OK.<br />This action creates table cells for each of the selected attributes and child elements of the book element.<br />You can specify additional table formatting features in this dialog box:<br />Table Growth<br />Disply Cells As<br />Header / Footer<br />
  32. 32. 12.6 Creating an XSLT Stylesheet with XSLT Editors (Cont)<br />To add formatting properties, place the cursor inside the table, row, column, or cell that you want to format.<br />Select additional formatting properties for individual cells on the StyleVisioncanvas. <br />Click the XSLT-HTML tab at the bottom of the canvas on the right side to view the source code for the XSLT document.<br />Choose File➪Save Generated Files➪Save Generated XSLT-HTML File.<br />
  33. 33. 12.7 Summary<br />Picking your style of XSL<br />Transforming XML documents with XSLT<br />Using templates for style<br />Creating an XSLT stylesheet with an XSLT editor<br />

×