XSLT transformation with c#


Figure 1 - XSL Transforming an XML Data File based on Radio Selection
XSL (eXtensible Stylesheet Language) is a powerful XML language that enables you to specify how you want your XML data to appear on your Web Page. Table 1 shows a list of common XSL commands and what they do:
In our ASP.NET application we want to take a list of customers and display them nicely on a web page. The XSL code in listing 1a cycles through each customer in the XML customer data shown in listing 1b and transforms the information into a readable HTML Table (listing 2). The transform uses the XSL for-each select command to loop and builds the table row by row. Within the loop, the transform fills the appropriate customer information using the XSL value-of select command, selecting on each Customer node in the XML file.

If the XSL transform in listing 1a is applied to the XML Data in listing 1b, the HTML table output in listing 2 results:
Listing 2 - Output resulting from the xsl transform of listing 1a applied to xml data in listing 1b
Using ASP.NET to Transform XML
ASP.NET comes with an XML control that allows you to transform an XML file using XSL. You just need to do 2 things: (1) assign an XmlDocument containing the XML you want to transform to the XML control's Document property. (2) assign an XslTransform class containing the XSL to the Transform property of the XML control.
Listing 3 - Making the transform happen using the Xml class
// get the physical directory path of the root default page's directory
string path = Path.GetDirectoryName(Request.PhysicalPath);
// Create the Xml Document
XmlDocument xmlDoc = new XmlDocument();
// load the xml customer database from the customers.xml file
xmlDoc.Load(String.Format("{0}/Docs/{1}", path, "customers.xml"));
If we just wanted to use the existing XSL contained in the customerformat.xsl file, then the process of retrieving the XSL is fairly straightforward. Simply construct the XslTransform object and call the Load method, just like we did with the XmlDocument.


Listing 5 - Loading a StyleSheet into an XslTransform object
// by loading it indirectly with an XmlReader
XslTransform xslTransform = new XslTransform();
xslTransform.Load(String.Format("{0}/Docs/{1}",path, Session["TransformFile"]));
However, we would like to do something a bit more interesting. We want to be able to manipulate the XSL in memory. Therefore we need a way to read the XSL, convert it to a string, alter it, and then shove the string into an XslTransform object. This turns out to be a little less straightforward. The XSL we want to load into memory is shown below in listing 6. Note that this XSL differs from the XSL in listing 1a with just one xsl command, xsl:if. We want to be able to dynamically replace the expression with a boolean expression of our choosing, such as . In the case of our ASP.NET program, we want to replace the expression {0} with something that is always true (such as the boolean expression 1 > 0) when Employees Only is not checked, but we want to filter on the condition 'Employee='yes' when Employees Only is checked. This way, all workers (consultants and employees) will be shown when the employees checkbox is not checked, but only employees will be shown when the employees check box is checked.

Listing 6 - XSL Transform with xsl:if to produce a table based on a given condition

In order to read the XSL into a string that we can manipulate in memory we will use a StreamReader. Then we'll replace the {0} in the string with the appropriate boolean expression using the string Replace method. Next we'll read the string into a memory stream and then use the memory stream to construct an XmlTextReader containing the string. Finally, we will construct the XslTransform object with the XmlTextReader containing the manipulated XSL string. As convoluted as this sounds, it is a workable approach. However, it would have been faster if there were a XslTransform method you could call that took a string containing the XSL.

Listing 7 - Dynamically changing XSL inside a string and loading the string into and XslTransform object

// need to go through a bit of a convoluted task// to load the transform with a string// by loading it indirectly with an XmlReader
XslTransform xslTransform = new XslTransform();

xslTransform.Load(String.Format("{0}/Docs/{1}",path, Session["TransformFile"]));

StreamReader sr = new StreamReader(String.Format("{0}/Docs/{1}", path, Session["TransformFile"]));

string xslBlock = sr.ReadToEnd();

sr.Close();
// alter the 'xsl:if' statement in the xsl string // to test for filtering on employees

if (chkEmployee.Checked)

{

// if 'only employees' is checked,

// put a condition to test whether or not

// we have an employee

// in the xsl:if expression

xslBlock = xslBlock.Replace("{0}", "Employee = \'yes\'");

}

else

{

// if 'only employees' is not checked,

// put a condition that is always true (1>0)

// in the xsl:if expression

xslBlock = xslBlock.Replace("{0}", "1 > 0");

}
// need to convert the xsl string to a memory stream

// in order to put it in the xml reader.

MemoryStream ms = new MemoryStream(System.Text.ASCIIEncoding.ASCII.GetBytes(xslBlock));

XmlReader reader = new XmlTextReader(ms);

// load the tranform with the XmlReader containing

// the xsl transform string

xslTransform.Load(reader);

Conclusion
Although XSL may seem complex, it is actually a useful language for taking an XML file containing data and presenting it effectively on a web page. This article has shown you how you can take advantage of the features in ASP.NET to alter XSL on the fly in response to input from the web browser and display your XML data differently. So add a bit of style to your ASP.NET apps, to C a sharper view of your data with a combination of stylesheets and .NET.








1 comments:

ramesh (visit their site)

Hi,
i have the same requirement in my project could you please give me the source of this project.
it's great help for me.
please send me code to chrames@gmail.com

Thanks in advance