Creating an XSL Extension in C#
Tags: c# xml xslAlthough 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: <xsl:value-of select="ext:Proper(@FirstName)"/><br />Last Name: <xsl:value-of select="ext:Proper(@LastName)"/><br /></xsl:for-each></xsl:template></xsl:stylesheet>
Comments

I really loved reading this write-up. I'll be returning to look over some more interesting views. Bless you.
Thanks for sharing the - but the links seems to be down? Does anybody know what it was about?
You earned a laugh out of me. I am seeing why it's not as hard as I thought, going to just have to take the initiative and do it.
Thanks for sharing, please keep an update about this info. love to read it more. i like this site too much. Good theme ;).
Fantastic post! This could aid lots of people find out about this matter. Do you want to incorporate video clips together with these? It could undoubtedly help out. Your reason was spot on and owing to you; I probably won't have to describe everything to my pals. I can simply direct them here
Finally someone that actually knows what they are talking about - thank you!
Well said, you just earned another subscriber
I bet it's a good idea to become a part of this flash game app hype. There is a massive amount of profits to be made with flash apps on social networks right now. Did you hear they just now launched a new Farmville game for cell phones few days ago. This is what's popular now.
Thanks for taking this opportunity to talk about this, I feel strongly about it and I benefit from learning about this subject. If possible, as you gain data, please update this blog with new information. I have found it extremely useful.
Thanks for taking this opportunity to talk about this, I feel strongly about it and I benefit from learning about this subject. If possible, as you gain data, please update this blog with new information. I have found it extremely useful.
I couldn't agree more.
I just couldnt leave your website before saying that I really enjoyed the quality information you offer to your visitors... Will be back often to check up on new stuff you post!
This blog post was interesting. I am looking forward for your next blog post. Thanks again, Mona Kennedy ~ pocketpeple
Very good site! I am loving it!! Will keep in mind– taking you feeds also, Regards
Paris, France has to be my favorite vacation destination. Each time I visit I discover new things that I fall in love with.
A great example of insider knowledge! Thanks for sharing!
Ola.Hello there.Sorry but my english isn't good.however i wish to tell that i like this post so much.Thanxxx
Good to be stumbling up to your site again, it has been weeks for me. Anyway, this is the post that i've been waiting for so long. I can use this article to complete my paper in the college, and it has same topic like your article. Thank you, fantastic share.
Hi, love the article, good information, and well written, thanks once again.
Brother: Did you put the cat out ? Sister: Why, is it on fire ?
Form some cause only one-half of the site is being showed. Is this my browser causing it or is it the website?
Thanks for write-up, it looks like you got it all figure out, no? John Anderson ( sontek ) - was a wonderful read.