Michael Kay wrote:
 - how do I use the a NodeInfo as the context for the 
expressions to be evaluated (and reuse namespaces and variables, so.)

For run-time context, it's easy to set up the dynamic context in an
XPathContext object. Never modify the context you were supplied with on the
call, always create a new one (use the saxon:highest function for guidance).
As mentioned, static context is more difficult. This is why saxon:evaluate
doesn't give you access to the variables defined in the stylesheet, instead
it gives you access to its own set of variables $p1, $p2 etc which are bound
to extra parameters in the extension function call.
Things nearly seemed to work but I forgot that the data I receive often makes use of the default namespace which makes things brake:

So using "saxon:highest(/d:value, saxon:expression('number(d:number)') )" on (with "default" mapped to the prefix "d") throws an error:

<root xmlns="default">

Is it possible to create the expression (programmatically) at runtime, so that I could basically write something like "saxon:highest(/d:value, 'number(d:number)' )" or in my case "sort:sort-by(/d:value, 'number(d:number)' ) ?