In this part of my C# in XSLT series of posts, I will show an example of making a basic remoteHTTP request from some inline C# code embedded inside a XSLT stylesheet. We will fetch a RSS feed from CNET.com and display it in a simpel UL list. The post XSLT transformations have been done using Microsoft Visual Studio.
A remoteHTTP request
You probertly know what a remoteHTTP request is, even if you have never heard about by that name. Every time you enter an URL you actually do a HTTP request. You point to an URL and get “something” back. In this post we will do just that, but from within some C# code being executed in the context of a XSLT transformation.
The result – effect – is that we can expand the XML which the XSLT can transform! Actually we only need a basic XML document to start up with – the actual XML is fetched from a remote site using the remoteHTTP request. So imagine that you have some XML which comes from a CMS system – say Dynamicweb CMS – you then need to fetch a RSS feed containing news from CNET.com and output an UL list with the news items on the webpage. We need some C# method to do the job for us…
The C# remoteHTTP method
Below you can see a screenshoot of the C# remoteHTTP method (you will find a link to a ZIP file containing the source code of this and the other files at the bottom of this page).
I will not say more about this code, other than I have added som basic Exception handling, returning a DOM tree with exception details (StackTrace, Message, Source, URL).
Here are some things I suggest when coding your C# code for use in XSLT:
a) In Visual Studio create a C# web project. In a “dummy” class add create the methodes and thereby getting intellicense and syntax check of the C# code.
b) Surround your code with try-catch error handling, things will go wrong at some time, so why not be prepared for it to happen?
c) Adding your XSLT file to the same project, you in practice have one structured container for both the syntax-validated methodes (classes at a later time!) and your XSLT stylesheets!
Preparing the XSLT stylesheet for inline C# code
To be able to make use of C# methodes from XSLT you need to do three things other than coding your C# code:
1) Add the namespaces and references making it possibel to execute C# code
We need to declare two namespaces for “urn:scemas-microsoft-com:xslt” and “urn:custom-cs”. At the same time I do not wish to return any elements with that namespace, so I add it to the “exclude-result-prefixes” of the stylesheet element.
2) Insert the C# code with the needed C# namespaces and assemblies
3) Executing the C# and recieving the result
In our example we define a variable called “feed”, and it will contain the XMLDocument which is located at the URL at http://news.cnet.com/2547-1_3-0-20.xml. It is a valid RSS 2.0 news feed, and the result will be a 100% valid XML DOM tree. With that we can do any kind of normal XSLT transformation – as you can see in line 75. Here I select the child element “rss” of within the variable ($feed). We have succeded in fetching an external RSS feed from within a XSLT stylesheet – allowing us in a very dynamic web to execute .NET code based on dynamic values!
As I mentioned in the first post “Using C# in XSLT transformations – Very strong tool!” I will be writing about the pros and cons when using this approach, so far I have been very positive, but ofcause nothing comes from free! There is a price to pay – more about that in a later post. For now: Enjoy this “new” way to enrich and add power to XSLT! I do!
You may download ZIP file using the link below, it contains a visual studio project which contains all the files you need, and a SIMPEL how-to page:
Basic XML file which you can point to as your source XML file from Mictosoft Visual Studio when doing the transformation.
The XSLT stylesheet containing the C# code
An example output of a transformation done in Mictosoft Visual Studio
A C# class which I used for coding the C# code