The more I learn XPath the more I need back refererencing
For example, when I discovered some information in some of intermediate steps, I sometimes need to use the info later. This problem actually has a workaround with a little help of ability of predicate expression also to contain XPathes also. The working solution can look something like this
/foo/item[@attrtocheck = ../../otheritem[@anotherattr]]
But the problem with this approach is that I just evaluate predicate on some criteria and can not have multiple following results based on this. Even if there are many otheritem with the same @anotherattr, I can not collect them. So the better solution would be
/foo/item[@attrtocheck]/../../otheritem[@anotherattr=BACKREFERENCING_ATTRTOCHECK_VALUE]
but as long as I know it’s not possible.
If there’s no solution in the standard, maybe there’s an extension in some tools/libraties? Actually I implement my own engine and if there’s a non-standard syntax already existing, I would adopt it.
UPDATE: After Michael Kay comment, more about why I miss the feature
Actually I’m imlementing XPath search for AST (Abstract syntax trees). I’m not the first who try to do this (web search for [Xpath AST]). This kind of searches reveals very strong relations between different parts of the tree. For example,when I found some call in some context (for example inside some other method), I would like to know whether this symbol is introduced as a virtual function and if it is then output it to the results. In this case my first search produces some previously unknown symbol (function name) and I would like to use it in the following sub-pathes (searching class declaration and its keywords). So, back referencing would be very helpful.
I think if it’s appliable to AST then there is a possibility to have such relations in other structured data.
In XQuery you can use
let .. returnto declare temporary variables. Like this:And in XPath 2 you can emulate the let expression with a for loop:
Or you can use the for loop as intended and do it without “backreference”:
And in this case since comparisons are existential and you can just use:
(and btw, I also wrote my own XPath implementation and I have the syntax ((values := /xx/a/@id)[0], /xx/b[@id = $values]) for variable declaration, with := defining the variable, and the ()[0] preventing $values from being in the returned sequence. )