Nothing Special   »   [go: up one dir, main page]

Web Technologies Lab Experiment 09: Name: Harshdeep Telang ID: 191070024 Branch: Computer Engineering

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Web Technologies Lab

Experiment 09
Submission Date : 14/04/2021
Name : Harshdeep Telang
ID : 191070024
Branch: Computer Engineering

AIM:Write Programs using XML Schema XSLT/XSL.

THEORY:

XSL:
Before learning XSLT, we should first understand XSL which stands for
Extensible Stylesheet Language. It is similar to XML as CSS is to HTML.

Need for XSL


In case of HTML document, tags are predefined such as table, div, and span;
and the browser knows how to add style to them and display those using CSS
styles. But in case of XML documents, tags are not predefined. In order to
understand and style an XML document, World Wide Web Consortium (W3C)
developed XSL which can act as XML based Stylesheet Language. An XSL
document specifies how a browser should render an XML document.

Following are the main parts of XSL −


 XSLT − used to transform XML document into various other types of
document.
 XPath − used to navigate XML document.
 XSL-FO − used to format XML document.
What is XSLT
XSLT, Extensible Stylesheet Language Transformations, provides the ability to
transform XML data from one format to another automatically.

How XSLT Works


An XSLT stylesheet is used to define the transformation rules to be applied on
the target XML document. XSLT stylesheet is written in XML format. XSLT
Processor takes the XSLT stylesheet and applies the transformation rules on
the target XML document and then it generates a formatted document in the
form of XML, HTML, or text format. This formatted document is then utilized
by XSLT formatter to generate the actual output which is to be displayed to
the end-user.

XML Namespace
 XML Namespaces are the unique names .
 XML Namespace is a mechanism by which element orattribute is
assigned to a group.
 XML Namespace is used to avoid the name conflicts in the XML
document.
 XML Namespace is recommended by W3C.

XML Namespace Declaration:

It is declared using reserved attribute such as the


attribute is xmlns or it can begin with xmlns:

Syntax:
<element xmlns:name = "URL">

Advantages
Here are the advantages of using XSLT −
 Independent of programming. Transformations are written in a separate
xsl file which is again an XML document.
 Output can be altered by simply modifying the transformations in xsl
file. No need to change any code. So Web designers can edit the
stylesheet and can see the change in the output quickly.

Let’s suppose we have the following sample XML file, students.xml, which is
required to be transformed into a well-formatted HTML document.
students.xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="Student.xsl" ?>
 <student>
  <s>
   <name> Akash Dineshkumar Nigam</name>
   <branch> CSE</branch>
   <age>19</age>
   <city> Mumbai</city>
  </s>
  <s>
   <name> Harshdeep Telang </name>
   <branch> CSE</branch>
   <age> 19</age>
   <city> Ambernath </city>
  </s>
  <s> 
   <name> Shivam Tiwari</name>
   <branch> Chemical</branch>
   <age> 19</age>
   <city> Diva </city>
  </s>
  <s> 
   <name> Ravi Maurya </name>
   <branch> IT</branch>
   <age> 20 </age>
   <city> Karachi</city>
  </s>
  <s> 
   <name> Sejal Memane</name>
   <branch> Chemical</branch>
   <age> 18</age>
   <city> Sion</city>
  </s>
 </student>

We need to define an XSLT style sheet document for the above XML
document to meet the following criteria −
 Page should have a title Students.
 Page should have a table of student details.
 Columns should have following headers: Roll No, First Name, Last Name,
Nick Name, Marks
 Table must contain details of the students accordingly.

Step 1: Create XSLT document


Create an XSLT document to meet the above requirements, name it as
students.xsl and save it in the same location where students.xml lies.
<?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 bgcolor="#ffcccc">
  <h1 align="center"> <u> Student's Basic Details </u></h1>
   <table border="3" align="center" >
   <tr bgcolor="#828282">
    <th>Name</th>
    <th>Branch</th>
    <th>Age</th>
    <th>City</th>
   </tr>
    <xsl:for-each select="student/s">
   <tr bgcolor="#717171">
    <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>

Step 2: Link the XSLT Document to the XML Document


Update student.xml document with the following xml-stylesheet tag. Set href
value to students.xsl

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="Student.xsl" ?>
<student>

</student>

Step 3: View the XML Document in Internet Explorer


students.xsl
OUTPUT:

CONCLUSION:
In this experiment we have successfully understand XSL which stands for
Extensible Stylesheet Languagealong with their implementation.
We studied programs using XML Schema XSLT/XSL.

You might also like