5

I'd like preceding-sibling-or-self functionality in my xpath and I'm using ../(self::*|preceding-sibling::*) and it's giving me Expression must evaluate to a node-set., so I must be doing it wrong.

This bombs no matter what context node you are on or what the xml looks like. I want to use this functionality after a / so I need the right syntax for that.

The xpath expression (self::*|preceding-sibling::*) does not give an error, so it has to do with the place in the xpath which I'm trying to use it.

EDIT:

My mistake is actually more basic. You can't even do ../(.|.) without that error. In general, I want to go to a node, then look through a set of nodes from there. The error seems to correlate with trying to use the union operator | after a slash /.

toddmo
  • 20,682
  • 14
  • 97
  • 107
  • The syntax you are trying to use is valid in XPath 2.0, but not 1.0. I don't think there is a good way to accomplish this in a single path. In what context are you using these expressions? With `XmlNode.SelectNodes()`? Or in an XSLT? – JLRishe Oct 27 '14 at 21:05
  • @JLRishe, `XmlNode.SelectNodes`. It's not pretty; I put an answer below :) – toddmo Oct 27 '14 at 23:00

1 Answers1

7

In XPath, if you want to use the union operator, you must use it at the beginning of the path.

(self::*|preceding-sibling::*)/Child is ok.

If you need to do Something/(self::*|preceding-sibling::*)/Child, you have to expand the left part out like this:

(Something/self::*|Something/preceding-sibling::*)/Child

toddmo
  • 20,682
  • 14
  • 97
  • 107