XSLT Md. Asfak Mahamud Associate Software Engineer KAZ Software Ltd. Dhaka, Bangladesh February 19, 2008
Agenda <ul><li>Trailer </li></ul><ul><li>Cast </li></ul><ul><ul><li>XML </li></ul></ul><ul><ul><li>XPath </li></ul></ul><u...
Agenda <ul><li>Trailer </li></ul><ul><li>Cast </li></ul><ul><ul><li>XML </li></ul></ul><ul><ul><li>XPath </li></ul></ul><u...
Trailer (continued) XSLT processor hello.xml hello.xsl hello.html
Trailer (continued) XSLT processor
Trailer (continued) Transform.java
Trailer (continued) Streamsource  of XSL File XSL File StreamSource  of XML File XML File transformer StreamResult of Html...
Trailer (continued) Streamsource  of XSL File XSL File StreamSource  of XML File XML File StreamResult of Html File Html F...
Trailer (continued) TransformerFactory   tFactory   = TransformerFactory. newInstance (); StreamSource   xslStreamSource  ...
Agenda <ul><li>Trailer </li></ul><ul><li>Cast </li></ul><ul><ul><li>XML </li></ul></ul><ul><ul><li>XPath </li></ul></ul><u...
Cast   XML <ul><li>stands for E X tensible  M arkup  L anguage. </li></ul><ul><li>was designed to  transport  and  store  ...
Cast   XML
Cast   XML
Cast   XML  with CSS
Cast   XML  with XSL
Cast <ul><li>XPath </li></ul>
Cast   XPath
Cast   XPath
Cast   XPath
Cast   XPath
Cast   XPath
Cast   XPath
Cast   XPath
Cast <ul><li>XSL/XSLT </li></ul>
Cast   XSL/XSLT
Cast   XSL/XSLT <ul><li>XSLT elements……………….. </li></ul>
Cast   XSL/XSLT
Cast   XSL/XSLT  Functions <ul><li>For the functions required for XPath 2.0, XQuery 1.0 and XSLT 2.0.  </li></ul><ul><li>h...
Cast   XSL/XSLT <ul><li>Simplest </li></ul>
Cast   XSL/XSLT <ul><li>Adding some style </li></ul>
Cast   XSL/XSLT <ul><li>use of <xsl:apply-templates> element </li></ul>
Cast   XSL/XSLT <ul><li>Getting location paths…….. </li></ul>
<ul><li>mode in <xsl:apply-templates> </li></ul>Cast   XSL/XSLT
<ul><li>If we need variable and  </li></ul><ul><li>for loop </li></ul>Cast   XSL/XSLT
<ul><li>If we need conditions </li></ul>Cast   XSL/XSLT
Cast   XSL/XSLT
<ul><li><xsl-apply-imports> works only for templates imported with xsl:import, not for templates included with xsl:include...
<ul><li>Extension Elements </li></ul><ul><li>Extension Function </li></ul><ul><li>Fallback Processing </li></ul>Cast   XSL...
Two Important Functions: Boolean element-available(string) Boolean function-available(string) Cast   XSL/XSLT
Cast   XSL/XSLT  Extention-Elements
Example: Generating multiple output file
Cast   XSL/XSLT   Extention Function <ul><li>If we want to compute a path expression on the fly </li></ul><ul><li><xsl:var...
To write a stylesheet that will work with multiple XSLT processors? <xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=“ ht...
Writing extensions in other languages Cast   XSL/XSLT <lxslt:component prefix=&quot;javascript-extension“ functions=&quot;...
Fallback Processing Cast   XSL/XSLT
Agenda <ul><li>Trailer </li></ul><ul><li>Cast </li></ul><ul><ul><li>XML </li></ul></ul><ul><ul><li>XPath </li></ul></ul><u...
Director   XSLT Processors <ul><li>Apache Xalan http://xml.apache.org/xalan </li></ul><ul><li>SAXON http://saxon.sourcefor...
Director   Java API <ul><li>JDK 1.4 contains all necessary classes </li></ul><ul><ul><li>javax.xml.transform package. </li...
Producer   Transformation
Agenda <ul><li>Trailer </li></ul><ul><li>Cast </li></ul><ul><ul><li>XML </li></ul></ul><ul><ul><li>XPath </li></ul></ul><u...
Production <ul><li>Stage setup and action !! </li></ul><ul><ul><li>Let’s see some demos………… </li></ul></ul>
Agenda <ul><li>Trailer </li></ul><ul><li>Cast </li></ul><ul><ul><li>XML </li></ul></ul><ul><ul><li>XPath </li></ul></ul><u...
Discussion <ul><li>Something from XSLT 2.0  </li></ul><ul><ul><li>Grouping </li></ul></ul><ul><ul><li>Function Definitions...
Discussion  XSLT 2.0 - Grouping <paper> <title> XML and PDF in Publishing Workflows</title> <author> Myers, Charles </auth...
Discussion  XSLT 2.0 - Grouping <ul><li>Usually Muenchian Method (keys) in XSLT 1.0 is used. </li></ul><ul><li>XSLT 2.0 ha...
<paper> <title> XML and PDF in Publishing Workflows</title> <author> Myers, Charles </author> </paper> <paper> <title> On ...
Discussion  XSLT 2.0 - Function Definitions
Discussion  XSLT 2.0 -  Multiple Result Documents <ul><li>Many XSLT 1.0 processors have extension elements to create multi...
Discussion  XSLT 2.0 -  Typing <ul><li>XPath 2.0 is strongly typed </li></ul><ul><ul><li>the type of a value determines ho...
Discussion  XSLT 2.0 -  Typing <ul><li>Declare the type of variables and parameters with  as  attribute </li></ul><ul><ul>...
<ul><li>And at last………… </li></ul>
Discussion XSLT Advantages & Disadvantages <ul><li>Advantages: </li></ul><ul><li>Easy display formatted XML data in browse...
References <ul><li>Features of XSLT 2.0 </li></ul><ul><ul><li>Jeni Tennison  http://www.jenitennison.com </li></ul></ul><u...
 
