Aboo Bolaky

{.NET, C#, Sitecore ...} Free your mind...

Registering XSLT Extension Objects with the ASP XML control

clock October 22, 2009 22:01 by author Aboo Bolaky

I once inherited a sublayout that inlcuded an asp:xml control. The asp:xml control was there to handle and display an xml feed from another system, while the rest of the sublayout concentrated on rendering related feed content from Sitecore. The presentation of the xml feed was handled via an xlst rendering.

In this particular situation, I made use use of XSL extensions in the XSLT file. Registering the XSL Extension was fairly easy.

<xslExtensions>      <extension mode="on" type="Utils.XslHelper, Utils" namespace="http://www.sitecore.net/Utils" singleInstance="true" />.....</xslExtensions>


Registering the extension in the xsl file
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:MyExtension="http://www.sitecore.net/Utils" exclude-result-prefixes="MyExtension" ><xsl:value-of select="MyExtension:HelloWorld()" />



Binding the XML control to the XML Data

<asp:Xml ID="Xml1" runat="server" TransformSource="~/xsl/CustomFile.xslt"></asp:Xml>


   1:  protected override void OnInit(EventArgs e)
   2:  {  
   3:  if (!this.IsPostBack)
   4:        {
   5:             string xmlContent = "xml data goes here"  ;              
   6:             XsltArgumentList list = new XsltArgumentList();
   7:                               
   8:             Utils.XslHelper ext= new Utils.XslHelper ();
   9:             list.AddExtensionObject("http://www.sitecore.net/Utils", ext);
  10:             Xml1.TransformArgumentList = list;
  11:   
  12:             Xml1.DocumentContent = xmlContent;
  13:        }
  14:  }


It turns out that, even though I had previously registered the xsl extension in the xslt file, I also had to register it via code as well. Otherwise, the following exception occurs at runtime.

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

Sorted!

Currently rated 3.0 by 5 people

  • Currently 3/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Comments

29. October 2009 18:00

Raul Jimenez

Hey Aboo,
You don't need step 1 (add it to the <xslExtensions> in the web.config) if you are not using the extension in a Sitecore xslt rendering.
In here you are using the extension in your own xslt which you are using in your own XML control. Then all makes sense as in the xslt file you are binding the prefix to a namespace, and in the C# code you are mapping the namespace to the actual object that implements it.
Hope you are well.

Raul Jimenez

Add comment


(Will show your Gravatar )  

  Country flag

biuquote
  • Comment
  • Preview
Loading



A b o u t M e

Annoying

Brilliant

Open and

Objective

in every way..
Only Human >>
 
"First learn computer science and all the theory.

Next develop a programming style.

Then forget all that and just hack." Carrette (1990)