如何使用XPath函数在XPathEx pression实例编程? [英] How to use XPath function in a XPathExpression instance programatically?

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

问题描述

我目前的程序需要使用编程方式创建一个XPathEx pression实例应用到XmlDocument的。中的XPath需要使用XPath的一些功能,如结尾,以。不过,我不能找到一种方法XPath中使用目的 - 用。我

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
  必要的。这个查询有preFIX,
  可变的,或用户定义的函数。

  在
  MS.Internal.Xml.XPath.CompiledXpathExpr.get_QueryTree()
  在
  System.Xml.XPath.XPathNavigator.Evaluate(XPathEx pression
  EXPR,XPathNodeIt员上下文)

  在
  System.Xml.XPath.XPathNavigator.Evaluate(XPathEx pression
  表达式)

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)

在code是这样的:

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

我试过编译时的前pression,像下面

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

这对失败XPathEx pression.Compile调用:

It fails on XPathExpression.Compile invocation:

未处理的异常:
  System.Xml.XPath.XPathException:
  XsltContext需要为此查询
  因为未知的功能。在
  MS.Internal.Xml.XPath.CompiledXpathExpr.UndefinedXsltContext.ResolveFuncti
  上(字符串preFIX,字符串名称,
  XPathResultType [] ArgTypes)在
  MS.Internal.Xml.XPath.FunctionQuery.SetXsltContext(XsltContext
  上下文)在
  MS.Internal.Xml.XPath.CompiledXpathExpr.SetContext(的XmlNamespaceManager
  NSM anager)的
  System.Xml.XPath.XPathEx pression.Compile(字符串
  XPath的,IXmlNamespaceResolv呃
  nsResolver)

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)

有人知道使用过的,现成的XPath函数与XPathEx pression.Compile的把戏?
谢谢

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

推荐答案

函数 <$c$c>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 EX pression,评估其产生相同的结果作为函数的 结尾,用()

One can easily construct an XPath 1.0 expression, the evaluation of which produces the same result as the function ends-with():

$ str2的=子($ STR1,字符串长度($ STR1) - 字符串长度($ STR2)+1)

$str2 = substring($str1, string-length($str1)- string-length($str2) +1)

产生相同的布尔结果(真()假())为:

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

结尾,用($ STR1,$ STR2)

ends-with($str1, $str2)

在您的具体情况,你只需要替代的 $ STR1 <$ C $正确的前pressions C> $ STR2 。他们,因此, /与myXML /数据 世界

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中前pression使用,也就是相当于XPath 2.0中前pression 结尾,用(/与myXML /数据,世界)

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
             )

这篇关于如何使用XPath函数在XPathEx pression实例编程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