In XSLT, is there a way to determine where you are in an XML document when processing an element?
Example: Given the following XML Doc Fragment...
&l
The currently accepted answer will return incorrect paths. For example, the element Ele2 in the OP sample XML would return the path /Doc[1]/Ele2[2]. It should be /Doc[1]/Ele2[1].
Here's a similar XSLT 1.0 template that returns the correct paths:
Here's an example that will add a path attribute to all elements.
XML Input
XSLT 1.0
XML Output
Here's another version that only outputs the positional predicate if it's needed. This example is also different in that it's just outputting the path instead of adding an attribute.
using the input above, this stylesheet outputs:
/Doc
/Doc/Ele1
/Doc/Ele1/Ele11
/Doc/Ele1/Ele11/Ele111
/Doc/Ele1/Ele11/Ele111/foo[1]
/Doc/Ele1/Ele11/Ele111/foo[2]
/Doc/Ele1/Ele11/Ele111/bar[1]
/Doc/Ele1/Ele11/Ele111/foo[3]
/Doc/Ele1/Ele11/Ele111/foo[4]
/Doc/Ele1/Ele11/Ele111/bar[2]
/Doc/Ele1/Ele11/Ele111/bar[3]
/Doc/Ele2