I have the following XML source structure:
The easiest way to do it in XSLT is probably to use programming language bindings, so that you can define your own XPath functions.