I am looking for an example of a XQuery that returns the xpath for each node found. So for example instead of getting the text() of each found node I would like to get the path() – but that function doesn’t seem to exist 🙂
I see now that the XPath 3.0 spec defines a path() function that would do what i want, but apparently this is new to 3.0
http://www.w3.org/TR/2011/WD-xpath-functions-30-20111213/#func-path
As Martin Honnen commented, the XPath expression selecting a specific node isn’t unique. For example, the second
yelement in the following documentcould be selected (non-exclusively) by:
If you are just interested in getting some XPath expression selecting the node, the FunctX XQuery Function Library contains two functions that do that:
functx:path-to-node
returns an XPath expression that selects all nodes with the same name and
ancestor names as the node you evaluate it for.
In our example it would return
x/yfor bothyelements (mind the missing leading slash).functx:path-to-node-with-pos also includes the position of each ancestor in the document, thereby only selecting the node you pass in.
In the example, the first
yelement would producex/y[1]and the second onex/y[2].