450 likes | 564 Vues
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
E N D
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 document More readable presentations
(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> …
(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>
(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
(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
(4) And hey presto…. … all the TITLES from the play…
(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>
(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>
(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>
(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
… (6) apply the XSLT file to the XML file of the play again…
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…
… (7) apply the XSLT file to the XML file of the play again …
Lets make this more useful… Task: select all the lines in the play that were spoken by some particular character…
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
The DTD gives us the kinds of XPath expressions that we need /PLAY
The DTD gives us the kinds of XPath expressions that we need /ACT /PLAY
The DTD gives us the kinds of XPath expressions that we need /ACT /SCENE /PLAY
The DTD gives us the kinds of XPath expressions that we need /ACT /SCENE /SPEECH /PLAY
The DTD gives us the kinds of XPath expressions that we need /ACT /SCENE /SPEECH /PLAY
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>
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>
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>
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>
As final icing on the cake… Lets replace the redundant name with the title of the scene that the turns occur in!
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>
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
There is very little one cannot do with an XSLT for transforming documents Although it can be quite difficult sometimes to see how!