Tuesday, March 20, 2012

Linking the XSL Style Sheet to the XML Document



Using this approach, you do not need any code behind to have an
transformation for your only client side requirement.
Save following two files in your C:\SomeFolder

File1 - Home.xml

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="UseMeToTransform.xsl"?>
<Root>
<Item>
<cat>1</cat>
<Name>Car</Name>
</Item>
<Item>
<cat>2</cat>
<Name>Bike</Name>
</Item>
<Item>
<cat>3</cat>
<Name>Boat</Name>
</Item>
</Root>

File2 - UseMeToTransform.xsl

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="Root">
<html>
<table border="1">
<tr>
<td>
<xsl:value-of select="'Category'"/>
</td>
<td>
<xsl:value-of select="'Name'"/>
</td>
</tr>
<xsl:for-each select="Item">
<tr>
<td>
<xsl:value-of select="cat"/>
</td>
<td>
<xsl:value-of select="Name"/>
</td>
</tr>
</xsl:for-each>
</table>
</html>
</xsl:template>
</xsl:stylesheet>

Now double click onthe Home.xml files you will get the desired output.
Where you can see that the date of Home.xml has been converted in to HTML contents.

No comments:

Post a Comment