Upcoming SlideShare
Loading in …5
×

Xslt by asfak mahamud

1,437 views

Published on

Seminar Slide.

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

  • Be the first to like this

No Downloads
Views
Total views
1,437
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
41
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Xslt by asfak mahamud

  1. 1. XSLT Md. Asfak Mahamud Associate Software Engineer KAZ Software Ltd. Dhaka, Bangladesh February 19, 2008
  2. 2. Agenda <ul><li>Trailer </li></ul><ul><li>Cast </li></ul><ul><ul><li>XML </li></ul></ul><ul><ul><li>XPath </li></ul></ul><ul><ul><li>XSL/XSLT </li></ul></ul><ul><li>Director </li></ul><ul><ul><li>XSLT Processors </li></ul></ul><ul><ul><li>Java API </li></ul></ul><ul><li>Producer </li></ul><ul><ul><li>Transformation </li></ul></ul><ul><li>Production </li></ul><ul><ul><li>Html/XHtml </li></ul></ul><ul><ul><li>XML </li></ul></ul><ul><ul><li>Text </li></ul></ul><ul><li>Discussion </li></ul>It’s Wonderful Life James Stewart Donna Reed Lionel Barrymore Frank Capra Liberty Film Inc.
  3. 3. Agenda <ul><li>Trailer </li></ul><ul><li>Cast </li></ul><ul><ul><li>XML </li></ul></ul><ul><ul><li>XPath </li></ul></ul><ul><ul><li>XSL/XSLT </li></ul></ul><ul><li>Director </li></ul><ul><ul><li>XSLT Processors </li></ul></ul><ul><ul><li>Java API </li></ul></ul><ul><li>Producer </li></ul><ul><ul><li>Transformation </li></ul></ul><ul><li>Production </li></ul><ul><ul><li>Html/XHtml </li></ul></ul><ul><ul><li>XML </li></ul></ul><ul><ul><li>Text </li></ul></ul><ul><li>Discussion </li></ul>It’s Wonderful Life James Stewart Donna Reed Lionel Barrymore Frank Capra Liberty Film Inc.
  4. 4. Trailer (continued) XSLT processor hello.xml hello.xsl hello.html
  5. 5. Trailer (continued) XSLT processor
  6. 6. Trailer (continued) Transform.java
  7. 7. Trailer (continued) Streamsource of XSL File XSL File StreamSource of XML File XML File transformer StreamResult of Html File
  8. 8. Trailer (continued) Streamsource of XSL File XSL File StreamSource of XML File XML File StreamResult of Html File Html File StreamSource of XML File StreamResult of Html File
  9. 9. Trailer (continued) TransformerFactory tFactory = TransformerFactory. newInstance (); StreamSource xslStreamSource = new StreamSource(xslDoc); Transformer transformer = tFactory.newTransformer(stylesource); StreamSource xmlStreamSource = new StreamSource(new File(xmlDoc)); StreamResult htmlStreamResult = new StreamResult(new FileOutputStream(htmlDoc)); transformer.transform ( xmlStreamSource , htmlStreamResult );
  10. 10. Agenda <ul><li>Trailer </li></ul><ul><li>Cast </li></ul><ul><ul><li>XML </li></ul></ul><ul><ul><li>XPath </li></ul></ul><ul><ul><li>XSL/XSLT </li></ul></ul><ul><li>Director </li></ul><ul><ul><li>XSLT Processors </li></ul></ul><ul><ul><li>Java API </li></ul></ul><ul><li>Producer </li></ul><ul><ul><li>Transformation </li></ul></ul><ul><li>Production </li></ul><ul><ul><li>Html/XHtml </li></ul></ul><ul><ul><li>XML </li></ul></ul><ul><ul><li>Text </li></ul></ul><ul><li>Discussion </li></ul>It’s Wonderful Life James Stewart Donna Reed Lionel Barrymore Frank Capra Liberty Film Inc.
  11. 11. Cast XML <ul><li>stands for E X tensible M arkup L anguage. </li></ul><ul><li>was designed to transport and store data. </li></ul>
  12. 12. Cast XML
  13. 13. Cast XML
  14. 14. Cast XML with CSS
  15. 15. Cast XML with XSL
  16. 16. Cast <ul><li>XPath </li></ul>
  17. 17. Cast XPath
  18. 18. Cast XPath
  19. 19. Cast XPath
  20. 20. Cast XPath
  21. 21. Cast XPath
  22. 22. Cast XPath
  23. 23. Cast XPath
  24. 24. Cast <ul><li>XSL/XSLT </li></ul>
  25. 25. Cast XSL/XSLT
  26. 26. Cast XSL/XSLT <ul><li>XSLT elements……………….. </li></ul>
  27. 27. Cast XSL/XSLT
  28. 28. Cast XSL/XSLT Functions <ul><li>For the functions required for XPath 2.0, XQuery 1.0 and XSLT 2.0. </li></ul><ul><li>http://www.w3schools.com/xpath/xpath_functions.asp </li></ul>
  29. 29. Cast XSL/XSLT <ul><li>Simplest </li></ul>
  30. 30. Cast XSL/XSLT <ul><li>Adding some style </li></ul>
  31. 31. Cast XSL/XSLT <ul><li>use of <xsl:apply-templates> element </li></ul>
  32. 32. Cast XSL/XSLT <ul><li>Getting location paths…….. </li></ul>
  33. 33. <ul><li>mode in <xsl:apply-templates> </li></ul>Cast XSL/XSLT
  34. 34. <ul><li>If we need variable and </li></ul><ul><li>for loop </li></ul>Cast XSL/XSLT
  35. 35. <ul><li>If we need conditions </li></ul>Cast XSL/XSLT
  36. 36. Cast XSL/XSLT
  37. 37. <ul><li><xsl-apply-imports> works only for templates imported with xsl:import, not for templates included with xsl:include </li></ul>Cast XSL/XSLT
  38. 38. <ul><li>Extension Elements </li></ul><ul><li>Extension Function </li></ul><ul><li>Fallback Processing </li></ul>Cast XSL/XSLT
  39. 39. Two Important Functions: Boolean element-available(string) Boolean function-available(string) Cast XSL/XSLT
  40. 40. Cast XSL/XSLT Extention-Elements
  41. 41. Example: Generating multiple output file
  42. 42. Cast XSL/XSLT Extention Function <ul><li>If we want to compute a path expression on the fly </li></ul><ul><li><xsl:variable name=&quot;element&quot;>foo/bar</xsl:variable> </li></ul><ul><li>wrong: <xsl:apply-templates select=&quot;$element&quot;/> </li></ul><ul><li>saxon:evaluate() extension function: </li></ul><ul><li><xsl:apply-templates select=&quot; saxon:evaluate ($element)&quot;/> </li></ul>
  43. 43. To write a stylesheet that will work with multiple XSLT processors? <xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=“ http://www.w3.org/1999/XSL/Transform ”  xmlns:redirect =&quot; org.apache.xalan.xslt.extensions.Redirect &quot;  xmlns:saxon =&quot; http://icl.com/saxon &quot;  xmlns:xt =&quot; http://www.jclark.com/xt &quot;  extension-element-prefixes =&quot; redirect saxon xt &quot; > Cast XSL/XSLT
  44. 44. Writing extensions in other languages Cast XSL/XSLT <lxslt:component prefix=&quot;javascript-extension“ functions=&quot;cos sin toRadians&quot;> <lxslt:script lang=&quot;javascript&quot;> function cos(d) { return Math.cos(d); } function sin(d) { return Math.sin(d); } function toRadians(d) { return d * Math.PI / 180; } </lxslt:script> </lxslt:component>
  45. 45. Fallback Processing Cast XSL/XSLT
  46. 46. Agenda <ul><li>Trailer </li></ul><ul><li>Cast </li></ul><ul><ul><li>XML </li></ul></ul><ul><ul><li>XPath </li></ul></ul><ul><ul><li>XSL/XSLT </li></ul></ul><ul><li>Director </li></ul><ul><ul><li>XSLT Processors </li></ul></ul><ul><ul><li>Java API </li></ul></ul><ul><li>Producer </li></ul><ul><ul><li>Transformation </li></ul></ul><ul><li>Production </li></ul><ul><ul><li>Html/XHtml </li></ul></ul><ul><ul><li>XML </li></ul></ul><ul><ul><li>Text </li></ul></ul><ul><li>Discussion </li></ul>It’s Wonderful Life James Stewart Donna Reed Lionel Barrymore Frank Capra Liberty Film Inc.
  47. 47. Director XSLT Processors <ul><li>Apache Xalan http://xml.apache.org/xalan </li></ul><ul><li>SAXON http://saxon.sourceforge.net </li></ul><ul><li>Microsoft’s XML Parser 4.0 (MSXML) http://www.microsoft.com/xml </li></ul>
  48. 48. Director Java API <ul><li>JDK 1.4 contains all necessary classes </li></ul><ul><ul><li>javax.xml.transform package. </li></ul></ul><ul><li>Lower versions require downloading XSLT processor and SAX parser. </li></ul>
  49. 49. Producer Transformation
  50. 50. Agenda <ul><li>Trailer </li></ul><ul><li>Cast </li></ul><ul><ul><li>XML </li></ul></ul><ul><ul><li>XPath </li></ul></ul><ul><ul><li>XSL/XSLT </li></ul></ul><ul><li>Director </li></ul><ul><ul><li>XSLT Processors </li></ul></ul><ul><ul><li>Java API </li></ul></ul><ul><li>Producer </li></ul><ul><ul><li>Transformation </li></ul></ul><ul><li>Production </li></ul><ul><ul><li>Html/XHtml </li></ul></ul><ul><ul><li>XML </li></ul></ul><ul><ul><li>Text </li></ul></ul><ul><li>Discussion </li></ul>It’s Wonderful Life James Stewart Donna Reed Lionel Barrymore Frank Capra Liberty Film Inc.
  51. 51. Production <ul><li>Stage setup and action !! </li></ul><ul><ul><li>Let’s see some demos………… </li></ul></ul>
  52. 52. Agenda <ul><li>Trailer </li></ul><ul><li>Cast </li></ul><ul><ul><li>XML </li></ul></ul><ul><ul><li>XPath </li></ul></ul><ul><ul><li>XSL/XSLT </li></ul></ul><ul><li>Director </li></ul><ul><ul><li>XSLT Processors </li></ul></ul><ul><ul><li>Java API </li></ul></ul><ul><li>Producer </li></ul><ul><ul><li>Transformation </li></ul></ul><ul><li>Production </li></ul><ul><ul><li>Html/XHtml </li></ul></ul><ul><ul><li>XML </li></ul></ul><ul><ul><li>Text </li></ul></ul><ul><li>Discussion </li></ul>It’s Wonderful Life James Stewart Donna Reed Lionel Barrymore Frank Capra Liberty Film Inc.
  53. 53. Discussion <ul><li>Something from XSLT 2.0 </li></ul><ul><ul><li>Grouping </li></ul></ul><ul><ul><li>Function Definitions </li></ul></ul><ul><ul><li>Result Documents </li></ul></ul><ul><ul><li>Typing </li></ul></ul>
  54. 54. Discussion XSLT 2.0 - Grouping <paper> <title> XML and PDF in Publishing Workflows</title> <author> Myers, Charles </author> </paper> <paper> <title> On the Way to XML</title> <author> Parsons, Jonathan </author> <author> Caisley, Phil </author> </paper> <author> <name>Caisley, Phil</name> <paper> On the Way to XML </paper> </author> <author> <name>Myers, Charles</name> <paper> XML and PDF in Publishing Workflows </paper> </author> <author> <name> Parsons, Jonathan</name> <paper> On the Way to XML </paper> </author>
  55. 55. Discussion XSLT 2.0 - Grouping <ul><li>Usually Muenchian Method (keys) in XSLT 1.0 is used. </li></ul><ul><li>XSLT 2.0 has <xsl:for-each-group> </li></ul><ul><ul><li>select attribute identifies items to group </li></ul></ul><ul><ul><li>grouping by value calculates grouping key </li></ul></ul><ul><ul><ul><li>group-by groups all items </li></ul></ul></ul><ul><ul><ul><li>group-adjacent groups adjacent items </li></ul></ul></ul><ul><ul><li>grouping in sequence identifies start/end of group </li></ul></ul><ul><ul><ul><li>group-starting-with identifies start of group </li></ul></ul></ul><ul><ul><ul><li>group-ending-with identifies end of group </li></ul></ul></ul><ul><ul><li>Use current-group() to get members of current group, current-grouping-key() to get value for current group </li></ul></ul>
  56. 56. <paper> <title> XML and PDF in Publishing Workflows</title> <author> Myers, Charles </author> </paper> <paper> <title> On the Way to XML</title> <author> Parsons, Jonathan </author> <author> Caisley, Phil </author> </paper> <author> <name>Caisley, Phil</name> <paper> On the Way to XML </paper> </author> <author> <name>Myers, Charles</name> <paper> XML and PDF in Publishing Workflows </paper> </author> <author> <name> Parsons, Jonathan</name> <paper> On the Way to XML </paper> </author> <xsl:for-each-group select= &quot;paper&quot; group-by= &quot;author&quot; > <xsl:sort select=&quot;current-grouping-key()&quot; /> <author> <name> <xsl:value-of select=&quot;current-grouping-key()&quot; /> </name> <xsl:for-each select=&quot;current-group()&quot;> <paper> <xsl:value-of select=&quot;title&quot; /> </paper> </xsl:for-each> </author> </xsl:for-each-group>
  57. 57. Discussion XSLT 2.0 - Function Definitions
  58. 58. Discussion XSLT 2.0 - Multiple Result Documents <ul><li>Many XSLT 1.0 processors have extension elements to create multiple output documents </li></ul><ul><li>XSLT 2.0 has <xsl:result-document> : </li></ul><xsl:for-each select=&quot;section&quot;> <xsl:result-document href=&quot;{@id}.html&quot;> <xsl:apply-templates select=&quot;.&quot; mode=&quot;html&quot; /> </xsl:result-document> </xsl:for-each>
  59. 59. Discussion XSLT 2.0 - Typing <ul><li>XPath 2.0 is strongly typed </li></ul><ul><ul><li>the type of a value determines how it is treated by some operators (e.g. +, =) </li></ul></ul><ul><ul><li>if the wrong type of value is passed to a function, you will get an error </li></ul></ul><ul><li>Similarly, in XSLT 2.0: </li></ul><ul><ul><li>the type of a sort key determines how values are sorted </li></ul></ul><ul><ul><li>if the wrong type of value is passed as a parameter to a template, you will get an error </li></ul></ul>
  60. 60. Discussion XSLT 2.0 - Typing <ul><li>Declare the type of variables and parameters with as attribute </li></ul><ul><ul><li>holds a SequenceType </li></ul></ul><ul><ul><ul><li>item test plus occurrence indicator </li></ul></ul></ul><ul><ul><ul><li>xs:integer+ means &quot;one or more integers&quot; </li></ul></ul></ul><ul><ul><ul><li>element()? means &quot;an optional element&quot; </li></ul></ul></ul><ul><li>Error if value doesn't comply with type </li></ul><xsl:function name=&quot;math:power&quot;> <xsl:param name=&quot;base&quot; as=&quot;xs:decimal&quot; /> <xsl:param name=&quot;power&quot; as=&quot;xs:integer&quot; /> … </xsl:function>
  61. 61. <ul><li>And at last………… </li></ul>
  62. 62. Discussion XSLT Advantages & Disadvantages <ul><li>Advantages: </li></ul><ul><li>Easy display formatted XML data in browser. </li></ul><ul><li>Easier to modify when XML data format changes than to modify DOM and SAX parsing code. </li></ul><ul><li>Can be used with database queries that return XML. </li></ul><ul><li>Disadvantages: </li></ul><ul><li>Memory intensive, performance hit. </li></ul><ul><li>Difficult to implement complex business rules. </li></ul><ul><li>Have to learn new language. </li></ul>
  63. 63. References <ul><li>Features of XSLT 2.0 </li></ul><ul><ul><li>Jeni Tennison http://www.jenitennison.com </li></ul></ul><ul><li>Most of the xslt and xpath examples are from </li></ul><ul><ul><li>http://www.zvon.org </li></ul></ul><ul><li>Above All </li></ul><ul><ul><li>http://www.w3.org/Style/XSL/ </li></ul></ul><ul><ul><li>http://www.w3schools.com/xsl/default.asp </li></ul></ul><ul><ul><li>http://www.w3schools.com/xpath/default.asp </li></ul></ul>

×