Although XSL is very powerful, I’m sometimes more comfortable with doing certain operations in C# and luckily it is pretty simple to call C# libraries from XSL.

First, you’ll need the code that you will call from XSL, this can just be a normal POCO, doesn’t have to be anything specific to XML/XSL:

 




public class StringExtension
{
public string Proper(string data)
{
CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
TextInfo textInfo = cultureInfo.TextInfo;
return textInfo.ToTitleCase(data.ToLower());
}
}

 



Then you need to use the XsltArgumentList class to add an extension object to your Xsl Transform:



class Program
{
static void Main()
{
XPathDocument doc = new XPathDocument("data.xml");
XslCompiledTransform trans = new XslCompiledTransform();
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
trans.Load("stylesheet.xslt");
XsltArgumentList args = new XsltArgumentList();
StringExtension ext = new StringExtension();
args.AddExtensionObject("ext", ext);
trans.Transform(doc, args, sw);
Console.WriteLine(sw.ToString());
Console.ReadLine();
}
}


 



Now all you have to do is register the namespace in your XSL (i.e xmlns:”ext”) and you can use the extension by calling ext:Proper():



<?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:ext="ext"
>
<xsl:output method="text" indent="yes"/>
<xsl:template match="/">
<xsl:for-each select="People/Person">
First Name:

Last Name:

</xsl:for-each>
</xsl:template>
</xsl:stylesheet>