XSLT Conversion >> Start Up:
What is XSLT?
XSLT is processors that can convert, manipulate XML files depending on XPATH statement. XSLT is a language that can define style for various XML elements depending on their positions. XSLT self is a hierarchical document. It is also a programming language with a moderate level of keywords and function.
What XSLT can do?
Most of the time we use XSLT to covert XML files to some other format:
- XML to XML
- XML to HTML
- XML to PDF
This post will discuss about converting XSLT using ASP.net.
1) Create a VS C# web project.
2) Add a XML document.
3) Add a XSLT document
VS Solution Structure:
Figure 1: VS Solution Structure
Now Let us see some code parts:
The ASPX Page: We will show converted HTML (form XML) as this page data.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Customer.aspx.cs" Inherits="XsltSolutions._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns=”http://www.w3.org/1999/xhtml” >
<form id=”form1″ runat=”server”>
using System.Xml;//need to add
using System.Xml.Xsl;//need to add
public partial class _Default : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
private void ConvertCustomerXml()
XmlDocument xmlDoc = new XmlDocument();
string xslPath = Server.MapPath(“Customer.xslt”);
XslCompiledTransform transform = new XslCompiledTransform();
transform.Transform(xmlDoc, null, Response.Output);
Here the ConvertCustomerXml() function is responsible for conversion. Please notice that we are loading a Customer.xml as xml input and also showing the Customer.xslt file. The Transform function is primarily responsible for XML to XSLT transformation. It takes three parameters (the XML, the Output method, and xslt arguments.
XML used in this Example: Customer.XML
<?xml version="1.0" encoding="utf-8" ?>
<!– This is list of employees –>
<![CDATA[includes a BA in psychology from Colorado State University in 1970.
She also completed “The Art of the Cold Call.” Nancy is a member of Toastmasters
<![CDATA[Andrew received his BTS commercial in 1974 and a Ph.D. in international
marketing from the University of Dallas in 1981. He is fluent in French and Italian
and reads German. He joined the company as a sales representative, was promoted
to sales manager in January 1992 and to vice president of sales in March 1993.
Andrew is a member of the Sales Management Roundtable, the Seattle Chamber of
Commerce, and the Pacific Rim Importers Association.]]>
<![CDATA[Janet has a BS degree in chemistry from Boston College (1984).
She has also completed a certificate program in food retailing management.
Janet was hired as a sales associate in 1991 and promoted to sales representative
in February 1992.]]>
XSLT used in this Example: Customer.xslt
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version=”1.0″ xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”>
<table cellspacing =”1px” cellpadding =”2″ style=”background-color:transparent; border:2px solid #00CCFF; font-family:calibri”>
Figure 2: XSLT Html Output
Solution Logical Coverage:
1) Naming the Table header using Loop:
Here I placed the table header dynamically form the XML using <xsl:for-each>. Here I have to be concerned about the face that, the table header must be appears only for the first time. Here name() function is returning all the elements names. So, I used the XPATH expression:
Here: //employee making sure we are taking the first employee so that the table header elements are only traversing once. So we are getting the following output:
2) Getting row using loop depending on elements:
Here I am traversing all the employee elements and creating a row for each employee elements.
3) Getting field value using loop:
Here I am getting all the value of node/elements and resulting in a table data (column).
4) Getting all the Node and Attributes:
This will get all the nodes an attributes. No need to get the attributes in a different way than getting all the nodes. In this example we are getting the attributes differently. But there we could use lines this also.