如何以编程方式在 XPathExpression 实例中使用 XPath 函数?

How to use XPath function in a XPathExpression instance programatically?(如何以编程方式在 XPathExpression 实例中使用 XPath 函数?)

本文介绍了如何以编程方式在 XPathExpression 实例中使用 XPath 函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前的程序需要以编程方式创建一个 XPathExpression 实例以应用于 XmlDocument.xpath 需要使用一些 XPath 函数,例如ends-with".但是,我找不到在 XPath 中使用ends-with"的方法.我

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

它抛出如下异常

未处理的异常:System.Xml.XPath.XPathException:命名空间管理器或 XsltC ontext需要.这个查询有一个前缀,变量,或用户定义的函数.
在MS.Internal.Xml.XPath.CompiledXpathExpr.get_QueryTree()在System.Xml.XPath.XPathNavigator.Evaluate(XPathExpressionexpr,XPathNodeIt 运行器上下文)
在System.Xml.XPath.XPathNavigator.Evaluate(XPathExpression表达式)

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)

代码是这样的:

    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);

我尝试在编译表达式时更改代码以插入 XmlNamespaceManager,如下所示

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);

XPathExpression.Compile 调用失败:

It fails on XPathExpression.Compile invocation:

未处理的异常:System.Xml.XPath.XPathException:此查询需要 XsltContext因为一个未知的功能.在MS.Internal.Xml.XPath.CompiledXpathExpr.UndefinedXsltContext.ResolveFunction(字符串前缀,字符串名称,XPathResultType[] ArgTypes) 在MS.Internal.Xml.XPath.FunctionQuery.SetXsltContext(XsltContext上下文)在MS.Internal.Xml.XPath.CompiledXpathExpr.SetContext(XmlNamespaceManagernsM 经理)在System.Xml.XPath.XPathExpression.Compile(字符串xpath, IXmlNamespaceResolvernsResolver)

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)

有人知道在 XPathExpression.Compile 中使用现成的 XPath 函数的技巧吗?谢谢

Anybody know the trick to use off-the-shelf XPath functions with XPathExpression.Compile? Thanks

推荐答案

函数 ends-with() 没有为 XPath 1.0 但仅适用于 XPath 2.0 和 XQuery.

The function ends-with() is not defined for XPath 1.0 but only for XPath 2.0 and XQuery.

您正在使用 .NET..NET 目前尚未实现 XPath 2.0、XSLT 2.0 或 XQuery.

You are using .NET. .NET at this date does not implement XPath 2.0, XSLT 2.0 or XQuery.

可以很容易地构造一个 XPath 1.0 表达式,其计算结果与函数 ends-with() 相同:

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)

产生与以下相同的布尔结果(true() 或 false()):

produces the same boolean result (true() or false()) as:

ends-with($str1, $str2)

在您的具体情况下,您只需将正确的表达式替换为 $str1 和 $str2.因此,它们是 /myXml/data 和 'World'.

In your concrete case you just need to substitute the right expressions for $str1 and $str2. They are, accordingly, /myXml/data and 'World'.

所以,要使用的 XPath 1.0 表达式,即等同于 XPath 2.0 表达式 ends-with(/myXml/data, '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
             )

这篇关于如何以编程方式在 XPathExpression 实例中使用 XPath 函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何以编程方式在 XPathExpression 实例中使用 XPath 函数?

基础教程推荐