在C#中编译XPath表达式会导致';无效令牌';例外

在C#中编译XPath表达式会导致';无效令牌';例外,c#,xpath,xquery,C#,Xpath,Xquery,我试图用C#编译XPath表达式,但我一直得到以下异常 'max(//*[local-name()="acc"][accdetails/accgroupid="2" and accdetails/status="N"]/acchistory/count(ah[position()<=3 and (self::ah/@pay>following-sibling::ah[1]/@pay)]))' has an invalid token. at MS.Internal.Xml.X

我试图用C#编译XPath表达式,但我一直得到以下异常

'max(//*[local-name()="acc"][accdetails/accgroupid="2" and accdetails/status="N"]/acchistory/count(ah[position()<=3 and (self::ah/@pay>following-sibling::ah[1]/@pay)]))' has an invalid token.

   at MS.Internal.Xml.XPath.XPathParser.CheckToken(LexKind t)
   at MS.Internal.Xml.XPath.XPathParser.ParseMethod(AstNode qyInput)
   at MS.Internal.Xml.XPath.XPathParser.ParsePrimaryExpr(AstNode qyInput)
   at MS.Internal.Xml.XPath.XPathParser.ParseFilterExpr(AstNode qyInput)
   at MS.Internal.Xml.XPath.XPathParser.ParsePathExpr(AstNode qyInput)
   at MS.Internal.Xml.XPath.XPathParser.ParseUnionExpr(AstNode qyInput)
   at MS.Internal.Xml.XPath.XPathParser.ParseUnaryExpr(AstNode qyInput)
   at MS.Internal.Xml.XPath.XPathParser.ParseMultiplicativeExpr(AstNode qyInput)
   at MS.Internal.Xml.XPath.XPathParser.ParseAdditiveExpr(AstNode qyInput)
   at MS.Internal.Xml.XPath.XPathParser.ParseRelationalExpr(AstNode qyInput)
   at MS.Internal.Xml.XPath.XPathParser.ParseEqualityExpr(AstNode qyInput)
   at MS.Internal.Xml.XPath.XPathParser.ParseAndExpr(AstNode qyInput)
   at MS.Internal.Xml.XPath.XPathParser.ParseOrExpr(AstNode qyInput)
   at MS.Internal.Xml.XPath.XPathParser.ParseExpresion(AstNode qyInput)
   at MS.Internal.Xml.XPath.XPathParser.ParseXPathExpresion(String xpathExpresion)
   at System.Xml.XPath.XPathExpression.Compile(String xpath, IXmlNamespaceResolver nsResolver)
   at System.Xml.XPath.XPathNavigator.Compile(String xpath)
'max(/*[local-name()=“acc”][accdetails/accgroupid=“2”和accdetails/status=“N”]/acchhistory/count(ah[position()跟随兄弟姐妹::ah[1]/@pay)]具有无效令牌。
位于MS.Internal.Xml.XPath.XPathParser.CheckToken(LexKind t)
位于MS.Internal.Xml.XPath.XPathParser.ParseMethod(AstNode qyInput)
位于MS.Internal.Xml.XPath.XPathParser.ParsePrimaryExpr(AstNode qyInput)
位于MS.Internal.Xml.XPath.XPathParser.ParseFilterExpr(AstNode qyInput)
位于MS.Internal.Xml.XPath.XPathParser.ParsePathExpr(AstNode qyInput)
位于MS.Internal.Xml.XPath.XPathParser.ParseUnionExpr(AstNode qyInput)
位于MS.Internal.Xml.XPath.XPathParser.ParseUnaryExpr(AstNode qyInput)
位于MS.Internal.Xml.XPath.XPathParser.ParseMultiplicativeExpr(AstNode qyInput)
位于MS.Internal.Xml.XPath.XPathParser.ParseAdditiveExpr(AstNode qyInput)
位于MS.Internal.Xml.XPath.XPathParser.ParseRelationalExpr(AstNode qyInput)
位于MS.Internal.Xml.XPath.XPathParser.ParseEqualityExpr(AstNode qyInput)
位于MS.Internal.Xml.XPath.XPathParser.ParseAndExpr(AstNode qyInput)
位于MS.Internal.Xml.XPath.XPathParser.ParseOrExpr(AstNode qyInput)
位于MS.Internal.Xml.XPath.XPathParser.parsexpresion(AstNode qyInput)
位于MS.Internal.Xml.XPath.XPathParser.ParseXPathExpresion(字符串xpathExpresion)
位于System.Xml.XPath.XPathExpression.Compile(字符串XPath,IXmlNamespaceResolver nsResolver)
位于System.Xml.XPath.XPathNavigator.Compile(字符串XPath)
上述异常来自以下代码

