Some predicates do not work (i.e [1])

Topics: Developer Forum
Aug 7, 2008 at 10:02 AM
I am calling DocumentNode.SelectNodes with an xpath query that returns a number of rows. A simple example might be //div.

If I qualify the xpath expression to be //div[1] I would expect only the first node to be returned. In reality the [1] is ignored and I still get all the nodes. //div[2] on the other hand returns no nodes.

In a similar vain, //div[(last()] has no effect and all nodes are returned.

I am using the release version of the software. Has anyone fixed this problem or can advise me of a work around?
Aug 8, 2008 at 9:36 AM

I though I would verify what the Microsoft System.Xml namespace would do regarding predicates. I discoved the following:

Given a document:




//div                         returns 1, 1a, 1b, 1c,2,3
//div[1]                    returns 1,1a
//div[position()=1]    returns 1,1a
//div[last()]              returns 1c,3

Now to the best of my knowledge this is incorrect, as it is applying the predicate to each level in the structure, rather than applying it to the whole resultset.

Given the "bug" exists in the default Microsoft namespaces I am guessing it is unreasonable to expect HtmlAgilityPack to behave differently.

Any views greatfully received.