Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xpath: preceding-sibling-or-self

Tags:

.net

xpath

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 /.

like image 610
toddmo Avatar asked Oct 27 '14 20:10

toddmo


People also ask

What is preceding in XPath?

The preceding axis selects all nodes that come before the current node in the document, except ancestor, attribute nodes, and namespace nodes. Let us consider the login button as current node on the Facebook web page as shown below screenshot. Let's first, find the XPath of current node i.e XPath of login button.

What is preceding sibling and following sibling?

XPath using Preceding-Sibling This is a concept very similar to Following-Siblings. The only difference in functionality is that of preceding. So, here, in contrast to Following-Sibling, you get all the nodes that are siblings or at the same level but are before your current node.


1 Answers

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

like image 56
toddmo Avatar answered Sep 21 '22 12:09

toddmo