public object ParseCreditReportResponse(XmlDocument document)
{
    var navigator = document.CreateNavigator();
    var expr = navigator.Compile("max(//*[local-name()=\"acc\"][accdetails/accgroupid=\"2\" and accdetails/status=\"N\"]/acchistory/count(ah[position()<=3 and (self::ah/@pay>following-sibling::ah[1]/@pay)]))");

    return expr;
}
公共对象ParseCreditReportResponse(XmlDocument) { var navigator=document.CreateNavigator(); var expr=navigator.Compile(“max(//*[local-name()=\“acc\”][accdetails/accgroupid=\“2\”和accdetails/status=\“N\”]/acchhistory/count(ah[position()跟随兄弟姐妹::ah[1]/@pay)]); 返回表达式; }
有人可以帮助识别XPath语句的问题吗。问题似乎围绕着
/acchhistory/count(ah[position()后面的兄弟姐妹::ah[1]/@pay)])

您可能需要退出角括号。更换
可能需要退出角括号。替换
也许您应该在xpath之前添加一个“@”符号,如下所示:

@"max(//*[local-name()=\"acc\"][accdetails/accgroupid=\"2\" and accdetails/status=\"N\"]/acchistory/count(ah[position()<=3 and (self::ah/@pay>following-sibling::ah[1]/@pay)]))"
@“max(/*[local-name()=\“acc\”][accdetails/accgroupid=\“2\”和accdetails/status=\“N\”]/acchhistory/count(ah[职位()跟随兄弟姐妹::ah[1]/@pay)])

也许您应该在xpath之前添加一个“@”符号,如下所示:

@"max(//*[local-name()=\"acc\"][accdetails/accgroupid=\"2\" and accdetails/status=\"N\"]/acchistory/count(ah[position()<=3 and (self::ah/@pay>following-sibling::ah[1]/@pay)]))"
@“max(/*[local-name()=\“acc\”][accdetails/accgroupid=\“2\”和accdetails/status=\“N\”]/acchhistory/count(ah[职位()跟随兄弟姐妹::ah[1]/@pay)])

XPath 1.0不支持路径步骤中的函数调用,即XPath中的
/acchhistory/count(…)
。可能有一种解决方案可以在一个XPath中完成您想要完成的任务,但是XPath很可能会变得复杂,因为它一开始就非常复杂。

XPath 1.0不支持在路径步骤中调用函数,即XPath中的
/acchhistory/count(…)
。可能有一种解决方案可以在一个XPath中实现您想要实现的目标,但是XPath很可能会变得复杂,因为它一开始就非常复杂。

XPath表达式正在计算一组计数的最大值,这超出了XPath 1.0的能力,但是您尝试编写它。您需要切换到XPath 2.0处理器——有几种可用于C#的处理器,例如XmlPrime和Saxon。(唯一的替代方法是用C#而不是XPath完成大部分工作)。XPath表达式计算一组计数的最大值,这超出了XPath 1.0的能力,但是您尝试编写它。您需要切换到XPath 2.0处理器——有几种可用于C#的处理器,例如XmlPrime和Saxon。(唯一的选择是用C#而不是XPath完成大部分工作)。