Transformation Using CSS And XSLT

In this session, I will discuss how to convert XML to XSLT. Basically, we need XSLT for the presentation of XML using some user-defined transformation.

Introduction

 
XML is known as eXtensible Markup Language. Like HTML, XML is also a markup language. It is used for storing and transporting data, not to display data.
 
Like HTML, it does not have predefined tags. It has only the user-defined tags. XML schema architecture is like a tree structure that has one root element and a number of branch elements. Elements and attributes are used for representing the data.
 
XSLTapplies some user-defined transformations in XML for the presentation of the XML. Using XSLT transforms, we can convert the XML document into various types of documents. 
 

The role of Attribute and Element

 
As per my knowledge, attribute is used to identify an element, but the element is used to store the data. Attribute has no closing tag, but element has a closing tag.
 
Example
 
In the below XML document, "college" and "Student Name" are known as elements while "name" is known as an attribute. 
  1. <college name="GEC">   <!-- name is attribute-->    
  2.   <StudentName>Anna</StudentName> <!-- StudentName is element-->    
  3.   <StudentName>Smith</StudentName>    
  4. </college>    

XML Transformation

 
There are two ways by which we can do the XML transformation.
  1. CSS (Cascade style sheet)
  2. XSLT (Extensible Stylesheet Language Transformations)
Before starting the above transformation approaches, we need an XML document. I will use the below XML document for XSLT transformation.
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <college>  
  3.   <student>  
  4.     <Name>Pradosh</Name>  
  5.     <RollNumber>100</RollNumber>  
  6.     <Branch>CSE</Branch>  
  7.   </student>  
  8.   <student>  
  9.     <Name>Bidyasagar</Name>  
  10.     <RollNumber>101</RollNumber>  
  11.     <Branch>CSE</Branch>  
  12.   </student>  
  13.   <student>  
  14.     <Name>Bibhu</Name>  
  15.     <RollNumber>102</RollNumber>  
  16.     <Branch>CSE</Branch>  
  17.   </student>  
  18.   <student>  
  19.     <Name>Nila</Name>  
  20.     <RollNumber>103</RollNumber>  
  21.     <Branch>ECE</Branch>  
  22.   </student>  
  23.   <student>  
  24.     <Name>Chandan</Name>  
  25.     <RollNumber>104</RollNumber>  
  26.     <Branch>ECE</Branch>  
  27.   </student>  
  28.   </college>  
After transformation, the raw XML looks like below.
 
 

Trasformation using CSS

 
We need to write some CSS codes using the XML node for XML transformation. I used the below CSS codes for the transformation of the above XML document.
  1. college {     
  2.     margin-top:2%;      
  3.    padding0px 2px;    
  4.    background-colorwhite;    
  5. }    
  6.      
  7. student {    
  8.    displayblock;   
  9.    margin-bottom2px;    
  10.    border-bottom:1px solid red;  
  11. }   
For rendering the above CSS file to the xml we have a predefined code in XML i.e. 'xml-stylesheet'. We need to add the 'xml-stylesheet' with the properties under the XML declaration.
 
The below image is the reference of how to add style sheet in XML.
 
 
 
After transformation, the XML looks like below.

Transformation using XSLT 

 
To convert the XML document into XHTML or other documents we need to use XSLT. XSLT has various types of predefined approaches. Before moving to the XSLT we need to get knowledge about the approaches.
 
Here I will discuss the various types of XSLT approaches,
  1. <xsl:template>
  2.  <xsl:value-of>
  3. <xsl:for-each>
  4. <xsl:sort>
  5. <xsl:if>
  6. <xsl:choose>
<xsl:template>
 
It is used for finding out the matched nodes from XML using 'match' attribute. we can pass any element from the XML to the 'match' attribute. 
 
It has the following attributes available,
  • name
    This attribute is used for adding a template name. This is very unique, two templates can not be available on the same name.

  • mode
    This attribute is used for adding a mode.

  • priority
    This attribute is used for the priority of the document.
<xsl:value-of>

It is used for fetching data from the selected node.

It has two following attributes available,
  • select - This attribute is used to select the attribute specifying some nodes.
  • disable-output-escaping - We can assign this attribute as yes or no. If we choose yes, then the output of the special character will view as it is otherwise it will represent the special character symbols.
<xsl:for-each>

Like other languages for each loop, it iterates the passed conditions. 

<xsl:sort>

It sorts the output.

<xsl:if>

Like other languages 'if' condition is used for conditional statement check.?

<xsl:choose>

This is used during multiple conditional statements.
We need to write some xslt codes using the XML node for XML transformation. I used the below xslt codes for the transformation of the above XML document. 
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">    
  3.   <xsl:template match="/">    
  4.     <html>    
  5.       <head>    
  6.         <style type="text/css">    
  7.           th{    
  8.           background-color: bisque;    
  9.           }    
  10.         </style>    
  11.       </head>    
  12.       <body>    
  13.         <table border="1" style="border-color: red;">    
  14.           <tr>    
  15.             <th>Name</th>    
  16.             <th>Roll Number</th>    
  17.             <th>Branch</th>    
  18.           </tr>    
  19.           <xsl:for-each select="college/student">    
  20.             <tr>    
  21.               <td>    
  22.                 <xsl:value-of select="Name"/>    
  23.               </td>    
  24.               <td>    
  25.                 <xsl:value-of select="RollNumber"/>    
  26.               </td>    
  27.               <td>    
  28.                 <xsl:value-of select="Branch"/>    
  29.               </td>    
  30.             </tr>    
  31.           </xsl:for-each>    
  32.         </table>    
  33.       </body>    
  34.     </html>    
  35.   </xsl:template>    
  36. </xsl:stylesheet>    
After transformation the XML looks like below.
 

Difference between XSLT and CSS transformation

  • CSS is used for the user interface purpose and XSLT is a language where we can implement condition types.
  • CSS changes the visual impact whereas XSLT is responsible for structural impact.
  • CSS is supported by all the browsers while XSLT is not supported. 

Summary

 
I hope this tutorial will provide information about basics of XML transformation and it will help you to implement of XML  transformation using CSS and XSLT.