WTA Topic 4:: Extensible Stylesheet Extensible Stylesheet Language (XSL)
WTA Topic 4:: Extensible Stylesheet Extensible Stylesheet Language (XSL)
WTA Topic 4:: Extensible Stylesheet Extensible Stylesheet Language (XSL)
Extensible Stylesheet
Language (XSL)
1
Topics:
XSL
Introduction
XSLT
XPATH
XSL-FO
XSL
XSLT Processing
reformatted.xsl
XSLT
Stylesheet
XSLT
Processor
reformatted.xml
(Reformatted XML
Document)
mydocument.xml
(XML
Document)
XSLT Processing
XSLT Template
<table>
<tr>
<th>First Name</th>
<th>Last Name</th>
</tr>
<tr>
<td>Lisa</td>
<td>Derik</td>
</tr>
<tr>
<td>Bill</td>
<td>Stark</td>
</tr>
</table>
Working
XSLT uses
10
11
Example : catalog1.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl"
href="xslcatalogs1.xsl"?>
<!DOCTYPE CATALOGS [
<!ELEMENT CATALOGS (BOOK)*>
<!ELEMENT BOOK (BOOKNAME, AUTHORNAME,
ISBN, PUBLISHER, PAGES?, PRICE)>
<!ELEMENT BOOKNAME (#PCDATA)>
<!ELEMENT AUTHORNAME (#PCDATA)>
<!ELEMENT ISBN (#PCDATA)>
<!ELEMENT PUBLISHER (#PCDATA)>
<!ELEMENT PAGES (#PCDATA)>
<!ELEMENT PRICE (#PCDATA)>
]>
<CATALOGS>
<BOOK>
<BOOKNAME>Oracle 8i</BOOKNAME>
<AUTHORNAME>Richard
Gosling</AUTHORNAME>
<ISBN>0-07-913702-4</ISBN>
<PUBLISHER>BPB Publications</PUBLISHER>
<PAGES>393</PAGES>
<PRICE>410.00</PRICE>
</BOOK>
<BOOK>
<BOOKNAME>XML in Action</BOOKNAME>
<AUTHORNAME>William J.
Pardi</AUTHORNAME>
<ISBN>0-07-914702-9</ISBN>
<PUBLISHER>Microsoft Press</PUBLISHER>
<PAGES>492</PAGES>
<PRICE>470.00</PRICE>
</BOOK>
<BOOK>
<BOOKNAME>XML to code</BOOKNAME>
<AUTHORNAME>Jesse Liberty</AUTHORNAME>
<ISBN>1-861000-95-2</ISBN>
<PUBLISHER>Wrox Press</PUBLISHER>
<PAGES>393</PAGES>
<PRICE>560.00</PRICE>
</BOOK>
<BOOK>
<BOOKNAME>Java Unleashed</BOOKNAME>
<AUTHORNAME>James
Spencer</AUTHORNAME>
<ISBN>0-7456-0964-1</ISBN>
<PUBLISHER>Techmedia
Publications</PUBLISHER>
<PAGES>491</PAGES>
<PRICE>670.00</PRICE>
</BOOK>
13
</CATALOGS>
Example 1 : xslcatalogs1.xsl
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<center><h1 style="background:blue">Catalogs Book Information</h1></center>
<table align="center" border="1">
<tr style="font-weight:bold; font-size:18">
<td>BOOK NAME</td>
<td>AUTHOR NAME</td>
<td>ISBN</td>
<td>PUBLISHER</td>
<td>PAGES</td>
<td>PRICE</td>
</tr>
<xsl:for-each select=CATALOGS/BOOK">
<tr>
<td><xsl:value-of select="BOOKNAME"/></td>
<td><xsl:value-of select="AUTHORNAME"/></td>
<td><xsl:value-of select="ISBN"/></td>
<td><xsl:value-of select="PUBLISHER"/></td>
<td><xsl:value-of select="PAGES"/></td>
<td><xsl:value-of select="PRICE"/></td>
</tr>
</xsl:for-each> </table>
</body>
</html>
catalog1.xml
</xsl:template>
</xsl:stylesheet>
14
15
Filtering
We can also filter the output from the XML file by adding a
criterion to the select attribute in the <xsl:for-each>
element.
<xsl:for-each select=CATALOGS/BOOK[PAGES=393']">
= (equal)
!= (not equal)
< (less than)
> (greater than)
16
Sorting
17
Example 2 : xslcatalogs2.xsl
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<center><h1 style="background:blue">Catalogs Book Information</h1></center>
<table align="center" border="1">
<tr style="font-weight:bold; font-size:18">
<td>BOOK NAME</td>
<td>AUTHOR NAME</td>
<td>ISBN</td>
<td>PUBLISHER</td>
<td>PAGES</td>
<td>PRICE</td>
</tr>
<xsl:for-each select=CATALOGS/BOOK">
<xsl:sort select=BOOKNAME/>
<tr>
<td><xsl:value-of select="BOOKNAME"/></td>
<td><xsl:value-of select="AUTHORNAME"/></td>
<td><xsl:value-of select="ISBN"/></td>
<td><xsl:value-of select="PUBLISHER"/></td>
<td><xsl:value-of select="PAGES"/></td>
<td><xsl:value-of select="PRICE"/></td>
</tr>
</xsl:for-each> </table>
</body> </html>
catalog2.xml
</xsl:template>
</xsl:stylesheet>
18
Conditional Statement
19
Example 3 : xslcatalogs3.xsl
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<center><h1 style="background:blue">Catalogs Book Information</h1></center>
<table align="center" border="1">
<tr style="font-weight:bold; font-size:18">
<td>BOOK NAME</td>
<td>AUTHOR NAME</td>
<td>ISBN</td>
<td>PUBLISHER</td>
<td>PAGES</td>
<td>PRICE</td>
</tr>
<xsl:for-each select=CATALOGS/BOOK">
<xsl:if test=PRICE<500>
<tr>
<td><xsl:value-of select="BOOKNAME"/></td>
<td><xsl:value-of select="AUTHORNAME"/></td>
<td><xsl:value-of select="ISBN"/></td>
<td><xsl:value-of select="PUBLISHER"/></td>
<td><xsl:value-of select="PAGES"/></td>
<td><xsl:value-of select="PRICE"/></td>
</tr>
</xsl:if>
</xsl:for-each> </table> </body> </html>
catalog3.xml
</xsl:template>
</xsl:stylesheet>
20
Multiple Choices
Syntax
<xsl:choose>
<xsl:when test="expression">
... some output ...
</xsl:when>
<xsl:otherwise>
... some output ....
</xsl:otherwise>
</xsl:choose>
21
22
xchoose.xml
23
24
<td><xsl:value-of select="carbs_per_serving"/></td>
<td><xsl:value-of select="fiber_per_serving"/></td>
<td><xsl:value-of select="fat_per_serving"/></td>
<td><xsl:value-of select="kj_per_serving"/></td>
</tr>
</xsl:when>
<xsl:otherwise>
<tr style="background-color:#cccccc">
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="carbs_per_serving"/></td>
<td><xsl:value-of select="fiber_per_serving"/></td>
<td><xsl:value-of select="fat_per_serving"/></td>
<td><xsl:value-of select="kj_per_serving"/></td>
</tr>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</table>
</xsl:template>
<xsl:template match="food_list">
<table>
<tr style="background-color:#ccff00">
<th>Food Item</th>
<th>Carbs (g)</th>
<th>Fiber (g)</th>
<th>Fat (g)</th>
<th>Energy (kj)</th>
</tr>
<xsl:for-each select="food_item">
<xsl:choose>
<xsl:when test="@type = 'grain'">
<tr style="background-color:#cccc00">
</xsl:stylesheet>
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="carbs_per_serving"/></td>
<td><xsl:value-of select="fiber_per_serving"/></td>
<td><xsl:value-of select="fat_per_serving"/></td>
<td><xsl:value-of select="kj_per_serving"/></td>
</tr>
</xsl:when>
<xsl:when test="@type = 'vegetable'">
<tr style="background-color:#00cc00">
<td><xsl:value-of select="name"/></td>
food.xml
25
Applying Templates
Example 5 : xsltemplate.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl"
href="xsltemplate.xsl"?>
<school>
<student>
<roll>678</roll>
<name>
<first>Harsh</first>
<last>Mehta</last>
</name>
<course>
<year>BTech</year>
<trim>12</trim>
<branch>IT</branch>
</course>
</student>
<student>
<roll> 782 </roll>
<name>
<first>Smita</first>
<last>Londhe</last>
</name>
<course>
<year>MBATech</year>
<trim>12</trim>
<branch>Computer</branch>
</course>
</student>
</school>
27
Example 5 : xsltemplate.xsl
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl=http://www.w3.org/1999/XSL/Transform>
<xsl:template match="school">
<h2><xsl:apply-templates
select="student/name"/></h2>
</xsl:template>
</xsl:stylesheet>
xsltemplate.xml
28
Example 6 : xslplanes.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="xslplanes.xsl"?>
<planes>
<plane>
<year> 1977 </year>
<make> Cessna </make>
<model> Skyhawk </model>
<color> Light blue and white </color>
</plane>
<plane>
<year> 1975 </year>
<make> Piper </make>
<model> Apache </model>
<color> White </color>
</plane>
</planes>
29
Example 6 : xslplanes.xsl
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="planes">
<html>
<body>
<h2>Airplane Descriptions</h2>
<xsl:for-each select="plane">
<span style="font-style: italic"> Year: </span>
<xsl:value-of select="year" /><br />
<span style="font-style: italic"> Make: </span>
<xsl:value-of select="make" /><br />
<span style="font-style: italic"> Model: </span>
<xsl:value-of select="model" /><br />
<span style="font-style: italic"> Color: </span>
<xsl:value-of select="color" /><br /><br />
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
xslplanes.xml
30
XPath
XSL-FO
32