XSL Transformation Style Sheet

What is XSL?

CSS has drawbacks, when linked with XML documents like, sorting and reordering elements based on a condition and displaying only sensitive elements which is possible in XSL.

XSL is made of two parts

a) XSL Transformation (XSLT)
Transformation between two XML is possible using XSLT.

b) XML Path (Xpath)
XML Path is an XML based language which is used to access elements and attributes present in the XML document.


CSS is unable to perform operations on elements like add, delete, reordering etc.XSLT is able to perform operations on elements like add, delete, reordering.
Access to PI and attributes of XML document is not possible in CSS.XSLT allows to access and manipulate the comments, PI, attribute names and values within the XML document.
Uses less memory.Requires more memory to manipulate the document.
Syntax is different than XML.Syntax is similar to XML.
It is simple to use and suitable for only small documents.It is complex to use.

XSLT Elements

XSLT elements are used for selecting and formatting data.

1. Stylesheet element: Stylesheet declaration statement is used to inform the browser that, this is a style sheet file.

<xsl:stylesheet xmlns:xsl= “http://www.w3.org/XSL/Transform” version=“1.0”>

2. Value-of element: Value-of element returns the value of specified element or an attribute.

Value of Element: <xsl:value-of select=“elementname”/>
Value of Attribute: <xsl:value-of select=“@attributename”/>  

3. For-each element: It instructs the XSLT processor to process the information for each instance of the specified pattern.


<xsl:for-each select=“pattern”>
     <!--XSLT code-->

4. Sort element: It sorts the data depending on the values assigned to elements and attributes.


<xsl:sort select=“expression” order=“ascending|descending”

     Select: Represents the element name.
     Order: Represents the sort order and the default value is ascending.
     case-order: Represents the data type of the data to be sorted and default value is text.
     data-type: Represents the data type of the data to be sorted and default value is text.

5. Text element: This element generates constant text in the output and used to display labels.


Explain the transformation of XML document using XSLT.

Step 1: Create an XML document which contains information of employee and save with employee.xml.

<?xml version="1.0"?>
     <Employee Id="101">
     <Employee Id="102">

In above XML document we have mentioned a employee information.

Step 2: Create XSLT document to fulfill the above requirement  and save with employee.xsl in same folder along with employee.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">  
<xsl:template match="/">
          <xsl:for-each select="class/Employee">
               <td><xsl:value-of select="@Id"/></td>
               <td><xsl:value-of select="firstname"/></td>
               <td><xsl:value-of select="lastname"/></td>
               <td><xsl:value-of select="salary"/></td>

Step 3: Link the XSLT document to XML document by following xml-stylesheet tag.
<?xml-stylesheet type="text/xsl" href="employee.xsl"?>

Step 4: Run the employee.xml file in the browser.

salary xslt