Title: Extensible Stylesheet Language for Transformations
1Extensible Stylesheet Language for Transformations
2Why transformations?
XML document
XML document
XML document
HTML document
XML document
HTML document
3Why transformations?
XML document
Corpus search results
XML document
HTML document
More readable presentations
4(1) Take an XML file
e.g., a play.
lt?xml version"1.0"?gt lt!DOCTYPE PLAY SYSTEM
"E\systems\xmls-1.2\tests\large\play.dtd"gt ltPLAYgt
ltTITLEgtThe Two Gentlemen of Veronalt/TITLEgt ltFMgt
ltPgtText placed in the public domain by Moby
Lexical Tools, 1992.lt/Pgt ltPgtSGML markup by Jon
Bosak, 1992-1994.lt/Pgt ltPgtXML version by Jon
Bosak, 1996-1998.lt/Pgt ltPgtThis work may be
freely copied and distributed worldwide.lt/Pgt lt/FM
gt ltPERSONAEgt ltTITLEgtDramatis
Personaelt/TITLEgt ltPERSONAgtDUKE OF MILAN, Father
to Silvia. lt/PERSONAgt ltPGROUPgt ltPERSONAgtVALEN
TINElt/PERSONAgt ltPERSONAgtPROTEUSlt/PERSONAgt ltG
RPDESCRgtthe two Gentlemen.lt/GRPDESCRgt lt/PGROUPgt
5(No Transcript)
6(No Transcript)
7(No Transcript)
8(2) Take an XSLT file
lt?xml version"1.0" encoding"UTF-8"?gt ltxslstyles
heet version"1.0"
xmlnsxslhttp//www.w3.org/1999/XSL/Transformgt lt
xsltemplate match"/"gt ltxslapply-templates
select"//TITLE"/gt lt/xsltemplategt ltxsltemplate
match"TITLE"gt ltxslvalue-of select"."/gt
lt/xsltemplategt lt/xslstylesheetgt
9(2) Take an XSLT file
lt?xml version"1.0" encoding"UTF-8"?gt ltxslstyles
heet version"1.0"
xmlnsxslhttp//www.w3.org/1999/XSL/Transformgt lt
xsltemplate match"/"gt ltxslapply-templates
select"//TITLE"/gt lt/xsltemplategt ltxsltemplate
match"TITLE"gt ltxslvalue-of select"."/gt
lt/xsltemplategt lt/xslstylesheetgt
From the top of an XML file(/), go looking for
any place where we can find a TITLE element
(//TITLE).
XPath
10(2) Take an XSLT file
lt?xml version"1.0" encoding"UTF-8"?gt ltxslstyles
heet version"1.0"
xmlnsxslhttp//www.w3.org/1999/XSL/Transformgt lt
xsltemplate match"/"gt ltxslapply-templates
select"//TITLE"/gt lt/xsltemplategt ltxsltemplate
match"TITLE"gt ltxslvalue-of select"."/gt
lt/xsltemplategt lt/xslstylesheetgt
Whenever we find a TITLE element (TITLE), take
the value of that node (.) and put it in our
output document
11 (3) apply the XSLT file to the XML file of the
play
12(4) And hey presto.
all the TITLES from the play
13 that is not very readable though, so we can
14(5) add some HTML
lt?xml version"1.0" encoding"UTF-8"?gt ltxslstyles
heet version"1.0" xmlnsxsl"http//www.w3.org/19
99/XSL/Transform"gt ltxsltemplate match"/"gt
ltxslapply-templates select"//TITLE"/gt lt/xsltemp
lategt ltxsltemplate match"TITLE"gt
ltxslvalue-of select"."/gt lt/xsltemplategt lt/xsl
stylesheetgt
15(5) add some HTML
lt?xml version"1.0" encoding"UTF-8"?gt ltxslstyles
heet version"1.0" xmlnsxsl"http//www.w3.org/19
99/XSL/Transform"gt ltxsltemplate match"/"gt
ltxslapply-templates
select"//TITLE"/gt lt/xsltemplategt ltxsltem
plate match"TITLE"gt ltxslvalue-of
select"."/gt lt/xsltemplategt lt/xslstylesheetgt
16(5) add some HTML
lt?xml version"1.0" encoding"UTF-8"?gt ltxslstyles
heet version"1.0" xmlnsxsl"http//www.w3.org/19
99/XSL/Transform"gt ltxsltemplate match"/"gt
lthtmlgt ltheadgt lttitlegtSearch
Resultslt/titlegt lt/headgt ltbodygt
ltxslapply-templates select"//TITLE"/gt
lt/bodygt lt/htmlgt lt/xsltemplategt ltxsltemplate
match"TITLE"gt ltp/gt ltxslvalue-of
select"."/gt lt/xsltemplategt lt/xslstylesheetgt
17(5) add some HTML
lt?xml version"1.0" encoding"UTF-8"?gt ltxslstyles
heet version"1.0" xmlnsxsl"http//www.w3.org/19
99/XSL/Transform"gt ltxsltemplate match"/"gt
lthtmlgt ltheadgt lttitlegtSearch
Resultslt/titlegt lt/headgt ltbodygt
ltxslapply-templates select"//TITLE"/gt
lt/bodygt lt/htmlgt lt/xsltemplategt ltxsltemplate
match"TITLE"gt ltp/gt ltxslvalue-of
select"."/gt lt/xsltemplategt lt/xslstylesheetgt
HTML
HTML
HTML
18 (6) apply the XSLT file to the XML file of the
play again
19 and we get
20We can also start making it fancier
lt?xml version"1.0" encoding"UTF-8"?gt ltxslstyles
heet version"1.0" xmlnsxsl"http//www.w3.org/19
99/XSL/Transform"gt ltxsltemplate match"/"gt
lthtmlgt ltheadgt ltTITLEgtSearch
Resultslt/TITLEgt lt/headgt ltbodygt
lth2gtOur search resultslt/h2gt lttable
border"1"gt ltxslapply-templates
select"//TITLE"/gt lt/tablegt
lt/bodygt lt/htmlgt lt/xsltemplategt ltxsltemplate
match"TITLE"gt lttrgt lttdgtltxslvalue-of
select"."/gt lt/tdgt lt/trgt lt/xsltemplategt lt/xsls
tylesheetgt
This time we wrap the HTML code for a table
around our XSLT expressions
21 (7) apply the XSLT file to the XML file of the
play again
22 and we get
23Lets make this more useful
- Task select all the lines in the play that were
spoken by some particular character
24How 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
25The DTD gives us the kinds of XPath expressions
that we need
/PLAY
26The DTD gives us the kinds of XPath expressions
that we need
/ACT
/PLAY
27The DTD gives us the kinds of XPath expressions
that we need
/ACT
/SCENE
/PLAY
28The DTD gives us the kinds of XPath expressions
that we need
/ACT
/SCENE
/SPEECH
/PLAY
29The DTD gives us the kinds of XPath expressions
that we need
/ACT
/SCENE
/SPEECH
/PLAY
30Try 1
lt?xml version"1.0" encoding"UTF-8"?gt ltxslstyles
heet version"1.0" xmlnsxsl"http//www.w3.org/19
99/XSL/Transform"gt ltxsltemplate match"/"gt
lthtmlgt ltheadgt ltTITLEgtSearch
Resultslt/TITLEgt lt/headgt ltbodygt lth2gtOur
search resultslt/h2gt lttable border"1"gt
ltxslapply-templates select"/PLAY/ACT/SCENE/SPEEC
H"/gt lt/tablegt lt/bodygt
lt/htmlgt lt/xsltemplategt ltxsltemplate
match"SPEECH"gt lttrgt lttdgtltxslvalue-of
select"SPEAKER"/gtlt/tdgt lttdgtltxslvalue-of
select"LINE"/gtlt/tdgt lt/trgt lt/xsltemplategt lt/xs
lstylesheetgt
31Result 1
32(No Transcript)
33Try 2
ltxsltemplate match"SPEECH"gt lttrgt
lttdgtltxslvalue-of select"SPEAKER"/gtlt/tdgt
lttdgt ltxslfor-each select"LINE"gt
ltxslvalue-of select"."/gtltbr/gt
lt/xslfor-eachgt lt/tdgt
lt/trgt lt/xsltemplategt
34Result 2
35(No Transcript)
36A variant of Try 2
ltxsltemplate match"SPEECH"gt
lttrgt lttdgtltxslvalue-of select"SPEAKER"/gtlt/tdgt lt
tdgtltxslapply-templates select"LINE"/gtlt/tdgt
lt/trgt lt/xsltemplategt ltxsltemplate
match"LINE"gt ltxslvalue-of select"."/gtltbr/gt lt
/xsltemplategt
37Try 3
ltxsltemplate match"SPEAKER"gt ltxslchoosegt
ltxslwhen test"contains(text(),'SPEED')"gt
lttrgt lttdgtltxslvalue-of select"../SPEAKER"
/gtlt/tdgt lttdgtltxslapply-templates
select"../LINE"/gtlt/tdgt lt/trgt
lt/xslwhengt lt/xslchoosegt lt/xsltemplategt
38Result 3
39(No Transcript)
40As final icing on the cake
- Lets replace the redundant name with the title of
the scene that the turns occur in!
41A solution
ltxsltemplate match"SPEAKER"gt ltxslchoosegt
ltxslwhen test"contains(text(),'SPEED')"gt
lttrgt lttdgtltxslvalue-of select"../../TITLE
"/gtlt/tdgt lttdgtltxslapply-templates
select"../LINE"/gtlt/tdgt lt/trgt
lt/xslwhengt lt/xslchoosegt lt/xsltemplategt
42(No Transcript)
43And 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
44(No Transcript)
45There is very little one cannot do with an XSLT
for transforming documents
- Although it can be quite difficult sometimes to
see how!