XML XSLT

XML and XSLT

To transform an XML document into HTML, the XSLT can be used.

Displaying XML with XSLT:

Being a recommended style sheet language for XML, XSLT or eXtensible Stylesheet Language Transformations is far more sophisticated than CSS. To add/remove elements and attributes to or from the output file, rearrange and sort elements, perform tests and make decisions about which elements to hide and display, and many more, XSLT can be used. To find information in an XML document, XPath is used by XSLT. Being a major element in the XSLT standard, XPath knowledge allows us to take great advantage of XSLT.

Example:

<?xml version="1.0" encoding="UTF-8"?>
<result>
<student>
    <name>Tom</name>
    <class>8</class>
    <exam>
   Finals (Out of 100)
   </exam>
    <marks>99</marks>
</student>
<student>
    <name>Jin</name>
    <class>9</class>
    <exam>
   Finals (Out of 100)
   </exam>
    <marks>90</marks>
</student>
<student>
   <name>Jenny</name>
    <class>7</class>
    <exam>
   Finals (Out of 100)
   </exam>
    <marks>89</marks>
</student>
<student>
   <name>Adi</name>
    <class>10</class>
    <exam>
   Finals (Out of 100)
   </exam>
    <marks>85</marks>
</student>
<student>
    <name>Smith</name>
    <class>10</class>
    <exam>
   Finals (Out of 100)
   </exam>
    <marks>80</marks>
</student>
</result>

Before displaying the above XML file in a browser, we will transform it into HTML, using XSLT.

Example XSLT Stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE">
<xsl:for-each select="result/student">
  <div style="background-color:teal;color:white;padding:4px">
    <span style="font-weight:bold"><xsl:value-of select="name"/> - </span>
    <xsl:value-of select="class"/>
    </div>
  <div style="margin-left:20px;margin-bottom:1em;font-size:10pt">
    <p>
    <xsl:value-of select="exam"/>
    <span style="font-style:italic"> (<xsl:value-of select="marks"/> out of hundred)</span>
    </p>
  </div>
</xsl:for-each>
</body>
</html>
Please Share