C:\fakepath\xsl final


Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

C:\fakepath\xsl final

  1. 1. E X tensible S tylesheet L anguage Sep 3, 2010
  2. 2. What is XSL? <ul><li>XSL stands for E x tensible S tylesheet L anguage </li></ul><ul><li>CSS was designed for styling HTML pages, and can be used to style XML pages </li></ul><ul><li>XSL was designed specifically to style XML pages, and is much more sophisticated than CSS </li></ul><ul><li>XSL consists of three languages: </li></ul><ul><ul><li>XSLT ( XSL T ransformations ) is a language used to transform XML documents into other kinds of documents (most commonly HTML, so they can be displayed) </li></ul></ul><ul><ul><li>XPath is a language to select parts of an XML document to transform with XSLT </li></ul></ul><ul><ul><li>XSL-FO ( XSL F ormatting O bjects ) is a replacement for CSS </li></ul></ul><ul><ul><ul><li>There are no current implementations of XSL-FO, and we won’t cover it </li></ul></ul></ul>
  3. 3. How does it work? <ul><li>The XML source document is parsed into an XML source tree </li></ul><ul><li>You use XPath to define templates that match parts of the source tree </li></ul><ul><li>You use XSLT to transform the matched part and put the transformed information into the result tree </li></ul><ul><li>The result tree is output as a result document </li></ul><ul><li>Parts of the source document that are not matched by a template are typically copied unchanged </li></ul>
  4. 4. Simple XPath <ul><li>Here’s a simple XML document: </li></ul><ul><li><?xml version=&quot;1.0&quot;?> <library> <book> <title>XML</title> <author>Gregory Brill</author> </book> <book> <title>Java and XML</title> <author>Brett McLaughlin</author> </book> </library > </li></ul><ul><li>XPath expressions look a lot like paths in a computer file system </li></ul><ul><ul><li>/ means the document itself (but no specific elements) </li></ul></ul><ul><ul><li>/library selects the root element </li></ul></ul><ul><ul><li>/library/book selects every book element </li></ul></ul><ul><ul><li>//author selects every author element, wherever it occurs </li></ul></ul>
  5. 5. Simple XSLT <ul><li><xsl:for-each select=&quot;//book&quot;> loops through every book element, everywhere in the document </li></ul><ul><li><xsl:value-of select=&quot;title&quot;/> chooses the content of the title element at the current location </li></ul><ul><li><xsl:for-each select=&quot;//book&quot;> <xsl:value-of select=&quot;title&quot;/> </xsl:for-each> chooses the content of the title element for each book in the XML document </li></ul>
  6. 6. Using XSL to create HTML <ul><li>Our goal is to turn this: </li></ul><ul><li><?xml version=&quot;1.0&quot;?> <library> <book> <title>XML</title> <author>Gregory Brill</author> </book> <book> <title>Java and XML</title> <author>Brett McLaughlin</author> </book> </library > </li></ul><ul><li>Into HTML that displays something like this: </li></ul><ul><li>Book Titles: • XML • Java and XML Book Authors: • Gregory Brill • Brett McLaughlin </li></ul><ul><li>Note that we’ve grouped titles and authors separately </li></ul>
  7. 7. What we need to do <ul><li>We need to save our XML into a file (let’s call it books.xml ) </li></ul><ul><li>We need to create a file (say, books.xsl ) that describes how to select elements from books.xml and embed them into an HTML page </li></ul><ul><ul><li>We do this by intermixing the HTML and the XSL in the books.xsl file </li></ul></ul><ul><li>We need to add a line to our books.xml file to tell it to refer to books.xsl for formatting information </li></ul>
  8. 8. books.xml , revised <ul><li><?xml version=&quot;1.0&quot;?> <?xml-stylesheet type=&quot;text/xsl&quot; href=&quot;books.xsl&quot;?> <library> <book> <title>XML</title> <author>Gregory Brill</author> </book> <book> <title>Java and XML</title> <author>Brett McLaughlin</author> </book> </library > </li></ul>This tells you where to find the XSL file
  9. 9. Desired HTML <ul><li><html> <head> <title>Book Titles and Authors</title> </head> <body> <h2>Book titles:</h2> <ul> <li> XML </li> <li> Java and XML </li> </ul> <h2>Book authors:</h2> <ul> <li> Gregory Brill </li> <li> Brett McLaughlin </li> </ul> </body> </html> </li></ul>Blue text is data extracted from the XML document Brown text is our HTML template We don’t necessarily know how much data we will have
  10. 10. XSL outline <ul><li><?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?> </li></ul><ul><li><xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;> </li></ul><ul><li><xsl:template match=&quot;/&quot;> </li></ul><ul><li><html> ... </html> </li></ul><ul><li></xsl:template> </li></ul><ul><li></xsl:stylesheet> </li></ul>
  11. 11. XSL Architecture XSL Processor XML Source XSL Stylesheet HTML Output
  12. 12. XSL Elements <ul><ul><li><xsl:Value-of> </li></ul></ul><ul><ul><li><xsl:if> </li></ul></ul><ul><ul><li><xsl:choose> </li></ul></ul><ul><ul><li><xsl:copy> </li></ul></ul><ul><ul><li><xsl:comment> </li></ul></ul><ul><ul><li><xsl:copy-of> </li></ul></ul><ul><ul><li><xsl:element> </li></ul></ul><ul><ul><li><xsl:apply-import> </li></ul></ul><ul><ul><li><xsl:fallback> </li></ul></ul><ul><ul><li><xsl:attribute> </li></ul></ul><ul><ul><li><xsl:call-tempalate> </li></ul></ul><ul><ul><li><xsl:for-each> </li></ul></ul><ul><ul><li><xsl:sort> </li></ul></ul><ul><ul><li><xsl:message> </li></ul></ul><ul><ul><li><xsl:number> </li></ul></ul><ul><ul><li><xsl:text> </li></ul></ul><ul><ul><li><xsl:param> </li></ul></ul><ul><ul><li><xsl:variable> </li></ul></ul>
  13. 13. <xsl:value-of> Element <ul><li>The XSL element <xsl:value-of> can be used to extract the value of an element that is selected from the source XML document </li></ul><ul><li>The extracted value is added to the output stream </li></ul><ul><li>The selected element is located by an XPath expression that appears as the value of the select attribute </li></ul><xsl:value-of select =“xpath-expression”/>
  14. 14. Output Of <xsl:value-of> Selected values
  15. 15. <xsl:for-each> Element <ul><li>The <xsl:for-each> element loops over all the nodes in the nodelist of the XPath expression that appears as the value of the select attribute </li></ul><ul><li>The value of each node can be extracted by an <xsl:value-of> element </li></ul><xsl:for-each select =“xpath-expression”/>
  16. 16. Output of <xsl:for-each> All the values are selected
  17. 17. <xsl:sort> Element <ul><li>The <xsl:sort> element is used to sort the list of nodes that are looped over by the <xsl:for-each> element </li></ul><ul><li>Thus, the <xsl:sort> must appear inside the <xsl:for-each> element </li></ul><ul><li>The looping is done in sorted order </li></ul><xsl:sort select =“xpath-expression”/>
  18. 18. <xsl:if> Element <ul><li>The <xsl:if> element is used for conditional processing </li></ul><ul><li>The condition appears as the value of the test attribute, for example: </li></ul><ul><li>< xsl:if test=&quot; price &gt; 10 &quot;>   some output ... </ xsl:if > </li></ul><ul><li>The elements inside the <xsl:if> element are processed if the condition is true </li></ul>
  19. 19. <xsl:choose> Element <ul><li>The <xsl:choose> element is used in conjunction with <xsl:when> and <xsl:otherwise> to express test with multiple conditions </li></ul><ul><li>There can be many <xsl:when> inside an <xsl:choose> element, but there should be a single <xsl:otherwise> inside an <xsl:choose> element </li></ul>
  20. 20. Using <xsl:choose> <ul><li>To insert a conditional choose against the content of the XML file, simply add the <xsl:choose> , <xsl:when> , and <xsl:otherwise> elements to your XSL document like this: </li></ul><ul><li>< xsl:choose >    < xsl:when test=&quot; price &gt; 10 &quot;>       ... some code ...    </ xsl:when >    < xsl:otherwise >       ... some code ....    </ xsl:otherwise > </ xsl:choose > </li></ul>
  21. 21. How to use it <ul><li>In a modern browser, such as Netscape 6, Internet Explorer 6, or Mozilla 1.0, you can just open the XML file </li></ul><ul><ul><li>Older browsers will ignore the XSL and just show you the XML contents as continuous text </li></ul></ul><ul><li>You can use a program such as Xalan, MSXML, or Saxon to create the HTML as a file </li></ul><ul><ul><li>This can be done on the server side, so that all the client side browser sees is plain HTML </li></ul></ul><ul><ul><li>The server can create the HTML dynamically from the information currently in XML </li></ul></ul>
  22. 22. XSL Disadvantages <ul><li>Confusing syntax and semantics </li></ul><ul><ul><li>Like Prolog+C+XML </li></ul></ul><ul><ul><li>It's really a programming language, but using markup language </li></ul></ul><ul><li>Hard to debug </li></ul><ul><ul><li>XSL Trace helps a little </li></ul></ul><ul><li>Don't have full power of, say, Java inside templates </li></ul><ul><ul><li>No database access, hashtables, methods, objects, etc. </li></ul></ul><ul><li>Still need separate .xsl file for each client device </li></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.