Mvp.Xml includes an similar extension function dyn2:dynamic.
object dyn2:evaluate (node-set,
string, string?)
The dyn2:evaluate function evaluates a
string as an XPath expression and
returns the resulting value, which
might be a boolean, number, string,
node set, result tree fragment or
external object.
First node-set argument provides a
context node (the first node in the
passed node-set), such that selection
paths are evaluated relative to it.
Second string argument is the XPath
expression to be evaluated. Third
optional string argument provides
namespace bindings to be used to
resolve namespace prefixes in the
XPath expression. Namespaces are
defined in the XML style, as a space
separated list of namespace
declaration attributes.
All namespace prefixes that are in
scope for the context node (or its
parent node if the context node isn't
element node) can be referenced in the
evaluated XPath expression. Note
though that relying on namespace
prefixes defined in the source XML is
very unreliable. We encourage users to
define namespace bindings explicitly
in the third argument instead.
If the node-set passed as first
argument is empty (no context node) or
the expression string passed as the
second argument is an invalid XPath
expression (including an empty
string), this function returns an
empty string. Malformed namespace
declarations in the third argument are
ignored.
Note that this function is more
limited than EXSLT's dyn:evaluate()
function. More formally:
- No context position and context size information is available.
- No variable bindings - this function is unable to evaluate XPath expressions, which contain variable references!
- No custom extension functions - only core XPath functions and all extension functions, supported by EXSLT.NET are available.
- No current node, so the expression cannot contain the current() function calls.
- No key definition information available, so the expression cannot contain the key() function calls.
- No custom decimal format definitions are avilable, so the
expression cannot contain the
fomat-number() function calls that
refer to a
definition.
There is no 1 parameter version of that extension function, because it would have no context to evaluate the expression in.
This following extension is a 1 parameter version of evaluate. The evaluation context is fixed.
public class MyExtension
{
IXPathNavigable context;
public MyExtension( IXPathNavigable context )
{
this.context = context;
}
public object Evaluate( string expression )
{
return context.CreateNavigator().Evaluate( expression );
}
}
XsltArgumentList args = new XsltArgumentList();
args.AddExtensionObject("my-ext", new MyExtension(doc));
xslt.Transform( doc, args output );