Wednesday 4 December 2013

How to convert XML into HTML by using XSLT



XSLT

eXtensible Stylsheet Language for Transformation specifies rules for conversion i.e. template or template rules. There are two types of template rules for transformation: Matching and selection templates, and Branching elements.
How to convert XML into HTML


e.g. Hello.xml
<?xml version="1.0" encoding="UTF-8"?>

<?xml-stylesheet type="text/xsl" href="nrs.xsl"?>

<weather1>

<day1>

<location>Vita</location>

<date>20/04/2013</date>

<temp>20'c</temp>

</day1>

<day1>

<location>Kolhapur</location>

<date>30/04/2013</date>

<temp>30'c</temp>

</day1>

</weather1>

e.g. Hello.xsl
<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:template match="weather1">

<html>

<body>

 <table border="10">

  <tr>

    <th>

            <font size="10" color="red">Location</font>

    </th>

    <th>

            <font size="10" color="green">Date</font>

    </th>

    <th>

            <font size="10" color="blue">Temp</font>

    </th>

   </tr>

   <xsl:for-each select="//day1">

            <tr>

                        <td>

                        <xsl:value-of select="location"/>

                        </td>

                        <td>

                        <xsl:value-of select="date"/>

                        </td>

                        <td>

                        <xsl:value-of select="temp"/>

                        </td>

            </tr>

            </xsl:for-each>

     </table>

    </body>

   </html>

  </xsl:template>

</xsl:stylesheet>




Save both these program in one directory.
check well form of each one .
Run Hello.xml and Watch the output of xml file into html.

ShareThis

Related Posts Plugin for WordPress, Blogger...