Monday, January 10, 2011

Calling C# function in XSL

In my current project, someone was looking for calling a C# function in XSL transformation. The following example calls a C# function declared in an external assembly for XSL transformation. DollerEuroConverter class is defined in the assembly CurrencyConverter.dll. The GetEuroValue function is just a sample implementation to show how the data from xsl can be modified using C# function. I tested it using a XML Notepad, I got the following output.

clip_image002

CurrencyConverter class

using System;
namespace CurrencyConverter
{
//our custom class
public class DollerEuroConverter
{
//function that gets called from XSLT
public string GetEuroValue(string xslDollar)
{
return (float.Parse(xslDollar) / 0.749962502).ToString("##.##");
}

}
}


CD.xml (Sample from w3schools)




<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
<cd>
<title>Greatest Hits</title>
<artist>Dolly Parton</artist>
<country>USA</country>
<company>RCA</company>
<price>9.90</price>
<year>1982</year>
</cd>
<cd>
<title>Still got the blues</title>
<artist>Gary Moore</artist>
<country>UK</country>
<company>Virgin records</company>
<price>10.20</price>
<year>1990</year>
</cd>
</catalog>


CD.xsl




<?xml version="1.0" encoding="utf-16"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:MyClass="CurrencyConverter.DollerEuroConverter">


<msxsl:script language="C#" implements-prefix="MyClass">
<msxsl:assembly href=".\bin\Debug\CurrencyConverter.dll" />
<msxsl:using namespace="CurrencyConverter"/>

<![CDATA[

public string GetPrice(string data)

{
CurrencyConverter.DollerEuroConverter conveter = new CurrencyConverter.DollerEuroConverter();
string days = conveter.GetEuroValue(data);
return days;

}

]]>

</msxsl:script>

<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
<th>Price in Euro</th>
</tr>
<xsl:for-each select="catalog/cd">
<tr>
<td>
<xsl:value-of select="title"/>
</td>
<td>
<xsl:value-of select="artist"/>
</td>
<td>
<xsl:value-of select="MyClass:GetPrice(price)"/>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

2 comments:

  1. I really appreciate this post. I looked at several others that just didn't measure up. Thank you.

    By the way, xmlns:MyClass="CurrencyConverter.DollerEuroConverter" might mislead some folks. It might be better to use xmlns:MyNamespaceAcronym="http://www.mydomain.com/MyNamespaceAcronym"

    ReplyDelete
  2. Kindly share the dll file also.... That will help....

    ReplyDelete