Web Technology - Old Questions
2. What is the use of XSLT? Create a XML file with elements and elements having attributes. Write the equivalent XSLT of the XML for rendering as a HTML document in a browser.
XSLT (Extensible Stylesheet Language Transformations) is a language for transforming XML documents into other XML documents, or other formats such as HTML for web pages, plain text or XSL Formatting Objects, which may subsequently be converted to other formats, such as PDF, Postscript and PNG.
The original document is not changed; rather, a new document is created based on the content of an existing one. Typically, input documents are XML files, but anything from which the processor can build an XQuery and XPath Data Model can be used, such as relational database tables or geographical information system.
Creating Students.xml as:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl "href="student.xsl" ?>
<student>
<s>
<name> Jayanta </name>
<branch> CS</branch>
<age>20</age>
<city> Kathmandu </city>
</s>
<s>
<name> Manoj </name>
<branch> IT </branch>
<age> 20</age>
<city> Pokhara </city>
</s>
<s>
<name> Sagar</name>
<branch> HM </branch>
<age> 23</age>
<city> Biratnagar </city>
</s>
</student>
Now, equivalent XSLT of the above XML for rendering as a HTML document in a browser is given below:
// student.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h1 align="center">Students' Basic Details</h1>
<table border="3" align="center" >
<tr>
<th>Name</th>
<th>Branch</th>
<th>Age</th>
<th>City</th>
</tr>
<xsl:for-each select="student/s">
<tr>
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="branch"/></td>
<td><xsl:value-of select="age"/></td>
<td><xsl:value-of select="city"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>