在C#中编译XPath表达式会导致“无效令牌”异常

时间:2017-03-18 20:55:28

标签: 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.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)

以上例外来自以下代码

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

有人可以帮助识别XPath语句的问题。问题似乎围绕/acchistory/count(ah[position()<=3 and (self::ah/@pay>following-sibling::ah[1]/@pay)])

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

你可能需要逃离尖括号。将<替换为&lt;

答案 2 :(得分:1)

也许你应该添加一个&#39; @&#39; 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)]))"