extensible stylesheet language for transformations

45
Extensible Stylesheet Language for Transformations XSLT An introduction

Upload: bayard

Post on 05-Jan-2016

31 views

Category:

Documents


0 download

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Extensible Stylesheet Language for Transformations

Extensible Stylesheet Language for Transformations

XSLT

An introduction

Page 2: Extensible Stylesheet Language for Transformations

Why ‘transformations’?

XML document

XML document

XML document

HTML document

XML document

HTML document

Page 3: Extensible Stylesheet Language for Transformations

Why ‘transformations’?

XML document

XML document

HTML document

Corpus search results

More readable presentations

Page 4: Extensible Stylesheet Language for Transformations

(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
Page 6: Extensible Stylesheet Language for Transformations
Page 7: Extensible Stylesheet Language for Transformations
Page 8: Extensible Stylesheet Language for Transformations

(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

(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

(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

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

Page 12: Extensible Stylesheet Language for Transformations

(4) And hey presto….

… all the TITLES from the play…

Page 13: Extensible Stylesheet Language for Transformations

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

Page 14: Extensible Stylesheet Language for Transformations

(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

(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

(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

(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

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

Page 19: Extensible Stylesheet Language for Transformations

… and we

get…

Page 20: Extensible Stylesheet Language for Transformations

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

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

Page 22: Extensible Stylesheet Language for Transformations

… and we

get…

Page 23: Extensible Stylesheet Language for Transformations

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

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

The DTD gives us the kinds of XPath expressions that we need

/PLAY

Page 26: Extensible Stylesheet Language for Transformations

The DTD gives us the kinds of XPath expressions that we need

/PLAY /ACT

Page 27: Extensible Stylesheet Language for Transformations

The DTD gives us the kinds of XPath expressions that we need

/PLAY /ACT /SCENE

Page 28: Extensible Stylesheet Language for Transformations

The DTD gives us the kinds of XPath expressions that we need

/PLAY /ACT /SCENE /SPEECH

Page 29: Extensible Stylesheet Language for Transformations

The DTD gives us the kinds of XPath expressions that we need

/PLAY /ACT /SCENE /SPEECH

Page 30: Extensible Stylesheet Language for Transformations

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

Result #1

Page 32: Extensible Stylesheet Language for Transformations
Page 33: Extensible Stylesheet Language for Transformations

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

Result #2

Page 35: Extensible Stylesheet Language for Transformations
Page 36: Extensible Stylesheet Language for Transformations

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

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

Result #3

Page 39: Extensible Stylesheet Language for Transformations
Page 40: Extensible Stylesheet Language for Transformations

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

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
Page 43: Extensible Stylesheet Language for Transformations

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
Page 45: Extensible Stylesheet Language for Transformations

There is very little one cannot do with an XSLT for transforming

documentsAlthough it can be quite difficult

sometimes to see how!