Tags

, , ,


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.

Steps:

1)      Create a VS C# web project.

2)      Add a XML document.

3)      Add a XSLT document

VS Solution Structure:

XSLT

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.

Code:

<%@ 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&#8221; >

<head runat=”server”>

<title></title>

</head>

<body>

<form id=”form1″ runat=”server”>

<div>

</div>

</form>

</body>

</html>

Code Behind:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Xml;//need to add

using System.Xml.Xsl;//need to add

namespace XsltSolutions

{

public partial class _Default : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

ConvertCustomerXml();

}

private void ConvertCustomerXml()

{

XmlDocument xmlDoc = new XmlDocument();

xmlDoc.Load(MapPath(“Customer.xml”));

string xslPath = Server.MapPath(“Customer.xslt”);

XslCompiledTransform transform = new XslCompiledTransform();

transform.Load(xslPath);

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 –>

<employees>

<employee employeeid=”1″>

<firstname>Nancy</firstname>

<lastname>Davolio</lastname>

<homephone>(206) 555-9857</homephone>

<notes>

<![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

International.]]>

</notes>

</employee>

<employee employeeid=”2″>

<firstname>Andrew</firstname>

<lastname>Fuller</lastname>

<homephone>(206) 555-9482</homephone>

<notes>

<![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.]]>

</notes>

</employee>

<employee employeeid=”3″>

<firstname>Janet</firstname>

<lastname>Leverling</lastname>

<homephone>(206) 555-3412</homephone>

<notes>

<![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.]]>

</notes>

</employee>

</employees>

XSLT used in this Example: Customer.xslt

<?xml version="1.0" encoding="iso-8859-1"?>

</xsl:stylesheet>

<xsl:stylesheet version=”1.0″ xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”&gt;

<xsl:template match=”/”>

<html>

<body>

<h2>Customer Information</h2>

<table style=”background-color:#97DDFF”>

<tr>

<td>

<table cellspacing =”1px” cellpadding =”2″ style=”background-color:transparent; border:2px solid #00CCFF; font-family:calibri”>

<tr>

<th style=”background-color:#C1FFFF”>

Employee Id

</th>

<xsl:for-each select=”/employees/employee[1]/*”>

<th style=”background-color:#C1FFFF”>

<xsl:value-of select=”name()”/>

</th>

</xsl:for-each>

</tr>

<xsl:for-each select=”/employees/employee”>

<tr>

<td style=”background-color:white”>

<xsl:value-of select=”@employeeid”/>

</td>

<xsl:for-each select=”node()”>

<td style=”background-color:white”>

<xsl:value-of select=”.”/>

</td>

</xsl:for-each>

</tr>

</xsl:for-each>

</table>

</td>

</tr>

</table>

</body>

</html>

</xsl:template>

Output:XSLTOutput

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:

<xsl:for-each select="/employees/employee[1]/*">

Here:  //employee[1] 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:

Header

2)      Getting row using loop depending on elements:

<xsl:for-each select="/employees/employee">

Here I am traversing all the employee elements and creating a row for each employee elements.

3)      Getting field value using loop:

<xsl:for-each select="node()">

<td style=”background-color:white”>

<xsl:value-of select=”.”/>

</td>

</xsl:for-each>

Here I am getting all the value of node/elements and resulting in a table data (column).

4)      Getting all the Node and Attributes:

<xsl:for-each select="@*|node()">

<td style=”background-color:white”>

<xsl:value-of select=”.”/>

</td>

</xsl:for-each>

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.

Advertisements