JoeGeeky
JoeGeeky

Reputation: 3796

Compiling XPath expression in C# leads to 'invalid token' exception

I am trying to compile an XPath expression in C# but I keep getting the below exception

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

The above exception comes from the below code

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

Can someone help identify the problem with the XPath statement. The problem appears to revolve around /acchistory/count(ah[position()<=3 and (self::ah/@pay>following-sibling::ah[1]/@pay)])

Upvotes: 1

Views: 563

Answers (3)

har07
har07

Reputation: 89285

Function invocation in path step is not supported in XPath 1.0 i.e /acchistory/count(...) in your XPath. There might be a solution to accomplish what you wanted to accomplish in one XPath, but the XPath will likely become complicated then, as it is quite complex to start with.

Upvotes: 2

Kirhgoph
Kirhgoph

Reputation: 415

Maybe you should add a '@' symbol before your xpath like that:

@"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)]))"

Upvotes: 1

wst
wst

Reputation: 11773

It's possible that you need to escape the angle bracket. Replace < with &lt;.

Upvotes: 1

Related Questions