I am searching for a good solution (short expression, best performance) to get the reverse order of all ancestor nodes with XQuery 3.1 on BaseX.
Now i'm using this code, to get dirA/dirA3/dirA31 for the given XML example:
xquery version "3.1" encoding "utf-8";
declare variable $files :=
<dir name="dirA">
<file name="fileA_F1"/>
<file name="fileA_F2"/>
<dir name="dirA1">
<file name="fileA1_F1"/>
<file name="fileA1_F2"/>
</dir>
<dir name="dirA2">
<file name="fileA2_F1"/>
<file name="fileA2_F2"/>
</dir>
<dir name="dirA3">
<file name="fileA3_F1"/>
<file name="fileA3_F2"/>
<file name="fileA3_F3"/>
<dir name="dirA31">
<file name="fileA31_F1"/>
<file name="fileA31_F2"/>
<file name="fileA31_F3"/>
</dir>
</dir>
</dir>;
let $path := trace(string-join($files//file[@name='fileA31_F2']/ancestor::dir/@name,'/'))
return()
and this code to get the reverse order dirA31/dirA3/dirA :
let $reversepath := trace(string-join(reverse(tokenize(string-join($files//file[@name='fileA31_F2']/ancestor::dir/@name,'/'),'/')),'/'))
I mean, is there any other XPath or XQuery expression that traverses the ancestors in the reverse order?
Annotation: name attribute values of file nodes are unique
Another suggestion:
or more generally, you asked for a function that traverses ancestors in reverse order (innermost ancestor first):