Yes, you can call C# function from .xsl file. Please refer following code.
This is your input XML File:
<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>
<root>
<employee>
<firstname>Kaushal</firstname>
<lastname>Parik</lastname>
</employee>
<employee>
<firstname>Abhishek</firstname>
<lastname>Swarnkar</lastname>
</employee>
</root>
Formatting function in a C# class is like this:
public class MyXslExtension
{
public string FormatName(string name)
{
return "Mr. " + name;
}
}
Apply following xsl:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
xmlns:myUtils="pda:MyUtils">
<xsl:output method="xml" indent="yes"/>
<xsl:variable name="vQ">Mr. </xsl:variable>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="employee/firstname">
<xsl:element name="firstname">
<xsl:value-of select="myUtils:FormatName(.)" />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
And C# Functin to call Formatting function is like this:
private void button3_Click(object sender, EventArgs e)
{
XsltArgumentList arguments = new XsltArgumentList();
arguments.AddExtensionObject("pda:MyUtils", new MyXslExtension());
using (StreamWriter writer = new StreamWriter("books1.xml"))
{
XslCompiledTransform transform = new XslCompiledTransform();
transform.Load("transform.xslt");
transform.Transform("books.xml", arguments, writer);
}
}
And the out put is:
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>
<root>
<employee>
<firstname>Mr. Kaushal</firstname>
<lastname>Parik</lastname>
</employee>
<employee>
<firstname>Mr. Abhishek</firstname>
<lastname>Swarnkar</lastname>
</employee>
</root>
I have referred this link to answer your question.
Hope this will help you.
Please mark +1 if it is useful to you....