Wednesday, November 9, 2011

How to convert Xml in to Html (xHtml)

Hi today i got the idea how we can convert our xml page in to xhtml . Its quit simple as just like  css style sheet is uses to decorate html page.
The major different in the process just here we are use xsl (xml style sheet ) instead css. This can be done by the help of  Xslt (xml style sheet transformation ).
Here i am post the example (Its a xml page where we give href relation with xsl plae like.

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>
<catalog>
  <cd>
    <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
    <country>USA</country>
    <company>Columbia</company>
    <price>10.90</price>
    <year>1985</year>
  </cd>
</catalog>
And the code of  cdcatalog.xsl is here
<?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>My CD Collection</h2>
  <table border="1">
    <tr bgcolor="#9acd32">
      <th>Title</th>
      <th>Artist</th>
    </tr>
    <xsl:for-each select="catalog/cd">
    <tr>
      <td><xsl:value-of select="title"/></td>
      <td><xsl:value-of select="artist"/></td>
    </tr>
    </xsl:for-each>
  </table>
  </body>
  </html>
</xsl:template>

</xsl:stylesheet>
The reference of style sheet already given in the top header of xml page.

 <xsl:template match="/"> :-- Describe that we are apply style sheet in whole xml page due to match attribute match="/"


<xsl:for-each select="catalog/cd"> :-- Its reach all the nodes under the catalog .

Hi.... an another example of Converting xml in to Html
My Student.xml page code
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="student.xsl"?>

<student>
          <Roll_number>
                        <Name>Deepak Sharma</Name>
                        <Class>5th</Class>
                        <Year>2010</Year>  
          </Roll_number>
          <Roll_number>
                        <Name>Arun Sharma</Name>
                        <Class>5th</Class>
                        <Year>2011</Year>  
          </Roll_number>
          <Roll_number>
                        <Name>Mohit Sharma</Name>
                        <Class>5th</Class>
                        <Year>2010</Year>  
          </Roll_number>

</student>
Just check it that i have already mention student.xsl in there head session.
The code of student.xsl is.....
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
          <table><tr>
                 <th>Name</th><th>Class</th></tr>
                  <xsl:for-each select="student/Roll_number">
                 <tr><td><xsl:value-of select="Name"/></td><td><xsl:value-of select="Year"/></td></tr>
                 </xsl:for-each>
          </table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
The xml style sheet flow chat like that....
1.<xsl:stylesheet version="" href="" ?></xsl:stylesheet>
2.<xsl:template></xsl:template>
3.<xsl:for-each></xsl:for-each>
4.<xsl:value-of select="">
Thanks




No comments:

Post a Comment