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.