C# Extension Functions: Simple Interface

A simple extension function can be registered in SaxonCS by calling the method

processor.RegisterExtensionFunction(QName, XdmItemType, Func<XdmValue[], XdmValue>)

The first argument is the name of the function, for example new QName("urn:math", "sqrt").

The second argument is the function type. This is most conveniently provided using XPath ItemType syntax, for example processor.ParseItemType("function(xs:double?) as xs:double?").

The third argument is the implementation of the function, which can conveniently be supplied as a simple lambda expression. For example, a square-root function might be:

proc.RegisterExtensionFunction( new QName("urn:math", "sqrt"), proc.ParseItemType("function(xs:double) as xs:double"), val => new XdmAtomicValue(Math.sqrt((XdmAtomicValue)val[0][0].AsDouble()))

This can then be invoked from XPath as Q{urn:math}sqrt(2), or as m:sqrt(2) if the namespace prefix m has been bound to the URI urn:math.

The main restrictions of the simple interface are that the extension function has no access to static or dynamic context information. To avoid these restrictions, use the full interface described on the next page.