1 / 45

Extensible Stylesheet Language for Transformations

Extensible Stylesheet Language for Transformations. XSLT An introduction. Why ‘transformations’?. XML document. XML document. XML document. HTML document. XML document. HTML document. Why ‘transformations’?. XML document. Corpus search results. XML document. HTML

bayard
Download Presentation

Extensible Stylesheet Language for Transformations

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Extensible Stylesheet Language for Transformations XSLT An introduction

  2. Why ‘transformations’? XML document XML document XML document HTML document XML document HTML document

  3. Why ‘transformations’? XML document Corpus search results XML document HTML document More readable presentations

  4. (1) Take an XML file… e.g., a play…. <?xml version="1.0"?> <!DOCTYPE PLAY SYSTEM "E:\systems\xmls-1.2\tests\large\play.dtd"> <PLAY> <TITLE>The Two Gentlemen of Verona</TITLE> <FM> <P>Text placed in the public domain by Moby Lexical Tools, 1992.</P> <P>SGML markup by Jon Bosak, 1992-1994.</P> <P>XML version by Jon Bosak, 1996-1998.</P> <P>This work may be freely copied and distributed worldwide.</P> </FM> <PERSONAE> <TITLE>Dramatis Personae</TITLE> <PERSONA>DUKE OF MILAN, Father to Silvia. </PERSONA> <PGROUP> <PERSONA>VALENTINE</PERSONA> <PERSONA>PROTEUS</PERSONA> <GRPDESCR>the two Gentlemen.</GRPDESCR> </PGROUP> …

  5. (2) Take an XSLT file… <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl=http://www.w3.org/1999/XSL/Transform> <xsl:template match="/"> <xsl:apply-templates select="//TITLE"/> </xsl:template> <xsl:template match="TITLE"> <xsl:value-of select="."/> </xsl:template> </xsl:stylesheet>

  6. (2) Take an XSLT file… <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl=http://www.w3.org/1999/XSL/Transform> <xsl:template match="/"> <xsl:apply-templates select="//TITLE"/> </xsl:template> <xsl:template match="TITLE"> <xsl:value-of select="."/> </xsl:template> </xsl:stylesheet> From the top of an XML file(“/”), go looking for any place where we can find a TITLE element (“//TITLE”). XPath

  7. (2) Take an XSLT file… <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl=http://www.w3.org/1999/XSL/Transform> <xsl:template match="/"> <xsl:apply-templates select="//TITLE"/> </xsl:template> <xsl:template match="TITLE"> <xsl:value-of select="."/> </xsl:template> </xsl:stylesheet> Whenever we find a TITLE element (“TITLE”), take the value of that ‘node’ (“.”) and put it in our output document

  8. … (3) apply the XSLT file to the XML file of the play …

  9. (4) And hey presto…. … all the TITLES from the play…

  10. … that is not very readable though, so we can …

  11. (5) … add some HTML <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <xsl:apply-templates select="//TITLE"/> </xsl:template> <xsl:template match="TITLE"> <xsl:value-of select="."/> </xsl:template> </xsl:stylesheet>

  12. (5) … add some HTML <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <xsl:apply-templates select="//TITLE"/> </xsl:template> <xsl:template match="TITLE"> <xsl:value-of select="."/> </xsl:template> </xsl:stylesheet>

  13. (5) … add some HTML <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <head> <title>Search Results</title> </head> <body> <xsl:apply-templates select="//TITLE"/> </body> </html> </xsl:template> <xsl:template match="TITLE"> <p/><xsl:value-of select="."/> </xsl:template> </xsl:stylesheet>

  14. (5) … add some HTML <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <head> <title>Search Results</title> </head> <body> <xsl:apply-templates select="//TITLE"/> </body> </html> </xsl:template> <xsl:template match="TITLE"> <p/><xsl:value-of select="."/> </xsl:template> </xsl:stylesheet> HTML HTML HTML

  15. … (6) apply the XSLT file to the XML file of the play again…

  16. … and we get…

  17. We can also start making it fancier… <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <head> <TITLE>Search Results</TITLE> </head> <body> <h2>Our search results</h2> <table border="1"> <xsl:apply-templates select="//TITLE"/> </table> </body> </html> </xsl:template> <xsl:template match="TITLE"> <tr> <td><xsl:value-of select="."/> </td> </tr> </xsl:template> </xsl:stylesheet> This time we wrap the HTML code for a table around our XSLT expressions…

  18. … (7) apply the XSLT file to the XML file of the play again …

  19. … and we get…

  20. Lets make this more useful… Task: select all the lines in the play that were spoken by some particular character…

  21. How do we do this? • Look in the XML file to see how speaker turns are represented • Write a proper XPath expression to pick out just the turns that we are interested in • Put that XPath expression in place of our “TITLE” XPath in the example XSLT file • Apply the XSLT file to the play again

  22. The DTD gives us the kinds of XPath expressions that we need /PLAY

  23. The DTD gives us the kinds of XPath expressions that we need /ACT /PLAY

  24. The DTD gives us the kinds of XPath expressions that we need /ACT /SCENE /PLAY

  25. The DTD gives us the kinds of XPath expressions that we need /ACT /SCENE /SPEECH /PLAY

  26. The DTD gives us the kinds of XPath expressions that we need /ACT /SCENE /SPEECH /PLAY

  27. Try #1 <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <head> <TITLE>Search Results</TITLE> </head> <body> <h2>Our search results</h2> <table border="1"> <xsl:apply-templates select="/PLAY/ACT/SCENE/SPEECH"/> </table> </body> </html> </xsl:template> <xsl:templatematch="SPEECH"> <tr> <td><xsl:value-of select="SPEAKER"/></td> <td><xsl:value-of select="LINE"/></td> </tr> </xsl:template> </xsl:stylesheet>

  28. Result #1

  29. Try #2 <xsl:template match="SPEECH"> <tr> <td><xsl:value-of select="SPEAKER"/></td> <td> <xsl:for-each select="LINE"> <xsl:value-of select="."/><br/> </xsl:for-each> </td> </tr> </xsl:template>

  30. Result #2

  31. A variant of Try #2 <xsl:template match="SPEECH"> <tr> <td><xsl:value-of select="SPEAKER"/></td> <td><xsl:apply-templates select="LINE"/></td> </tr> </xsl:template> <xsl:template match="LINE"> <xsl:value-of select="."/><br/> </xsl:template>

  32. Try #3 <xsl:template match="SPEAKER"> <xsl:choose> <xsl:when test="contains(text(),'SPEED')"> <tr> <td><xsl:value-of select="../SPEAKER"/></td> <td><xsl:apply-templates select="../LINE"/></td> </tr> </xsl:when> </xsl:choose> </xsl:template>

  33. Result #3

  34. As final icing on the cake… Lets replace the redundant name with the title of the scene that the turns occur in!

  35. A solution… <xsl:template match="SPEAKER"> <xsl:choose> <xsl:when test="contains(text(),'SPEED')"> <tr> <td><xsl:value-of select="../../TITLE"/></td> <td><xsl:apply-templates select="../LINE"/></td> </tr> </xsl:when> </xsl:choose> </xsl:template>

  36. And one more complex example still… The turns of SPEED plus the preceding turn to show the dialogue context… The stylesheet is on the course website

  37. There is very little one cannot do with an XSLT for transforming documents Although it can be quite difficult sometimes to see how!

More Related