Extensible Stylesheet Language for Transformations
description
Transcript of Extensible Stylesheet Language for Transformations
![Page 1: Extensible Stylesheet Language for Transformations](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813afe550346895da397d5/html5/thumbnails/1.jpg)
Extensible Stylesheet Language for Transformations
XSLT
An introduction
![Page 2: Extensible Stylesheet Language for Transformations](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813afe550346895da397d5/html5/thumbnails/2.jpg)
Why ‘transformations’?
XML document
XML document
XML document
HTML document
XML document
HTML document
![Page 3: Extensible Stylesheet Language for Transformations](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813afe550346895da397d5/html5/thumbnails/3.jpg)
Why ‘transformations’?
XML document
XML document
HTML document
Corpus search results
More readable presentations
![Page 4: Extensible Stylesheet Language for Transformations](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813afe550346895da397d5/html5/thumbnails/4.jpg)
(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>
…
![Page 5: Extensible Stylesheet Language for Transformations](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813afe550346895da397d5/html5/thumbnails/5.jpg)
![Page 6: Extensible Stylesheet Language for Transformations](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813afe550346895da397d5/html5/thumbnails/6.jpg)
![Page 7: Extensible Stylesheet Language for Transformations](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813afe550346895da397d5/html5/thumbnails/7.jpg)
![Page 8: Extensible Stylesheet Language for Transformations](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813afe550346895da397d5/html5/thumbnails/8.jpg)
(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>
![Page 9: Extensible Stylesheet Language for Transformations](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813afe550346895da397d5/html5/thumbnails/9.jpg)
(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
![Page 10: Extensible Stylesheet Language for Transformations](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813afe550346895da397d5/html5/thumbnails/10.jpg)
(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
![Page 11: Extensible Stylesheet Language for Transformations](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813afe550346895da397d5/html5/thumbnails/11.jpg)
… (3) apply the XSLT file to the XML file of the play …
![Page 12: Extensible Stylesheet Language for Transformations](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813afe550346895da397d5/html5/thumbnails/12.jpg)
(4) And hey presto….
… all the TITLES from the play…
![Page 13: Extensible Stylesheet Language for Transformations](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813afe550346895da397d5/html5/thumbnails/13.jpg)
… that is not very readable though, so we can …
![Page 14: Extensible Stylesheet Language for Transformations](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813afe550346895da397d5/html5/thumbnails/14.jpg)
(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>
![Page 15: Extensible Stylesheet Language for Transformations](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813afe550346895da397d5/html5/thumbnails/15.jpg)
(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>
![Page 16: Extensible Stylesheet Language for Transformations](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813afe550346895da397d5/html5/thumbnails/16.jpg)
(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>
![Page 17: Extensible Stylesheet Language for Transformations](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813afe550346895da397d5/html5/thumbnails/17.jpg)
(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
![Page 18: Extensible Stylesheet Language for Transformations](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813afe550346895da397d5/html5/thumbnails/18.jpg)
… (6) apply the XSLT file to the XML file of the play again…
![Page 19: Extensible Stylesheet Language for Transformations](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813afe550346895da397d5/html5/thumbnails/19.jpg)
… and we
get…
![Page 20: Extensible Stylesheet Language for Transformations](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813afe550346895da397d5/html5/thumbnails/20.jpg)
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…
![Page 21: Extensible Stylesheet Language for Transformations](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813afe550346895da397d5/html5/thumbnails/21.jpg)
… (7) apply the XSLT file to the XML file of the play again …
![Page 22: Extensible Stylesheet Language for Transformations](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813afe550346895da397d5/html5/thumbnails/22.jpg)
… and we
get…
![Page 23: Extensible Stylesheet Language for Transformations](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813afe550346895da397d5/html5/thumbnails/23.jpg)
Lets make this more useful…
Task: select all the lines in the play that were spoken by some
particular character…
![Page 24: Extensible Stylesheet Language for Transformations](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813afe550346895da397d5/html5/thumbnails/24.jpg)
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
![Page 25: Extensible Stylesheet Language for Transformations](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813afe550346895da397d5/html5/thumbnails/25.jpg)
The DTD gives us the kinds of XPath expressions that we need
/PLAY
![Page 26: Extensible Stylesheet Language for Transformations](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813afe550346895da397d5/html5/thumbnails/26.jpg)
The DTD gives us the kinds of XPath expressions that we need
/PLAY /ACT
![Page 27: Extensible Stylesheet Language for Transformations](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813afe550346895da397d5/html5/thumbnails/27.jpg)
The DTD gives us the kinds of XPath expressions that we need
/PLAY /ACT /SCENE
![Page 28: Extensible Stylesheet Language for Transformations](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813afe550346895da397d5/html5/thumbnails/28.jpg)
The DTD gives us the kinds of XPath expressions that we need
/PLAY /ACT /SCENE /SPEECH
![Page 29: Extensible Stylesheet Language for Transformations](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813afe550346895da397d5/html5/thumbnails/29.jpg)
The DTD gives us the kinds of XPath expressions that we need
/PLAY /ACT /SCENE /SPEECH
![Page 30: Extensible Stylesheet Language for Transformations](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813afe550346895da397d5/html5/thumbnails/30.jpg)
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:template match="SPEECH"> <tr>
<td><xsl:value-of select="SPEAKER"/></td><td><xsl:value-of select="LINE"/></td>
</tr></xsl:template>
</xsl:stylesheet>
![Page 31: Extensible Stylesheet Language for Transformations](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813afe550346895da397d5/html5/thumbnails/31.jpg)
Result #1
![Page 32: Extensible Stylesheet Language for Transformations](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813afe550346895da397d5/html5/thumbnails/32.jpg)
![Page 33: Extensible Stylesheet Language for Transformations](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813afe550346895da397d5/html5/thumbnails/33.jpg)
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>
![Page 34: Extensible Stylesheet Language for Transformations](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813afe550346895da397d5/html5/thumbnails/34.jpg)
Result #2
![Page 35: Extensible Stylesheet Language for Transformations](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813afe550346895da397d5/html5/thumbnails/35.jpg)
![Page 36: Extensible Stylesheet Language for Transformations](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813afe550346895da397d5/html5/thumbnails/36.jpg)
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>
![Page 37: Extensible Stylesheet Language for Transformations](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813afe550346895da397d5/html5/thumbnails/37.jpg)
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>
![Page 38: Extensible Stylesheet Language for Transformations](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813afe550346895da397d5/html5/thumbnails/38.jpg)
Result #3
![Page 39: Extensible Stylesheet Language for Transformations](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813afe550346895da397d5/html5/thumbnails/39.jpg)
![Page 40: Extensible Stylesheet Language for Transformations](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813afe550346895da397d5/html5/thumbnails/40.jpg)
As final icing on the cake…
Lets replace the redundant name with the title of the scene that the
turns occur in!
![Page 41: Extensible Stylesheet Language for Transformations](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813afe550346895da397d5/html5/thumbnails/41.jpg)
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>
![Page 42: Extensible Stylesheet Language for Transformations](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813afe550346895da397d5/html5/thumbnails/42.jpg)
![Page 43: Extensible Stylesheet Language for Transformations](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813afe550346895da397d5/html5/thumbnails/43.jpg)
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
![Page 44: Extensible Stylesheet Language for Transformations](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813afe550346895da397d5/html5/thumbnails/44.jpg)
![Page 45: Extensible Stylesheet Language for Transformations](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813afe550346895da397d5/html5/thumbnails/45.jpg)
There is very little one cannot do with an XSLT for transforming
documentsAlthough it can be quite difficult
sometimes to see how!