May 5, 2005

Declarative way to expose methods as XSLT extension functions?

I had a conversation with somebody about how EXSLT.NET worked around the hyphenated EXSLT function names problem and if there are better ways to solve it. Here is a suggestion for Microsoft: give us more control over exposing .NET methods as extension functions and make it declarative. ...

Currently when one exposes an object as an extension object to XSLT, all its public instance and static methods become accessible as XSLT extension functions. One has no control over which methods are exposed and which aren't. Also one has no control over how object methods are exposed to XSLT:

  • no custom name, so you call a function in XSLT by its CLR name, while XSLT and CLR functions syntax requirements and naming conventions are fairly different - hence aforementioned EXSLT.NET problem: many EXSLT functions are named in XSL style - names contain hyphens, but in .NET method names cannot contain them. As a consequence in EXSLT.NET we had to resort to a MSIL level hacking.
  • no control over marshalling between CLR and XSLT type systems, which are very different
  • no control over memoization (result caching)
  • no control over code access - security is binary-grained - everybody having FullTrust can use an extension object and nobody else
  • no overrides or priority means as in XSLT 2.0 stylesheet functions
  • no arguments optionality
  • no control over function namespace. Think about it - namespace, extension function belongs to is defined not by a person who implements it, but by someone who uses it.
When working with Web Services .NET doesn't expose every public method of your class as WebMethod, right? Instead Web methods should be marked with WebMethodAttribute, which along with bunch of other attributes controls exposing Web Service interface. Wouldn't it be nice to see the same declarative approach applied to XSLT extension objects? I mean something like
/// <summary>
/// Calculates the circumference of a circle given the radius.
/// </summary>
public class Calculate
    private double circ = 0;

    [XsltExtensionFunction(Name="circumference", Memoization=true)]
    public double Circumference(double radius)
      circ = Math.PI*2*radius;
      return circ;
Benefits are evident, aren't they? You would say - dream on, who cares? Ok, but I believe that such stuff is inevitable as XML penetrates to the core of modern programming languages such as C# or Java.

Oh well, I filed above as a suggestion to the MSDN Product Feedback Center, go vote for it if you like the idea. Any comments would be appreciated too.