XML to HTML Using of XSLT

 Overview of XSLT

XSLT stands for EXtensible Stylesheet Language Transformations. 

1. XSLT stands for XSL Transformations
2. XSLT transforms an XML document into another XML document
3. XSLT uses XPath to navigate in XML documents
4. XSLT is a W3C Recommendation

As we know XML is light weight and we can also store sensitive data in that.  XSLT is very useful for wireless applications. Using XSLT we can create html pages by traversing a XML document

Below example illustrates how to transform a xml document to html. 

XML file

 <?xml version="1.0" encoding="ISO-8859-1"?>
<Employees>
 <Employee>
  <name>xxx</name>
  <dept>abc</dept>
  <country>USA</country>
  <salary>2000</salary>
  </Employee>
 <Employee>
  <name>xyz</name>
  <dept>dddd</dept>
  <country>USA</country>
  <salary>2000</salary>
 </Employee>
 <Employee>
  <name>Abc</name>
  <dept>dsds</dept>
  <country>USA</country>
  <salary>3000</salary>
 </Employee>
 <Employee>
  <name>ccc</name>
  <dept>HR</dept>
  <country>USA</country>
  <salary>54545</salary>
 </Employee>
 <Employee>
  <name>aaa</name>
  <dept>Admin</dept>
  <country>USA</country>
  <salary>54454545</salary>
 </Employee>
  </Employees>


XSLT file

 
 <?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>XYZ Company</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th>Name</th>
        <th>Dept</th>
<th>Country</th>
<th>Salary</th>
      </tr>
      <xsl:for-each select="Employees/Employee">
      <tr>
        <td><xsl:value-of select="name" /></td>
        <td><xsl:value-of select="dept" /></td>
 <td><xsl:value-of select="country" /></td>
 <td><xsl:value-of select="salary" /></td>
      </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

Output 

XYZ Company

NameDeptCountrySalary
xxxabcUSA2000
xyzddddUSA2000
AbcdsdsUSA3000
cccHRUSA54545
aaaAdminUSA54454545

Conclusion

XSLT is used to transform an XML document into below format 


   XML document
   HTML 
   XHTML

Normally XSLT does this by transforming each XML element into an XHTML element.


Similar Articles