Creating an XSL Extension in C#

Tags: c# xml xsl

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: <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
2010-03-05 07:30:17 - RegCure Reviews

I really loved reading this write-up. I'll be returning to look over some more interesting views. Bless you.

2010-03-05 08:45:20 - free ovulation calendar

Thanks for sharing the - but the links seems to be down? Does anybody know what it was about?

2010-03-05 10:19:40 - Dayton Carelio

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.

2010-03-05 14:39:14 - tarcica

Thanks for sharing, please keep an update about this info. love to read it more. i like this site too much. Good theme ;).

2010-03-05 20:06:52 - Christian Weyman

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

2010-03-06 01:27:33 - Daniel Millions

Finally someone that actually knows what they are talking about - thank you!

2010-03-06 03:28:23 - Cody Wetherill

Well said, you just earned another subscriber

2010-03-06 09:12:11 - Carene Dayler

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.

2010-03-06 19:57:28 - moon in my room

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.

2010-03-07 15:06:20 - moon in my room

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.

2010-03-07 19:58:26 - Buy PS3 Games

I couldn't agree more.

2010-03-08 02:47:39 - free downloading movies online

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!

2010-03-08 02:52:06 - Clarke

This blog post was interesting. I am looking forward for your next blog post. Thanks again, Mona Kennedy ~ pocketpeple

2010-03-08 04:53:46 - ghd hair straighteners discount

Very good site! I am loving it!! Will keep in mind– taking you feeds also, Regards

2010-03-08 10:35:05 - Wilton Greenleaf

Paris, France has to be my favorite vacation destination. Each time I visit I discover new things that I fall in love with.

2010-03-08 11:19:10 - Social Bookmarking

A great example of insider knowledge! Thanks for sharing!

2010-03-08 12:43:07 - the pacific episode 1

Ola.Hello there.Sorry but my english isn't good.however i wish to tell that i like this post so much.Thanxxx

2010-03-08 16:36:09 - seo tools

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.

2010-03-08 19:18:44 - straighteners

Hi, love the article, good information, and well written, thanks once again.

2010-03-09 07:22:45 - Miriam McDonald

Brother: Did you put the cat out ? Sister: Why, is it on fire ?

2010-03-09 11:34:07 - Ujemanje partnerjev

Form some cause only one-half of the site is being showed. Is this my browser causing it or is it the website?

2010-03-10 06:06:36 - Julian

Thanks for write-up, it looks like you got it all figure out, no? John Anderson ( sontek ) - was a wonderful read.

2010-03-10 09:48:09 - windows xp registry cleaner

Keep up the great posts. I really like reading your articles. Thanks.

2010-03-10 15:24:14 - Ricky

Wow, Awesome post - John Anderson ( sontek ) - was a wonderful read. I'm such a newbie when it comes to all this, Thanks for this!

2010-03-10 17:18:11 - Bart Rannalli

Hi there, I just wanted to say That your blog is good!

2010-03-10 19:36:24 - reverse white pages

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.

2010-03-11 03:35:29 - reversewhite

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.

2010-03-11 05:04:25 - reversewhitepages

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.

2010-03-11 05:40:06 - Charles

Very interesting topic will bookmark your site to check if you write more about in the future.

2010-03-11 10:40:45 - Zachariah Cabanes

Nice to know...

2010-03-11 12:46:54 - Tomeka Waldmann

The depthof information here website is very rewarding I believe everyonecanbenefit from exploring andfinding out all the different sources of choices.

2010-03-12 08:31:16 - seo directory

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

2010-03-12 20:43:46 - nj scrap metal

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?

2010-03-13 05:33:18 - Lajuana Grabowsky

Thanks, I needed this information. Others commenting, this is as good as it gets!

2010-03-13 06:36:54 - love calculator

Howdy, I was browsing the net and I discovered your web site. Keep up the good work.

2010-03-13 20:59:07 - Online Games

There is obviously a lot to know about this. I think you made some good points. Thanks and keep it up.

2010-03-13 21:52:53 - Sales Agent

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.

2010-03-14 20:32:34 - coffee table sets

Howdy, I was searching the net and I found your web site. Keep up the good work.

2010-03-15 01:04:44 - Harley Davidson Sweatshirts

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!