janv. 21

I have been speaking of XSL in my latest articles, so to conclude (for now) the serie, let's see how we can call a C# (or VB.NET) function inside of an XSL file.

Let's first remember two things

  • you should be able to do whatever you want in XSL. However, as this is a functional language, you may need to forget all what you have learned with your favorites programming languages to suceed to do what you want. For examples, the fact that XSL do not allow to update the content of a variable may render things a bit trickier if you are not used to. Calling a C# function should not necessarily be used as soon as you have a problem
  • Visual Studio.NET allows you to debug an XSL file. This is very practical to validate the transformation you do. However, as soon you will call C# code from the XSL file, this won't be possible anymore. Just think before doing it ! (well... you will be able to debug, but no longer in the XSL editor)

However, despite of these two warnings, you can imagine many scenarii when this will be usefull, starting by code factorisation for example : having a single display or rendering function for C# and XSL is of course of great interest !

So let's imagine you have an XML file like this:

<?xml version="1.0" encoding="utf-8" ?>
<Employees>
   <Employee>
      <FirstName>Pierre-Emmanuel</FirstName>
      <LastName>Dautreppe</LastName>
   </Employee>
</Employees>

And that you already have your formatting function in C# like (of course, this is a very basic example you could very easily do in XSL)

public class MyXslExtension
{
   public string FormatName(string firstName, 
                            string name)
   { 
      return name + ", " + firstName;
   }
}

It's almost finished. Now you just need to use this method in an XSL file. Let's write a very basic one

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" 
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:myUtils="pda:MyUtils">
 
<xsl:template match="/Employees">
<Employees>
   <xsl:for-each select="Employee">
   <Employee>
      <xsl:value-of select="myUtils:FormatName(FirstName, 
                                               LastName)" />
   </Employee>
   </xsl:for-each>
</Employees>
</xsl:template>
 
</xsl:stylesheet>

As you can see we have added a new namespace in our file. What is the goal ? To map a function namespace we use in the XSL code to a .NET object we will provide. Note that the function name we use corresponds exactly to the .NET function name we have declared in our object. Note that the value of the namepsace can be whatever you want and will be used later on to do the mapping.

How to use it now ? Let's see the transformation code:

string xslPath = "XSLTFile1.xslt";
string xmlPath = "XMLFile1.xml";
string outputPath = "output.xml";
 
XsltArgumentList arguments = new XsltArgumentList();
arguments.AddExtensionObject("pda:MyUtils", new MyXslExtension());
 
using ( StreamWriter writer = new StreamWriter(outputPath) )
{
   XslCompiledTransform transform = new XslCompiledTransform();
   transform.Load(xslPath);
   transform.Transform(xmlPath, arguments, writer);
}

And here it is ! When launching your code, .NET will automatically map your object to the namespace used in your XSL file.

Tags: | | | |

Commentaires

Sangeeta

Posted on samedi, 27 décembre 2008 07:21

I need to call .net code in xsl. I have used above code, page is not showing any output.  

pe.dautreppe

Posted on mardi, 30 décembre 2008 22:01

Above is my production code, working good without any problem.
Post your own code and I will check what is going on !

kiran

Posted on jeudi, 29 janvier 2009 10:53

I am getting this error after implementing above code
can you help me to remove this error .

"Cannot find the script or external object that implements prefix 'MyUtils'."

gf7788

Posted on mercredi, 22 juillet 2009 17:56

Trackback from gf7788

Extending XSLT using C# 使用自定义函数

getnaleen.wordpress.com

Posted on vendredi, 17 décembre 2010 02:48

Pingback from getnaleen.wordpress.com

XSL rendering in Sitecore « Naleen Senevirathne

Naleen

Posted on vendredi, 17 décembre 2010 02:57

This is a great post and helped me to initaie the grounds for the issue I had to work on our intranet. It is a Sitecore intranet and I had to find how I can call a C# function from XSL file. You can find more details on http://getnaleen.wordpress.com/. I should thank Pierre-Emmanuel for posting this and it certainly helped. cheers!

anyone

Posted on jeudi, 30 juin 2011 22:00

10x for the post - it was very helpful.

Nandha

Posted on jeudi, 8 décembre 2011 12:14

Hi have done as per the coding
but when doing so it throws error as
Error: Cannot find the script or external object that implements prefix 'pda:MyUtils'.
Please help me regarding this.

Marcio

Posted on jeudi, 9 août 2012 17:39

Tks for your post.

reproductive health

Posted on dimanche, 28 juillet 2013 16:05

I do not know whether it's just me or if perhaps everybody else experiencing problems with your blog. It looks like some of the written text in your content are running off the screen. Can somebody else please provide feedback and let me know if this is happening to them too? This may be a issue with my browser because I've had this happen previously. Thank you

Feel free to visit my site:  reproductive health - team-6.eng.toyo.ac.jp/practice5-6/index.php/%E5%88%A9%E7%94%A8%E8%80%85:LuellaBingham

