tags:

views:

424

answers:

1

.NET allows to extend XSLT by using the so called extension object. Very handy and very convenient. You do so by creating a class:

public class VeryHandyExtensionFunctions
{
    public string VerySmartStringConcat(XPathNodeIterator NodeList)
    {
       return "some very smart string concat based on NodeList";
    }
}

pending some magic (see bellow) you can refer to VerySmartStringConcat as part of your xslt:

<xsl:value-of select="someprefix:VerySmartStringConcat(nodes[@withsomeattribute])"/>

The only thing to do in order to make it happen is to pass an instance of your extension class (VeryHandyExtensionFunctions above) to the XslCompiledTransform class, using a XsltArgumentList:

XsltArgumentList xsltArg = new XsltArgumentList();
xsltArg.AddExtensionObject("SomeUriResolvingToSomePrefix",new VeryHandyExtensionFunctions);
XslCompiledTransform xslTransform;
XmlWriter W = XmlWriter.Create(SomeTarget, Xslt.OutputSettings);
xslTransform.Transform(SomeXmlDocument, xsltArg, W);

.NET is fairly smart in figuring out how to convert the XML types to the input parameters & return types of the extension functions. However, every once in a while it complains about a not support type. What are the supported types?

+4  A: 

After some research I found this on http://msdn.microsoft.com/en-us/magazine/bb986125.aspx :

3C XPath Type -> Equivalent .NET Class (Type)
String -> System.String
Boolean -> System.Boolean
Number -> System.Double
Result Tree Fragment -> System.Xml.XPath.XPathNavigator
Node Set -> System.Xml.XPath.XPathNodeIterator

Boaz