Extensible Stylesheet Language

  • 326 views
Uploaded on

 

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
326
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
0
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

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