Scan XQuery

I need to make a Xquery to implement a scan function working on Tamino.
I?ll try to explain the problem by an example.
I have a collection of documents like


Let ?Peter? be an author and x a number (5 for example)
I?d like to obtain the list of distinct nodes (text values) ordered by ascending that satisfy these conditions.
The list is composed by 11 distinct items:

the first five items are preceding ?Peter?,
?Peter? is the middle item
the last five items are following ?Peter?

Moreover, for each item I also must display the number of its occurrences.
A probably output could be:


How can I make the Xquery?
Any help would be appreciated.
Thanks in advance.



As far as I understand your description I would say that the following query does the job:

declare namespace xs=“

{-- determine sequence with distinct authors --}
let $q := for $a in distinct-values(input()/Authority/author) sort by (. ascending)
let $b := input()/Authority/author) [. = $a]
let $fb := $b[1]
{-- create the sequence entry with all the required attributes --}
return {($fb/@date,$fb/@ref,$fb/@type,$a)}

let $name := "Peter"
let $offset := 5
{-- determine position of “Peter” entry --}
{-- the xs:integer is required due to a bug in the Tamino implementation --}
let $pos := xs:integer(count($q[. <= $name]))
{-- determine start position relative to “Peter” entry --}
let $startPos := max((1, $pos - $offset))
{-- determine end position relative to “Peter” entry --}
let $endPos := min((count($q),$pos + $offset))
return $q[position() >= $startPos and position() <= $endPos]

The query generates a sequence with an element for each distinct author. The element holds the author name and the required attributes.
After that the query determines the position of the “Peter” entry and the start and end position relative to the “Peter” entry determined by the offset (x) value.
Finally the query returns the interval specified by the start and end position.

Best regards,


Hi Thorsten,
the xquery we must make to execute is very similar to that one that you have written (in our xquery there is also “declare default collation…”).
Our database has about a million of documents like those of Raf.
Please, could you give me a suggest to improve this xquery performances?

Thanks in advance,