Web Technologies - Old Questions

3. Explain about XML and how to create HTML document with the help of XML.

10 marks | Asked in 2073

XML stands for Extensible Markup Language. It is a text-based markup language derived from Standard Generalized Markup Language (SGML).

XML tags identify the data and are used to store and organize the data, rather than specifying how to display it like HTML tags, which are used to display the data. 

There are three important characteristics of XML that make it useful in a variety of systems and solutions −

  • XML is extensible − XML allows you to create your own self-descriptive tags, or language, that suits your application.
  • XML carries the data, does not present it − XML allows you to store the data irrespective of how it will be presented.
  • XML is a public standard − XML was developed by an organization called the World Wide Web Consortium (W3C) and is available as an open standard.

We can create HTML document from XML using XSLT.

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> Sabina </name> 

   <branch> IT </branch> 

   <age> 20</age> 

   <city> Pokhara </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>