0

On my recently launched site I present documents, each of which has a distinct number between MS609-0001...MS609-3826. When I present a document, such as MS609-0003, a side bar should show the previous/next 5 documents in order, assuming a start from the first document in alpha-numeric order. But, looking at http://medieval-inquisition.huma-num.fr/doc/MS609-0003 the document list is:

 MS609-0001 Arnald Garnier
 MS609-0002 Guilhem de Rosengue
 > MS609-0003 Hugo de Mamiros
 MS609-0004 P Lapassa senior
 MS609-0005 Guilhem de la Silva
 MS609-0006 Pons Rainart
 MS609-0007 B de Vasega
 MS609-0008 P de Vasega
 MS609-3824 Arnaut Bartholomieu
 MS609-3825 Arnaut Ademar
 MS609-3826 Arnaud Sabater

Evidently the code is looping back through a list of documents and going to the 'end' to get enough for 10 documents. It does the same (in reverse) when you look towards the end http://medieval-inquisition.huma-num.fr/doc/MS609-3825 .

The XQuery for getting these records uses a subsequence().

let $listlength := 10
let $docprefix := "MS609"

let $list := xmldb:get-child-resources($globalvar:URIdata)[starts-with(., $docprefix)]

let $i := index-of($list, "MS609-0003.xml")

(: $listlength should be an odd number to place the active doc in the middle :)
let $listlength := if ($listlength mod 2 = 0) then ($listlength + 1) else $listlength

(: reduce list to max ODD number requested :)
let $list := subsequence($list, $i - (($listlength - 1) div 2), $listlength)

for $x in $list

order by $x ascending

return $x

I'm at a loss as to how to operatively prevent the code from returning to the end/beginning of the list.

This is XQuery 3.1 under eXist-db 3.6.

jbrehr
  • 775
  • 6
  • 19
  • I'd have to take a closer look than I have thus far, but *usually*, this class of problem is, instead of being caused by a real loop as such, caused by a query that doesn't enforce a relationship (say, searching from `/*` in a place where you want to search only `./*`) and thus has more results than intended. It's easy with that kind of bug to get `n^2` results, when you only want `n`. – Charles Duffy Dec 03 '18 at 22:17
  • Well, the key here is that the subsequence in real values is `subsequence($list, -2,11)` which means it's using a negative index number for subsequence range? – jbrehr Dec 03 '18 at 22:26
  • 1
    It turns out `xmldb:get-child-resources` is not returning an alpha-numerically sorted list, which means the index is not actually correct. I put a sort of $list to fix the index... – jbrehr Dec 03 '18 at 22:51
  • 3
    If you need to do a sort, shouldn't you be sorting the list *before* taking the desired subsequence, rather than afterwards? – Michael Kay Dec 04 '18 at 08:27

0 Answers0