Lecture 6-XSL and XPath Continued
Lecture 6-XSL and XPath Continued
1 2
DWAX 2010.1 DWAX 2010.1
1
XSLT Loops cont.. XSLT Loops cont..
• <xsl:for-each> Filtering information
• <xsl:for-each> Filtering information – result
<xsl:template match="/"> • Filter the output
from an XML file by
<html><body>
adding a predicate to
<h2>Course Information</h2> the node set created
<table border="1"> by the select
attribute in the
<tr bgcolor="yellow"> <th>Unit</th> <th>Lecturer</th> <xsl:for-each>
</tr> element
5 6
DWAX 2010.1 DWAX 2010.1
7 8
DWAX 2010.1 DWAX 2010.1
2
XSLT sorting XSLT Sorting cont…
– xsl:sort example
<xsl:template match="/">
<xsl:sort> example - result
<html><body> <h2>Course Information</h2> • The <xsl:sort>
element is used to
<table border="1"> <tr bgcolor="yellow">
sort the output
<th>Code</th><th>Unit</th> <th>Lecturer</th> </tr>
<xsl:for-each select="course/unit">
•Note: <xsl:sort> is
<xsl:sort select=“code" order="descending"/>
always within
<tr> <xsl:for-each> or
<td><xsl:value-of select="code"/></td> <xsl:apply-
<td><xsl:value-of select="name"/></td>
templates>
<td><xsl:value-of select="lecturer"/></td>
</tr>
</xsl:for-each>
</table>
</body></html>
</xsl:template>
9 10
DWAX 2010.1 DWAX 2010.1
3
Looping and conditional EXAMPLE:
XSL conditionals cont <xsl:template match="/">
<html>
<body>
xsl:choose – example on later slides, also see: <h2>My CD Collection</h2>
http://www.w3schools.com/xsl/xsl_choose.asp <p>Titles:
<xsl:choose> <xsl:for-each select="catalog/cd">
<xsl:value-of select="title"/>
<xsl:when test="expression"> <xsl:if test="position() < last()-1">
<xsl:text>, </xsl:text>
... some output ... </xsl:if>
<xsl:if test="position()=last()-1">
</xsl:when> <xsl:text>, and </xsl:text>
</xsl:if>
<xsl:otherwise> <xsl:if test="position()=last()">
<xsl:text>!</xsl:text>
</xsl:if> An example including looping
... some output .... structure, literal output using
</xsl:for-each>
xsl:text and conditional
</xsl:otherwise> </p>
statements. Each are
</body> discussed/explained on other
</xsl:choose> </html> slides.
13 </xsl:template> 14
DWAX 2010.1 DWAX 2010.1
XSLT Numbering
Syntax: <?xml version="1.0" encoding="ISO-8859-1"?>
The <xsl:number> element is used to <xsl:number count="expression"
level="single|multiple|any" <xsl:stylesheet version="1.0"
determine the integer position of the current from="expression"
node in the source. It is also used to format a value="expression"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/">
number. format="formatstring" <html>
lang="languagecode“
letter-value="alphabetic|traditional" <body>
grouping-separator="character"
grouping-size="number"/> <p>
<xsl:for-each select="catalog/cd">
<xsl:template match="course/unit">
<xsl:number value="position()" format="1" />
<html><body>
<xsl:value-of select="title" /><br />
<xsl:number/>
xsl:number/>
</xsl:for-each>
<xsl:text> ....</xsl:text>
<xsl:apply-templates select ="name"/><br/> </p>
</body></html> </body>
</xsl:template> </html>
</xsl:template></xsl:stylesheet>
15 16
DWAX 2010.1 DWAX 2010.1
4
XSLT explained more XSLT explained more
Inserting new elements and attributes – xsl:elements
and xsl:attributes
XSL elements (W3C Recommendation XSLT
<?xml version="1.0" encoding="ISO-8859-1"?>
Version 1.0) : <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
– http://www.w3schools.com/xsl/xsl_w3celementref.asp <xsl:template match="/">
<html><body><h2>My CD Collection</h2>
XSL functions <table border="1"><th align="left">Title</th><th align="left">Artist</th>
– Inherited Xpath functions </tr>
17 18
DWAX 2010.1 DWAX 2010.1
5
Using document() function Using document()
The document() function can be used to Second XML file – picture.xml
get data from an external file <?xml version="1.0" encoding="ISO-8859-1" ?>
document('picture.xml')/pictures/picture <pictures>
<picture>robyn.jpg</picture>
– Loads the picture.xml file into memory </pictures>
(creates a DOM structure)
– Uses Xpath expression to navigate through <xsl:value-of select="document('picture.xml')/pictures/picture"/>
the document nodes – Adds the content of the picture element to the output stream
21 22
DWAX 2010.1 DWAX 2010.1
Adds a new empty element (img) with an Tests condition specified in the ‘test’ attribute
attribute (src) to the output stream.
Here checks if the picture element is empty
The content of the src attribute comes from the
If test is true, the statements contained in the
external picture.xml document
xsl:when element (……) are executed.
23 24
DWAX 2010.1 DWAX 2010.1
6
Using xsl:otherwise example-XSL.xml
<?xml-stylesheet type="text/xsl" href="example-XSL2.xsl"?>
<course>
<xsl:choose>
<unit>
<xsl:when test="picture =''"> <name>Developing Web Applications with XML</name>
...... <code>300111</code>
</xsl:when> <campus>Campbelltown</campus>
<xsl:otherwise> <location>LT 06</location>
<lecturer>Heidi Stratti</lecturer>
...... <practicals>4</practicals>
</xsl:otherwise> <picture>heidi.jpg</picture>
</xsl:choose> </unit>
<unit>
<name>E-business Technologies and Applications</name>
Statements contained in xsl:otherwise will <code>300126</code>
<campus>Paramatta</campus>
be applied if test condition is false <location>ECG 22</location>
<lecturer>Robyn Lawson</lecturer>
It is optional to use xsl:otherwise <practicals>1</practicals>
<picture/>
</unit>
25
</course> 26
DWAX 2010.1 DWAX 2010.1
7
XSLT final example XSLT final example
XSL explained – use of xsl elements
Final example - result choose, when, otherwise
<xsl:choose>
<xsl:when test="picture =''">
<!– Load other file, get picture name; DISPLAY THE PICTURE TAKEN
FROM THE OTHER XML FILE---- >
</xsl:when>
<xsl:otherwise>
<!-- DISPLAY THE PICTURE---- >
</xsl:otherwise>
</xsl:choose>
29 30
DWAX 2010.1 DWAX 2010.1
8
XSLT exercise
XSLT exercise <?xml version="1.0" encoding="ISO-8859-
1"?>
<?xml-stylesheet type="text/xsl"
<book year="">
<title>Brave New World</title>
Use the given XML source (in the next slide) code and write XSLT file href="NOVELS-1.xsl"?>
<author><name>Aldous</name>
(NOVELS-1.xsl) to display the Authors of books as links with a similar <surname>Huxley</surname></author>
<bestnovels> </book>
output given in figure 1 below. When clicked on these links it should <book year=""> <book year="">
submit a search to google.com to search the web like in figure 2 below <title>Ulysses</title> <title>The Sound and the Fury</title>
<author><name>James</name>
A few hints are given in the last slide <author><name>William</name>
<surname>Joyce</surname></author> <surname>Faulkner</surname></author>
</book> </book>
<book year=""> <book year="">
<title>The Great Gatsby</title> <title>Catch-22</title>
<author><name>F. Scott</name> <author><name>Joseph</name>
<surname>Fitzgerald</surname></author> <surname>Heller</surname></author>
</book> </book>
<book year=""> <book year="">
<title>A Portrait of the Artist as a <title>Darkness at Noon</title>
Young Man</title>
<author><name>Arthur</name>
<author><name>James</name>
<surname>Koestler</surname></author>
<surname>Joyce</surname></author>
</book>
</book>
<book year="">
<book year="">
<title>Sons and Lovers</title>
<title>Lolita</title>
<author><name>D. H.</name>
<author><name>Vladimir</name>
<surname>Lawrence</surname></author>
<surname>Nabokov</surname></author>
</book>
</book>
Figure 1 Figure 2 33 </bestnovels> 34
DWAX 2010.1 DWAX 2010.1