Extensible Stylesheet Language Jussi Pohjolainen TAMK University of Applied Sciences
XSL <ul><li>E xtensible  S tylesheet  L anguage (XSL) </li></ul><ul><ul><li>family  of  transformation  languages </li></u...
Relationships with the Languages Extensible Stylesheet Language XSL Transformations XSL Formatting Objects XML Path Langua...
Languages <ul><li>XSL Transformations (XSLT) </li></ul><ul><ul><li>XML language for  transforming  XML documents </li></ul...
Main Idea of XSLT XSLT Processor The XSLT Processor can be client-side (browser) or server-side (php, java..)
Without XSLT - link
With XSLT - link Now the XML document is linked to an external xslt document Browser is used as an XSLT processor. (client...
XSLT <ul><li>XSLT is an  transformation language. </li></ul><ul><li>With XSLT you can make transformations: </li></ul><ul>...
XSL-FO <ul><li>XSL-FO is a W3C Specified XML language for defining visual presentation. </li></ul><ul><li>Unified presenta...
Example of an XSL-FO Document <ul><li><?xml version=&quot;1.0&quot;?> </li></ul><ul><li><fo:root xmlns:fo=&quot;http://www...
Transforming XSL-FO - document <ul><li>Once XSL-FO document is generated, it is passed to a  FO processor. </li></ul><ul><...
Example: XSL-FO and Processor XS Processor The possible output formats depends of the features of the XS processor. For ex...
Generating FO documents <ul><li>XSL-FO Documents  are not meant to be implemented by the programmer. </li></ul><ul><li>XSL...
Main Idea of XSLT: XML -> XHTML XSLT Processor The XSLT Processor can be client-side (browser) or server-side (php, java..)
Main Idea of XSLT: XML -> FO XSLT Processor Now the books.xml is transformed into XSL FO language...
Example: XSL-FO and Processor XS Processor And if you have an fo-document, you can transform it to pdf (for example)
XSL-FO Process in Whole XS Processor XSLT Processor
Example <ul><li>Converting XML to PDF using PHP and Apache FOP </li></ul><ul><li>Apache FOP </li></ul><ul><ul><li>http://x...
XSL-FO Process in Whole FOP PHP
PHP Code <ul><li><?php </li></ul><ul><li>// load xslt-file </li></ul><ul><li>$xslDoc = new DOMDocument(); </li></ul><ul><l...
XSLT AND XPATH
Relationships with the Languages Extensible Stylesheet Language XSL Transformations XSL Formatting Objects XML Path Langua...
XPath <ul><li>Before learning the basics of XSLT, let's look at XPath </li></ul><ul><ul><li>XPath is a non-XML language th...
Selecting Nodes? <ul><li><?xml version=&quot;1.0&quot;?> </li></ul><ul><li><A> </li></ul><ul><li><B> </li></ul><ul><li><C>...
Selecting Nodes (W3schools) Expression Description nodename Selects all child nodes of the named node / Selects from the r...
Examples (W3schools) Expression Description /bookstore Selects the root element bookstore bookstore/book Selects all book ...
More Examples <ul><li>/bookstore/book[1]  </li></ul><ul><li>/bookstore/book[last()] </li></ul><ul><li>/bookstore/book[last...
Example of an XSLT-transformation: XML-file <ul><li><?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?> </li></ul><...
Example of an XSLT-transformation: XSLT-file <ul><li><?xml version=&quot;1.0&quot;?> </li></ul><ul><li><xsl:stylesheet ver...
Result: Simple text – file.. <ul><li>Title: Programming with Java </li></ul>
Making the Transformation <ul><li>Implement a xml-document </li></ul><ul><li>Implement a xslt-document </li></ul><ul><li>L...
XSLT-transformation with PHP 5 <ul><li><?php </li></ul><ul><li>// Load the transformation file </li></ul><ul><li>$xslt = n...
Examples of XSLT <ul><li><?xml version=&quot;1.0&quot;?> </li></ul><ul><li><xsl:stylesheet version=&quot;1.0&quot; xmlns:x...
Examples of XSLT <ul><li><?xml version=&quot;1.0&quot;?> </li></ul><ul><li><xsl:stylesheet version=&quot;1.0&quot; xmlns:x...
Result of Previous XSLT <ul><li><html><head><title></title></head> </li></ul><ul><li><body>  </li></ul><ul><li><h1>Title: ...
Better Version <ul><li><?xml version=&quot;1.0&quot; encoding=&quot;iso-8859-1&quot;?> </li></ul><ul><li><xsl:stylesheet v...
<xsl:value-of select=&quot;XPath&quot;/> <ul><li>With <xsl:value-of> you can select an element from the source file </li><...
for-each <ul><li><?xml version=&quot;1.0&quot; encoding=&quot;iso-8859-1&quot;?> </li></ul><ul><li><xsl:stylesheet version...
sort <ul><li><?xml version=&quot;1.0&quot; encoding=&quot;iso-8859-1&quot;?> </li></ul><ul><li><xsl:stylesheet version=&qu...
Why this doesn't work? <ul><li><?xml version=&quot;1.0&quot; encoding=&quot;iso-8859-1&quot;?> </li></ul><ul><li><xsl:styl...
Using attribute <ul><li><?xml version=&quot;1.0&quot; encoding=&quot;iso-8859-1&quot;?> </li></ul><ul><li><xsl:stylesheet ...
Upcoming SlideShare
Loading in …5
×

Extensible Stylesheet Language

2,324 views

Published on

Published in: Technology
  • Great information for concerning people
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Extensible Stylesheet Language

  1. 1. Extensible Stylesheet Language Jussi Pohjolainen TAMK University of Applied Sciences
  2. 2. XSL <ul><li>E xtensible S tylesheet L anguage (XSL) </li></ul><ul><ul><li>family of transformation languages </li></ul></ul><ul><li>To format and / or transform XML – documents </li></ul><ul><li>XSL Family consists of three languages </li></ul><ul><ul><li>XSL Transformations (XSLT) </li></ul></ul><ul><ul><li>XSL Formatting Objects (XSL-FO) </li></ul></ul><ul><ul><li>XML Path Language (XPath) </li></ul></ul><ul><li>All languages are W3C recommendations </li></ul>
  3. 3. Relationships with the Languages Extensible Stylesheet Language XSL Transformations XSL Formatting Objects XML Path Language uses
  4. 4. Languages <ul><li>XSL Transformations (XSLT) </li></ul><ul><ul><li>XML language for transforming XML documents </li></ul></ul><ul><li>XSL Formatting Objects </li></ul><ul><ul><li>XML language for specifying visual formatting </li></ul></ul><ul><li>XML Path Language </li></ul><ul><ul><li>A non-XML language used by XSLT, addressing the parts of an XML document . Also available for use in non-XSLT contexts </li></ul></ul>
  5. 5. Main Idea of XSLT XSLT Processor The XSLT Processor can be client-side (browser) or server-side (php, java..)
  6. 6. Without XSLT - link
  7. 7. With XSLT - link Now the XML document is linked to an external xslt document Browser is used as an XSLT processor. (client-side)
  8. 8. XSLT <ul><li>XSLT is an transformation language. </li></ul><ul><li>With XSLT you can make transformations: </li></ul><ul><ul><li>XML -> XML in General: </li></ul></ul><ul><ul><ul><li>XML -> XHTML </li></ul></ul></ul><ul><ul><ul><li>XML -> SVG </li></ul></ul></ul><ul><ul><ul><li>... </li></ul></ul></ul><ul><ul><li>XML -> HTML </li></ul></ul><ul><ul><li>XML -> TEXT </li></ul></ul><ul><li>With XSLT you can transform a XML document to other text format (can be any). </li></ul>
  9. 9. XSL-FO <ul><li>XSL-FO is a W3C Specified XML language for defining visual presentation. </li></ul><ul><li>Unified presentation language </li></ul><ul><li>No semantic markup </li></ul>
  10. 10. Example of an XSL-FO Document <ul><li><?xml version=&quot;1.0&quot;?> </li></ul><ul><li><fo:root xmlns:fo=&quot;http://www.w3.org/1999/XSL/Format&quot;> </li></ul><ul><li><fo:layout-master-set> </li></ul><ul><li><fo:simple-page-master master-name=&quot;only&quot;> </li></ul><ul><li><fo:region-body/> </li></ul><ul><li></fo:simple-page-master> </li></ul><ul><li></fo:layout-master-set> </li></ul><ul><li><fo:page-sequence master-reference=&quot;only&quot;> </li></ul><ul><li><fo:flow flow-name=&quot;xsl-region-body&quot;> </li></ul><ul><li><fo:block font-size=&quot;20pt&quot; font-family=&quot;serif&quot; line-height=&quot;30pt&quot;> </li></ul><ul><li>Jussi Pohjolainen </li></ul><ul><li></fo:block> </li></ul><ul><li><fo:block color=&quot;red&quot; font-size=&quot;30pt&quot; font-family=&quot;serif&quot;> </li></ul><ul><li>jussi.pohjolainen(at)tamk.fi </li></ul><ul><li></fo:block> </li></ul><ul><li></fo:flow> </li></ul><ul><li></fo:page-sequence> </li></ul><ul><li></fo:root> </li></ul>
  11. 11. Transforming XSL-FO - document <ul><li>Once XSL-FO document is generated, it is passed to a FO processor. </li></ul><ul><li>FO Processor converts the document into something that is readible, printable or both. </li></ul><ul><ul><li>Most common output is PDF, PS, RTF. </li></ul></ul><ul><li>FO Processors? </li></ul><ul><ul><li>XEP, XSLFormatter, XINC and FOP </li></ul></ul>
  12. 12. Example: XSL-FO and Processor XS Processor The possible output formats depends of the features of the XS processor. For example, Apache FOP supports only PDF
  13. 13. Generating FO documents <ul><li>XSL-FO Documents are not meant to be implemented by the programmer. </li></ul><ul><li>XSL-FO Documents are generated </li></ul><ul><li>Well Who / What generates XSL FO Documents? </li></ul><ul><ul><li>It's result of an XSLT – transformation </li></ul></ul>
  14. 14. Main Idea of XSLT: XML -> XHTML XSLT Processor The XSLT Processor can be client-side (browser) or server-side (php, java..)
  15. 15. Main Idea of XSLT: XML -> FO XSLT Processor Now the books.xml is transformed into XSL FO language...
  16. 16. Example: XSL-FO and Processor XS Processor And if you have an fo-document, you can transform it to pdf (for example)
  17. 17. XSL-FO Process in Whole XS Processor XSLT Processor
  18. 18. Example <ul><li>Converting XML to PDF using PHP and Apache FOP </li></ul><ul><li>Apache FOP </li></ul><ul><ul><li>http://xmlgraphics.apache.org/fop/ </li></ul></ul><ul><li>Must be installed in your server </li></ul><ul><ul><li>It is NOT installed in TAMK Environment </li></ul></ul>
  19. 19. XSL-FO Process in Whole FOP PHP
  20. 20. PHP Code <ul><li><?php </li></ul><ul><li>// load xslt-file </li></ul><ul><li>$xslDoc = new DOMDocument(); </li></ul><ul><li>$xslDoc->load(&quot;books_to_fo.xslt&quot;); </li></ul><ul><li>// load source xml-file </li></ul><ul><li>$xmlDoc = new DOMDocument(); </li></ul><ul><li>$xmlDoc->load(&quot;books.xml&quot;); </li></ul><ul><li>// Make transformation </li></ul><ul><li>$proc = new XSLTProcessor(); </li></ul><ul><li>$proc->importStylesheet($xslDoc); </li></ul><ul><li>$booksFo = $proc->transformToXML($xmlDoc); </li></ul><ul><li>// Save the fo document to a file </li></ul><ul><li>file_put_contents(&quot;books.fo&quot;, $booksFo); </li></ul><ul><li>// Use fop to create the pdf </li></ul><ul><li>shell_exec(&quot;fop books.fo books.pdf&quot;); </li></ul><ul><li>?> </li></ul>
  21. 21. XSLT AND XPATH
  22. 22. Relationships with the Languages Extensible Stylesheet Language XSL Transformations XSL Formatting Objects XML Path Language uses
  23. 23. XPath <ul><li>Before learning the basics of XSLT, let's look at XPath </li></ul><ul><ul><li>XPath is a non-XML language that is used with XSLT. </li></ul></ul><ul><ul><li>XPath is used also in other languages, like XPointer </li></ul></ul><ul><li>XPath is a language for selecting elements, or nodes, from an XML – document </li></ul>
  24. 24. Selecting Nodes? <ul><li><?xml version=&quot;1.0&quot;?> </li></ul><ul><li><A> </li></ul><ul><li><B> </li></ul><ul><li><C></C> </li></ul><ul><li></B> </li></ul><ul><li></A> </li></ul><ul><li><!-- Let's select C --> </li></ul><ul><li>/A/B/C </li></ul>XPath
  25. 25. Selecting Nodes (W3schools) Expression Description nodename Selects all child nodes of the named node / Selects from the root node // Selects nodes in the document from the current node that match the selection no matter where they are . Selects the current node .. Selects the parent of the current node @ Selects attributes
  26. 26. Examples (W3schools) Expression Description /bookstore Selects the root element bookstore bookstore/book Selects all book elements that are children of bookstore //book Selects all book elements no matter where they are in the document bookstore//book Selects all book elements that are descendant of the bookstore element, no matter where they are under the bookstore element //@lang Selects all attributes that are named lang
  27. 27. More Examples <ul><li>/bookstore/book[1] </li></ul><ul><li>/bookstore/book[last()] </li></ul><ul><li>/bookstore/book[last()-1] </li></ul><ul><li>/bookstore/book[position()<3] </li></ul><ul><li>//title[@lang] </li></ul><ul><li>//title[@lang='eng'] </li></ul><ul><li>/bookstore/book[price>35.00] </li></ul><ul><li>/bookstore/book[price>35.00]/title </li></ul>
  28. 28. Example of an XSLT-transformation: XML-file <ul><li><?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?> </li></ul><ul><li><?xml-stylesheet type=&quot;text/xsl&quot; href=&quot;transform.xslt&quot;?> </li></ul><ul><li><book> </li></ul><ul><li><title>Programming with Java</title> </li></ul><ul><li></book> </li></ul>
  29. 29. Example of an XSLT-transformation: XSLT-file <ul><li><?xml version=&quot;1.0&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:output method=&quot; text &quot; indent=&quot;yes&quot; encoding=&quot;utf-8&quot;/> </li></ul><ul><li><xsl:template match=&quot;/&quot;> </li></ul><ul><li>Title: <xsl:value-of select=&quot; /book/title &quot;/> </li></ul><ul><li></xsl:template> </li></ul><ul><li></xsl:stylesheet> </li></ul>
  30. 30. Result: Simple text – file.. <ul><li>Title: Programming with Java </li></ul>
  31. 31. Making the Transformation <ul><li>Implement a xml-document </li></ul><ul><li>Implement a xslt-document </li></ul><ul><li>Link these two together </li></ul><ul><li>Output the result with some processor </li></ul><ul><ul><li>Browser, XML-editor, PHP, Java.. </li></ul></ul>
  32. 32. XSLT-transformation with PHP 5 <ul><li><?php </li></ul><ul><li>// Load the transformation file </li></ul><ul><li>$xslt = new domDocument(); </li></ul><ul><li>$xslt->load(&quot;transformation.xslt&quot;); </li></ul><ul><li>// Load the source xml file </li></ul><ul><li>$myXML = new DomDocument(); </li></ul><ul><li>$myXML->load(&quot;original.xml&quot;); </li></ul><ul><li>// Add the transformation file to XSLTProcessor </li></ul><ul><li>$proc = new XSLTProcessor(); </li></ul><ul><li>$proc->importStylesheet($xslt); </li></ul><ul><li>// Transform the xml and output the result </li></ul><ul><li>print $proc->transformToXml($myXML); </li></ul><ul><li>?> </li></ul>
  33. 33. Examples of XSLT <ul><li><?xml version=&quot;1.0&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:output method=&quot; text &quot; indent=&quot;yes&quot; encoding=&quot;utf-8&quot;/> </li></ul><ul><li><xsl:template match=&quot;/&quot;> </li></ul><ul><li>Title: <xsl:value-of select=&quot; /book/title &quot;/> </li></ul><ul><li></xsl:template> </li></ul><ul><li></xsl:stylesheet> </li></ul>
  34. 34. Examples of XSLT <ul><li><?xml version=&quot;1.0&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:output method=&quot; html &quot; indent=&quot;yes&quot; encoding=&quot;utf-8&quot;/> </li></ul><ul><li><xsl:template match=&quot;/&quot;> </li></ul><ul><li><html><head><title></title></head> </li></ul><ul><li><body> </li></ul><ul><li><h1>Title: <xsl:value-of select=&quot;/book/title&quot;/></h1> </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul><ul><li></xsl:template> </li></ul><ul><li></xsl:stylesheet> </li></ul>
  35. 35. Result of Previous XSLT <ul><li><html><head><title></title></head> </li></ul><ul><li><body> </li></ul><ul><li><h1>Title: Programming with Java</h1> </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul>Is this valid xhtml?
  36. 36. Better Version <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:output method=&quot;xml&quot; indent=&quot;yes&quot; encoding=&quot;utf-8&quot; </li></ul><ul><li>doctype-system=&quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot; </li></ul><ul><li>doctype-public=&quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot; /> </li></ul><ul><li><xsl:template match=&quot;/&quot;> </li></ul><ul><li><html><head><title>Example</title></head> </li></ul><ul><li><body> </li></ul><ul><li><h1>Title: <xsl:value-of select=&quot;/book/title&quot;/></h1> </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul><ul><li></xsl:template> </li></ul><ul><li></xsl:stylesheet> </li></ul>
  37. 37. <xsl:value-of select=&quot;XPath&quot;/> <ul><li>With <xsl:value-of> you can select an element from the source file </li></ul><ul><li>The value of the select attribute is XPath expression </li></ul><ul><ul><li><xsl:value-of select=&quot;books/book/name&quot;/> </li></ul></ul>
  38. 38. for-each <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:output method=&quot;xml&quot; indent=&quot;yes&quot; encoding=&quot;utf-8&quot; </li></ul><ul><li>doctype-system=&quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot; </li></ul><ul><li>doctype-public=&quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot; /> </li></ul><ul><li><xsl:template match=&quot;/&quot;> </li></ul><ul><li><html><head><title>Example</title></head> </li></ul><ul><li><body> </li></ul><ul><li><xsl:for-each select=&quot;/books/book&quot;> </li></ul><ul><li><p><xsl:value-of select=&quot;title&quot;/></p> </li></ul><ul><li></xsl:for-each> </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul><ul><li></xsl:template> </li></ul><ul><li></xsl:stylesheet> </li></ul>Repeat for every book..
  39. 39. sort <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:output method=&quot;xml&quot; indent=&quot;yes&quot; encoding=&quot;utf-8&quot; </li></ul><ul><li>doctype-system=&quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot; </li></ul><ul><li>doctype-public=&quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot; /> </li></ul><ul><li><xsl:template match=&quot;/&quot;> </li></ul><ul><li><html><head><title>Example</title></head> </li></ul><ul><li><body> </li></ul><ul><li><xsl:for-each select=&quot;/books/book&quot;> </li></ul><ul><li><xsl:sort select=&quot;price&quot; order=&quot;ascending&quot;/> </li></ul><ul><li><p><xsl:value-of select=&quot;title&quot;/></p> </li></ul><ul><li></xsl:for-each> </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul><ul><li></xsl:template> </li></ul><ul><li></xsl:stylesheet> </li></ul>Repeat for every book.. ..and sort them by price
  40. 40. Why this doesn't work? <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:output method=&quot;xml&quot; indent=&quot;yes&quot; encoding=&quot;utf-8&quot; </li></ul><ul><li>doctype-system=&quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot; </li></ul><ul><li>doctype-public=&quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot; /> </li></ul><ul><li><xsl:template match=&quot;/&quot;> </li></ul><ul><li><html><head><title>Example</title></head> </li></ul><ul><li><body> </li></ul><ul><li><img src=&quot; <xsl:value-of select=&quot;books/book/url&quot;/> &quot; /> </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul><ul><li></xsl:template> </li></ul><ul><li></xsl:stylesheet> </li></ul>What's wrong with this line?
  41. 41. Using attribute <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:output method=&quot;xml&quot; indent=&quot;yes&quot; encoding=&quot;utf-8&quot; </li></ul><ul><li>doctype-system=&quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot; </li></ul><ul><li>doctype-public=&quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot; /> </li></ul><ul><li><xsl:template match=&quot;/&quot;> </li></ul><ul><li><html><head><title>Example</title></head> </li></ul><ul><li><body> </li></ul><ul><li><img> </li></ul><ul><li><xsl:attribute name=&quot;src&quot;> </li></ul><ul><li><xsl:value-of select=&quot;books/book/url&quot;/> </li></ul><ul><li></xsl:attribute> </li></ul><ul><li></img> </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul><ul><li></xsl:template> </li></ul><ul><li></xsl:stylesheet> </li></ul>

×