My current program need to use programatically create a XPathExpression instance to apply to XmlDocument. The xpath needs to use some XPath functions like "ends-with". However, I cannot find a way to use "ends-with" in XPath. I
It throw exception like below
Unhandled Exception: System.Xml.XPath.XPathException: Namespace Manager or XsltC ontext needed. This query has a prefix, variable, or user-defined function.
at MS.Internal.Xml.XPath.CompiledXpathExpr.get_QueryTree() at System.Xml.XPath.XPathNavigator.Evaluate(XPathExpression expr, XPathNodeIt erator context)
at System.Xml.XPath.XPathNavigator.Evaluate(XPathExpression expr)
The code is like this:
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(@"<?xml version=""1.0"" encoding=""utf-8"" ?>
<myXml xmlns=""http://MyNamespace"" xmlns:fn=""http://www.w3.org/2005/xpath-functions"">
<data>Hello World</data>
</myXml>");
XPathNavigator navigator = xdoc.CreateNavigator();
XPathExpression xpr;
xpr = XPathExpression.Compile("fn:ends-with(/myXml/data, 'World')");
object result = navigator.Evaluate(xpr);
Console.WriteLine(result);
I tried to change the code to insert XmlNamespaceManager when compiling the expression, like below
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(@"<?xml version=""1.0"" encoding=""utf-8"" ?>
<myXml xmlns=""http://MyNamespace"" xmlns:fn=""http://www.w3.org/2005/xpath-functions"">
<data>Hello World</data>
</myXml>");
XPathNavigator navigator = xdoc.CreateNavigator();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xdoc.NameTable);
nsmgr.AddNamespace("fn", "http://www.w3.org/2005/xpath-functions");
XPathExpression xpr;
xpr = XPathExpression.Compile("fn:ends-with(/myXml/data, 'World')", nsmgr);
object result = navigator.Evaluate(xpr);
Console.WriteLine(result);
It fails on XPathExpression.Compile invocation:
Unhandled Exception: System.Xml.XPath.XPathException: XsltContext is needed for this query because of an unknown function. at MS.Internal.Xml.XPath.CompiledXpathExpr.UndefinedXsltContext.ResolveFuncti on(String prefix, String name, XPathResultType[] ArgTypes) at MS.Internal.Xml.XPath.FunctionQuery.SetXsltContext(XsltContext context) at MS.Internal.Xml.XPath.CompiledXpathExpr.SetContext(XmlNamespaceManager nsM anager) at System.Xml.XPath.XPathExpression.Compile(String xpath, IXmlNamespaceResolv er nsResolver)
Anybody know the trick to use off-the-shelf XPath functions with XPathExpression.Compile? Thanks
XPath uses path expressions to select nodes or node-sets in an XML document. These path expressions look very much like the expressions you see when you work with a traditional computer file system. XPath expressions can be used in JavaScript, Java, XML Schema, PHP, Python, C and C++, and lots of other languages.
XPath (XML Path Language) is based on a DOM representation of an XML document. Therefore, you can use XPath to find a specific node or nodes in an XML file that match some criteria defined in the XPath expression.
The local-name function returns a string representing the local name of the first node in a given node-set.
The function ends-with()
is not defined for XPath 1.0 but only for XPath 2.0 and XQuery.
You are using .NET. .NET at this date does not implement XPath 2.0, XSLT 2.0 or XQuery.
One can easily construct an XPath 1.0 expression, the evaluation of which produces the same result as the function ends-with()
:
$str2 = substring($str1, string-length($str1)- string-length($str2) +1)
produces the same boolean result (true()
or false()
) as:
ends-with($str1, $str2)
In your concrete case you just need to substitute the right expressions for $str1
and $str2
. They are, accordingly, /myXml/data
and 'World'
.
So, the XPath 1.0 expression to use, that is equivalent to the XPath 2.0 expression ends-with(/myXml/data, 'World')
is:
'World' =
substring(/myXml/data,
string-length(/myXml/data) - string-length('World') +1
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With