www.netsi.dk

"The internet is just a layer on the real world" (don't forget that!)

Using C# in XSLT – part 2: A remoteHTTP request

In this part of my in series of posts, I will show an example of making a basic remoteHTTP request from some inline 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 system – say 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).

C# remoteHTTP method

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

The start of the XSLT template

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

The XML element containing the C# code
Within the msxsl namespace the “script” element is used. As you might guess C# is not the only supported language. I am not sure exactly which languages are supported, but jscript.net is supported. Normally I prefer javascript, but in this case I think that C# is best, as there are more help when doing the coding i the web project class. Actually I think that Microsoft is doing very little to support jscript.net.

3) Executing the C# and recieving the result

Calling the C# methode from XSLT Above you see in line 60 how I call the remoteHTTP C# method:
cs:remoteHTTP('http://news.cnet.com/2547-1_3-0-20.xml')

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:

  • testXML.xml
    Basic XML file which you can point to as your source XML file from Mictosoft Visual Studio when doing the transformation.
  • remoteHTTP.xslt
    The XSLT stylesheet containing the C# code
  • remoteHTTP.htm
    An example output of a transformation done in Mictosoft Visual Studio
  • remoteHTTP.cs
    A C# class which I used for coding the C# code

Source files from this post

Links
Share

Using C# in XSLT transformations – Very strong tool!

When I build websites in I use . At the moment it is based on XSLT 1.0 which in its XSLT nature ofcause is much stronger than HTML based , but somehow limited compared to all the nice features which are defined in XSLT 2.0.

You may however extend to the extreme the facilities by using code in XSLT! I will start writing about the things I discover in this direction, writing about pros and cons. For now I wil not give any concrete examples, only mention that I have made an XSLT templates which gets a RSS feed (does a remoteHTTP request) and displays the transformed contents on a Dynamicweb page! Very cool! Something which I actually could not do without having to code my own Dynamicweb module!

It Rocks and I will start to think this into future templates – taking into account ofcause the pros and cons of that path!! This might just prove to be the best christmas present in the IT area of my life, this year!

Share

Finally – webdav access to Dynamicweb websites!

So far if you are working on a website you had to use the backend “filemanager” or buy a FTP account for your website if you wanted to work with the files on the site. With the latest release 7,  it is now possibel to use webdav, allowing you to open and save directly on the server from your computer!

image

What you have to do is to setup a virtual path in “Management center > Web and HTTP > Webdav”. Next you need to create a connection to a networkdrive on your computer:

image

I use windows vista and the above is how it might look in that  windows vista.

You will then be promped for username and password, which is the administrator username and password. When that is done – if everything goes well – you will have a new drive which points to the “/files” section of the Dynamicweb website!

From there on you can treat the webdav based "network drive” as a normal drive! You can copy and paste from local drives, open documents directly and well everything you are used to working on local drives! Very nice improvement! Well done Dynamicweb! :-)

Share

Zen Coding – a very fast way of generating (HTML) elements in your editor!

Surfing the net is a free ride accross a living planet, offering many inspiring and amazing moments. If I were to pay for my rides on the waves of the internet I would be ruined by now! I love surfing unknown territorie, but some places keep returning value for time spend on viewing or reading the information on them. One such site is SMASHING MAGAZINE – this time I meet Zen Coding! From now on coding HTML or XML will never be the same (slow) process!

 

The princip of Zen Coding

As I experience Zen Coding it is a fast lane for inserting/handling tag-based elements. For instance XHTML and XML, which both use open (and close) tags. Lets start with this:

ul#menu>li.item$*3>a

Will result in this:

<ul id=”menu”>
   <li class="item1"><a href=""></a></li>
   <li class="item2"><a href=""></a></li>
   <li class="item3"><a href=""></a></li>
</ul>

So that one line of CSS-alike code will be replaced with the 11 lines of HTML markup! It is faster and you get HTML markup which is without typing errors! It is elegant and easy! What more can a programming nerd wish in the month of Christmas?

The rules you can use

It is actually not very difficult, if you are familiar with CSS. You could divide the typer and operators into 3 groups:

Types/operators Description Example – look above for resulting HTML
Normal CSS: Element name

ClassIdentifier

Child element

Sibling element

ul

.item1#myID

ul>li

li+li

Repeating elements “*Times” li*3
Item numbering “$” li*3.item$

That is nice! Many users would be happy for that part alone, but the show has just begun! As it turns out – the Zen Coding is a snippet related idea…

Snippets – it even gets customizable!

In the original Zen Coding idea lies build in extensibility – you can customize it with your own “abbreviations” – in the file: zen_settings.js. That file is “simply” a JSON object defining which snippets/abbreviations should be available to which language. Looking through the JSON object I found many interesting abbreviations, let me just mention a couple:

CSS:  @i   =>   @import url();

: each =>   <xsl:for-each select="">                </xsl:for-each>

I decided to write my own snippet. It was simpel, no JSON involved, as I use the ZenCoding Visual Studio AddIn for Microsoft Visual Studio 2008.

Using Zen Coding with Microsoft Visual Studio 2008

I use Microsoft Visual Studio as my prefered editor, and there has been made an addIn for it which is called “ZenCoding Visual Studio AddIn”. It is easy to install and to set it up is not so hard. The only thing you need to do is to download, run and goto the “Tools > Options” and locate the “Environment > Keyboard”:

image

Here is where you define which keypresses should invoke which command. ZenCoding Visula Studio AddInn offers two commands, and you find them by putting focus to the “Show commands containing” and enter “zen” (see above).

Select the “ZenCoding.VisualStudio.ZenCodingAddInn.Expand” and put focus to the “Press shorcut keys”. Not you simply press the keys which you wish to use as shortcut to invoke the “Expand” command! I choose “[Ctlr]+[Shift]+[Alt]+,” – pic (almost) any shortcut you like. When you are done, press OK – and voila, you can start Zen Coding!

Adding your own snippet when using ZenCoding Visual Studio AddIn

One of the nice things with the ZenCoding AddIn is the way it handles adding your own snippets/abbreviations:

image

Again you open the “Tools > Options”, locate at the bottom “ZenCoding” options. Here you simply select Snippets and click New…. From there on it is a simpel matter of selecting Category, Abbreviation and of cause: Enter the value of the expanded abbreviation!

I often need to insert a remark that here I have messed up thing – well, I might have! So I decided to create such one – a remark. A comment you might also call it appears in two different ways, so I defined it in two forms – one for HTML and one for CSS. Nice and easy!

Conclusion

It is not everyday that something like Zen Coding appears! Looking at the thoughts behind it and the way it works it is a brilliant idea. This post have focused on giving you a quick start into Zen Coding – so I have not spend so much time giving credit to the people behind the idea (Sergey Chikuyonok), behind the article which made me investigate this concept (SMASHING MAGAZINE) or the man who have actually written the AddIn for Microsoft Visual Studio 2008 (Einar Egilsson). Let me therefor end by thanking them all! A great job all of you!!

Related articles/links:

Share