XML and XSL Tutorial with Example..
Tutorial1.xml
<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="tvshows.xsl" ?>
<TvShows>
<Shows>
<name>Utaran</name>
<release>2005</release>
<enddate>2009</enddate>
<network country="India">Star Plus</network>
<poster href="http://www.cinejosh.com/gallereys/bollywood/normal/uttaran_serial_1000_episodes_completion_party_0911120942/uttaran_serial_1000_episodes_completion_party_0911120942_034.jpg"></poster>
<viewers units="millions">7.82</viewers>
</Shows>
<Shows>
<name>Dance India Dance</name>
<release>2006</release>
<enddate>2013</enddate>
<network country="India">Colors</network>
<poster href="http://upload.wikimedia.org/wikipedia/en/thumb/4/45/Dance_India_Dance_Judges.jpg/290px-Dance_India_Dance_Judges.jpg"></poster>
<viewers units="millions">15.82</viewers>
</Shows>
<Shows>
<name>Kahaani Ghar Ghar Ki</name>
<release>2007</release>
<enddate>2013</enddate>
<network country="India">Colors</network>
<poster href="http://media.startv.in/newstream/star/starplus/kahani_ghar_ghar_ki/1651/kahani_ghar_ghar_ki_ep1651_300x200.jpg"></poster>
<viewers units="millions">2.82</viewers>
</Shows>
</TvShows>
TVShows.XSL File
<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<head>
<title>TV Shows</title></head>
<body>
Version: <xsl:value-of select="system-property('xsl:version')"/><br />
Vendor: <xsl:value-of select="system-property('xsl:vendor')"/><br />
Vendor URL: <xsl:value-of select="system-property('xsl:vendor-url')"/><br />
<xsl:for-each select="TvShows/Shows">
<a href="#{generate-id(name)}">
<xsl:value-of select="name"/>
</a><br/>
</xsl:for-each>
<xsl:for-each select="TvShows/Shows">
<xsl:sort select="name" order="ascending" data-type="text"/>
<h3>
<a name="{generate-id(name)}">
<xsl:value-of select="name"/>
</a>
</h3>
<img>
<xsl:attribute name="src">
<xsl:value-of select="poster/@href"/>
</xsl:attribute>
</img>
<br/>
<xsl:value-of select="current()"/> <br/>
<p> The show <xsl:value-of select="name"/> was
released in <xsl:value-of select="release"/> by
<xsl:value-of select="network"/>. It had an average viewership
of <xsl:value-of select="viewers"/> million people. It was
cancelled in <xsl:value-of select="enddate"/>
</p> <br/>
</xsl:for-each>
<table border="2">
<tr><th>Name</th><th>Network</th><th>Viewers</th></tr>
<xsl:for-each select="TvShows/Shows">
<xsl:if test="release < 2006">
<tr bgcolor="red">
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="network"/></td>
<td><xsl:value-of select="viewers"/></td>
</tr>
</xsl:if>
<xsl:choose>
<xsl:when test="release>2006">
<tr bgcolor="yellow">
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="network"/></td>
<td><xsl:value-of select="viewers"/></td>
</tr>
</xsl:when>
<xsl:when test="release=2006">
<tr bgcolor="orange">
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="network"/></td>
<td><xsl:value-of select="viewers"/></td>
</tr>
</xsl:when>
<xsl:otherwise>
<tr bgcolor="pink">
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="network"/></td>
<td><xsl:value-of select="viewers"/></td>
</tr>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
OUTPUT
Version: 1.0
Vendor: libxslt
Vendor URL: http://xmlsoft.org/XSLT/
Utaran
Dance India Dance
Kahaani Ghar Ghar Ki
Dance India Dance 2006 2013 Colors 15.82
Kahaani Ghar Ghar Ki 2007 2013 Colors 2.82
Utaran 2005 2009 Star Plus 7.82
Tutorial1.xml
<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="tvshows.xsl" ?>
<TvShows>
<Shows>
<name>Utaran</name>
<release>2005</release>
<enddate>2009</enddate>
<network country="India">Star Plus</network>
<poster href="http://www.cinejosh.com/gallereys/bollywood/normal/uttaran_serial_1000_episodes_completion_party_0911120942/uttaran_serial_1000_episodes_completion_party_0911120942_034.jpg"></poster>
<viewers units="millions">7.82</viewers>
</Shows>
<Shows>
<name>Dance India Dance</name>
<release>2006</release>
<enddate>2013</enddate>
<network country="India">Colors</network>
<poster href="http://upload.wikimedia.org/wikipedia/en/thumb/4/45/Dance_India_Dance_Judges.jpg/290px-Dance_India_Dance_Judges.jpg"></poster>
<viewers units="millions">15.82</viewers>
</Shows>
<Shows>
<name>Kahaani Ghar Ghar Ki</name>
<release>2007</release>
<enddate>2013</enddate>
<network country="India">Colors</network>
<poster href="http://media.startv.in/newstream/star/starplus/kahani_ghar_ghar_ki/1651/kahani_ghar_ghar_ki_ep1651_300x200.jpg"></poster>
<viewers units="millions">2.82</viewers>
</Shows>
</TvShows>
TVShows.XSL File
<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<head>
<title>TV Shows</title></head>
<body>
Version: <xsl:value-of select="system-property('xsl:version')"/><br />
Vendor: <xsl:value-of select="system-property('xsl:vendor')"/><br />
Vendor URL: <xsl:value-of select="system-property('xsl:vendor-url')"/><br />
<xsl:for-each select="TvShows/Shows">
<a href="#{generate-id(name)}">
<xsl:value-of select="name"/>
</a><br/>
</xsl:for-each>
<xsl:for-each select="TvShows/Shows">
<xsl:sort select="name" order="ascending" data-type="text"/>
<h3>
<a name="{generate-id(name)}">
<xsl:value-of select="name"/>
</a>
</h3>
<img>
<xsl:attribute name="src">
<xsl:value-of select="poster/@href"/>
</xsl:attribute>
</img>
<br/>
<xsl:value-of select="current()"/> <br/>
<p> The show <xsl:value-of select="name"/> was
released in <xsl:value-of select="release"/> by
<xsl:value-of select="network"/>. It had an average viewership
of <xsl:value-of select="viewers"/> million people. It was
cancelled in <xsl:value-of select="enddate"/>
</p> <br/>
</xsl:for-each>
<table border="2">
<tr><th>Name</th><th>Network</th><th>Viewers</th></tr>
<xsl:for-each select="TvShows/Shows">
<xsl:if test="release < 2006">
<tr bgcolor="red">
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="network"/></td>
<td><xsl:value-of select="viewers"/></td>
</tr>
</xsl:if>
<xsl:choose>
<xsl:when test="release>2006">
<tr bgcolor="yellow">
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="network"/></td>
<td><xsl:value-of select="viewers"/></td>
</tr>
</xsl:when>
<xsl:when test="release=2006">
<tr bgcolor="orange">
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="network"/></td>
<td><xsl:value-of select="viewers"/></td>
</tr>
</xsl:when>
<xsl:otherwise>
<tr bgcolor="pink">
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="network"/></td>
<td><xsl:value-of select="viewers"/></td>
</tr>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
OUTPUT
Version: 1.0
Vendor: libxslt
Vendor URL: http://xmlsoft.org/XSLT/
Utaran
Dance India Dance
Kahaani Ghar Ghar Ki
Dance India Dance
Dance India Dance 2006 2013 Colors 15.82
The show Dance India Dance was released in 2006 by Colors. It had an average viewership of 15.82 million people. It was cancelled in 2013
Kahaani Ghar Ghar Ki
Kahaani Ghar Ghar Ki 2007 2013 Colors 2.82
The show Kahaani Ghar Ghar Ki was released in 2007 by Colors. It had an average viewership of 2.82 million people. It was cancelled in 2013
Utaran
Utaran 2005 2009 Star Plus 7.82
The show Utaran was released in 2005 by Star Plus. It had an average viewership of 7.82 million people. It was cancelled in 2009
Name | Network | Viewers |
---|---|---|
Utaran | Star Plus | 7.82 |
Utaran | Star Plus | 7.82 |
Dance India Dance | Colors | 15.82 |
Kahaani Ghar Ghar Ki | Colors | 2.82 |