1

Is there a way to check why an xPathNavigator.evaluate() call returns no Nodes?

For example given the following xml document:

<Vehicles>
   <car>
      <wheels>4</wheels>
      <seats>5</seats>
   </car>
</Vehicles>

and the following xPath expression:

//Vehicles[((car[wheels = 4 and seats = 2]))]

I would like some sort of indication that the node was not selected because of the clause "seats = 2" not because of the number of wheels.

Is this in some way possible?

2 Answers2

2

"Why" is not the sort of question you can automate. There are countless reasons that an XPath expression would not select any nodes. What if the nodes are in the wrong namespace? What if you wrote "//vehicles" instead of "//Vehicles"?

The best you can do is define the sorts of conditions you want to check for and test them.

Dan
  • 901
  • 11
  • 25
0

the following xPath expression:

//Vehicles[((car[wheels = 4 and seats = 2]))] 

I would like some sort of indication that the node was not selected because of the clause "seats = 2" not because of the number of wheels.

Is this in some way possible?

This would be possible if you use these expressions:

//Vehicles[car[wheels = 4]]

Here if the result isn't null (or empty collection of XmlNode), then you will know that if the next expression:

//Vehicles[car[wheels = 4][seats = 2]]

selects 0 nodes, then the reason for this is that there are car children that have 4 wheels, but none of them has exactly 2 seats.

Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
  • I cannot write xPath queries myself to test it like that because the xPath query I want to test is generated at runtime. Unless there is a way to separate the "where" clauses – user1041808 Nov 11 '11 at 14:51
  • @user1041808: You should edit the question and explain that. In case you don't have control of the run-time generation of the XPath expressions, it is close to impossible to process the XPath expression and make two out of it -- unless you write your own XPath parser, of course. – Dimitre Novatchev Nov 11 '11 at 15:25