StefH / XPath2.Net

Lightweight XPath2 for .NET
Microsoft Public License
36 stars 14 forks source link

Should using a variable in XPath2Select on an XPathNavigator work? #20

Closed martin-honnen closed 5 years ago

martin-honnen commented 5 years ago

I am struggling to use a variable in XPath 2 with the XPath2Select method of an XPathNavigator taking a second argument which I thought should be an anonymous object with variable values.

When I used foreach (XPathNavigator item in doc.CreateNavigator().XPath2Select("root/list/item[foo = $v]", new { v = "test" })) I get an exception

Wmhelp.XPath2.XPath2Exception HResult=0x80131500 Message=Qname v is not defined Source=XPath2 StackTrace: at Wmhelp.XPath2.NameBinder.VarIndexByName(XmlQualifiedName name) at Wmhelp.XPath2.AST.VarRefNode.Bind() at Wmhelp.XPath2.AST.AbstractNode.Bind() at Wmhelp.XPath2.AST.AbstractNode.Bind() at Wmhelp.XPath2.AST.FilterExprNode.Bind() at Wmhelp.XPath2.AST.AbstractNode.Bind() at Wmhelp.XPath2.AST.PathExprNode.Bind() at Wmhelp.XPath2.XPath2Expression.BindExpr(IDictionary2 vars) at Wmhelp.XPath2.XPath2Expression.Evaluate(IContextProvider provider, IDictionary2 vars) at Wmhelp.XPath2.XPathNavigatorExtensions.XPath2Select(XPathNavigator nav, String xpath, Object arg)

Shouldn't that arg argument on XPathNavigatorExtensions.XPath2Select bind that variable?

Using XPath2 1.0.6.1 from NuGet.

martin-honnen commented 5 years ago

When I fix the method XPath2Select to use the passed in arg

        public static XPath2NodeIterator XPath2Select(this XPathNavigator nav, XPath2Expression expr, object arg)
        {
            return XPath2NodeIterator.Create(XPath2Evaluate(nav, expr, arg));
        }

I don't longer get the exception.

Also wrote a test case

        [Fact]
        public void XPath2_XPath2Select_Variable()
        {
            var nav = GetOrders().CreateNavigator();
            var nodeIt = nav.XPath2Select("//item[@quantity = $q]", new { q = 1 });

            Assert.Equal(5, nodeIt.Count);
        }
StefH commented 5 years ago

Fixed. New NuGet will be released today.