how to gain weight fast

Posted on lundi, 5 août 2013 10:01

Superb, what a blog it is! This web site gives helpful facts to us, keep it up.

My page ::  how to gain weight fast - http://howtogainweightfast.yourcelebgossip.com/

was benigner Prostatahyperplasie

Posted on mercredi, 21 août 2013 01:47

Hello colleagues, good post and good urging commented here, I am in fact enjoying by these.

My weblog:  was benigner Prostatahyperplasie - stepsthatwillhelpkeepimpotence.com/.../

conseil+en+referencement&content=This+design+is+spectacular!+You+obviously+know+how+to+keep+a+
reader+amused.+Between+your+wit+and+your+videos,+I+was+almost+moved+to+start+my+own+blog+(well,+almost.
..HaHa!)+Great+job.+I+really+enjoyed+what+you+had+to+say,+and+more+than+that,+how+you+presented+
it.+Too+cool!

Feel+free+to+surf+to+my+blog+post+conseil+en+referencement&author=Arthur&email=arthurcobb@gmail.com&webpage=http://sophie.kjetil.free.fr/magic/forum/phpBB2/profile.php?mode=viewprofile&u=25059&er=true">Amelie

Posted on mardi, 10 septembre 2013 12:18

Thanks  for any other informative website. The place else may I get that type of information written in such a perfect manner? I have a challenge that I am just now operating on, and I have been at the glance out for such information.

Feel free to visit my web-site: google referencer un site;  Amelie - tothepope.org/.../article_page.php%21+You+obviously+know+how+to+keep+a+reader+amused.%3Cbr%3E%3Cbr%3EBetween+your+wit+and+your+videos%2C+I+was+almost+moved+to+start+my+own+blog+%3Cbr%3E%28well%2C+almost...HaHa%21%29+Great+job.+I+really+enjoyed+what+you+%3Cbr%3Ehad+to+say%2C+and+more+than+that%2C+how+you+presented+it.%3Cbr%3EToo+cool%21%3Cbr%3E%3Cbr%3EHave+a+look+at+my+web-site+-+%3Ca+href%3D%22http%3A%2F%2Fsophie.kjetil.free.fr%2Fmagic%2Fforum%2FphpBB2%2Fprofile.php%3Fmode%3Dviewprofile%26u%3D25059%22%3Econseil+en+referencement%3C%2Fa%3E&content=This+design+is+spectacular%21+You+obviously+know+how+to+keep+a+%3Cbr%3Ereader+amused.+Between+your+wit+and+your+videos%2C+I+was+almost+moved+to+start+my+own+blog+%28well%2C+almost.%3Cbr%3E..HaHa%21%29+Great+job.+I+really+enjoyed+what+you+had+to+say%2C+and+more+than+that%2C+how+you+presented+%3Cbr%3Eit.+Too+cool%21%3Cbr%3E%3Cbr%3EFeel+free+to+surf+to+my+blog+post+%3Ca+href%3D%22http%3A%2F%2Fsophie.kjetil.free.fr%2Fmagic%2Fforum%2FphpBB2%2Fprofile.php%3Fmode%3Dviewprofile%26u%3D25059%22%3Econseil+en+referencement%3C%2Fa%3E&author=Arthur&email=arthurcobb%40gmail.com&webpage=http%3A%2F%2Fsophie.kjetil.free.fr%2Fmagic%2Fforum%2FphpBB2%2Fprofile.php%3Fmode%3Dviewprofile%26u%3D25059&er=true ,

Tablet Pc Refurbished

Posted on samedi, 12 octobre 2013 21:48

Nice blog right here! Also your web site rather a lot up very fast! What host are you the usage of? Can I am getting your associate hyperlink in your host? I wish my site loaded up as fast as yours lol

Also visit my site -  Tablet Pc Refurbished - http://to.ly/pvMA

colon cleansing kit

Posted on lundi, 28 octobre 2013 06:11

This is my first time visit at here and i am really happy to read all at alone place.

my weblog;  colon cleansing kit - newhopeacupuncturemd.com/news/?document_srl=43482

build Muscle Fast Supplements

Posted on mercredi, 30 octobre 2013 11:23

These are in fact enormous ideas in regarding blogging. You have touched some good points here. Any way keep up wrinting.

My web page -  build Muscle Fast Supplements - givingbirthwithconfidence.org/.../149815

where to get raspberry ketones

Posted on mercredi, 18 décembre 2013 11:33

This design is steller! You most certainly know how to keep a reader entertained. Between your wit and your videos, I was almost moved to start my own blog (well, almost...HaHa!) Wonderful job. I really enjoyed what you had to say, and more than that, how you presented it. Too cool!

Visit my website  where to get raspberry ketones - gwilymgold.com/vanilla/profile/3815/AlexisF76

Ajouter un commentaire




biuquote
  • Commentaire
  • Aperçu immédiat
Loading