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 vs XSLT
CSS | XSLT |
---|
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.
Syntax
<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.
Syntax
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.
Syntax
<xsl:for-each select=“pattern”>
<!--XSLT code-->
</xsl:for-each>
4. Sort element: It sorts the data depending on the values assigned to elements and attributes.
Syntax
<xsl:sort select=“expression” order=“ascending|descending”
case-order=“upper-first|lower-first”
data-type=“text|number|userdefined”/>
Where,
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.
Syntax
<xsl:text>Name:</xls:text>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"?>
<class>
<Employee Id="101">
<firstname>Nirja</firstname>
<lastname>Shah</lastname>
<salary>85000</salary>
</Employee>
<Employee Id="102">
<firstname>Prashant</firstname>
<lastname>Saxena</lastname>
<salary>95000</salary>
</Employee>
</class>
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="/">
<html>
<body>
<h2>Employee</h2>
<table>
<tr>
<th>Id</th>
<th>FirstName</th>
<th>LastName</th>
<th>Salary</th>
</tr>
<xsl:for-each select="class/Employee">
<tr>
<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>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
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.
Output: