Friday, December 30, 2011

XSLT and XML

XSLT is a specialized programming language for transforming XML documents. Even though it is part of XSLand as such intended to be used for transforming XMLdocuments into XSL-FO for presentation purposes...
Fro example :

XML Data :

<?xml-version = "1.0" ?>
<?xml-stylesheet type="text/xsl" href="samplexslt.xsl"?>
<sample>
<a>
<Name>Ramkumar</Name>
<age>23</age>
<occupation>SE</occupation>
</a>
<a>
<Name>Ramya</Name>
<age>21</age>
<occupation>SE</occupation>
</a>
<a>
<Name>Mahe</Name>
<age>21</age>
<occupation>SE</occupation>
</a>
</sample>

XSLT example:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>XSLT test</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Name</th>
<th>Age</th>
<th>Occupation</th>
</tr>
<xsl:for-each select="sample\a">
<tr>
<td><xsl:value-of select="age"/></td>
<xsl:choose>
<xsl:when test="age &gt; 21">
<td bgcolor="#ff00ff">
<xsl:value-of select="Name"/></td>
</xsl:when>
<xsl:otherwise>
<td><xsl:value-of select="Name"/></td>
</xsl:otherwise>
</xsl:choose>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

No comments:

Post a Comment