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.
Keep up the great posts. I really like reading your articles. Thanks.
Wow, Awesome post - John Anderson ( sontek ) - was a wonderful read. I'm such a newbie when it comes to all this, Thanks for this!
Hi there, I just wanted to say That your blog is good!
It's a good thing page I would say the advice I found invaluable. We've bookmarked it and definately will come by all over again shortly after. Thanks.
It is refreshing blog This facts I stumbled onto usable. We have made note of it and can explore for a second time eventually. Appreciate it.
It's nice post I would say the data I came across advantageous. We've bookmarked it and can have a look at once again soon after. Best wishes.
Very interesting topic will bookmark your site to check if you write more about in the future.
Nice to know...
The depthof information here website is very rewarding I believe everyonecanbenefit from exploring andfinding out all the different sources of choices.
Excellent article. I found your site through Bing with the keyword "seo". You really know your stuff and I have bookmarked your site. Thanks! --------------- Visit http://www.zednome.com
Hello, I was trying to go back and read the archive for back posts but it keeps coming up 404 error. Is there something wrong?
Thanks, I needed this information. Others commenting, this is as good as it gets!
Howdy, I was browsing the net and I discovered your web site. Keep up the good work.
There is obviously a lot to know about this. I think you made some good points. Thanks and keep it up.
Another UNIVERSAL WEB PROMOTERS First Setting The Standards In Web-Promotion. Simply Deemed The NUCLEAR BLAST. And, Without A Doubt The Next Advertising Craze. No Other Company Has Ever Placed A SOLO-AD In Front Of Such A Large Audience On Over 2 BILLION High-Traffic Websites And Its Just $14.95. If This Sounds Good Visit Our Website. We Guarantee A Low-Cost And Effective Advertising Service To Fit Your Needs.
Howdy, I was searching the net and I found your web site. Keep up the good work.
If we had twice as many blogs as yours focused on adding value to the conversation, the quality of the internet blogosphere would be dramatically improved. I'd love to see that happen!