This .xq works the first time when returning db:create.  However, once the DB are created, if I run it again, files are note replaced. Instead, I get java.lang.ArrayIndexOutOfBoundsException.  I can send the DB in private if necessary for debugging. 
The result is not exactly an index, it's just flattening a few levels of indirection to make future queries faster.

I was testing this on 9.2.3.

Also a side question, is there a way to rewrite this query so not all DBs are locked? I get lock: global. I would like to lock only DBs that start with 'index-prompt-' I tried hard coding the prefix in the db:create and db:replace fonctions but that didn't help. Since I will have this running on a loop (cannot catch updated on src DB), I don't want to lock the whole system each time this query is running. It does take a good 12 seconds to run. 

(: USAGE:
for $prompt in db:attribute('index-prompt-en-us', 'setup_as_new', 'name')/..
return $prompt => prof:time()
 :)
let $langs :=
   for $lang in db:list()[string-length(.) = 5][substring(., 3, 1) = '-']
   return $lang
return
   for $lang in $langs
   let $index-db := 'index-prompt-' || $lang
   let $prompt-file-name := 'prompts-' || $lang || '.xml'
   let $index := <index>{
      for $string-group in db:open('global-content')//*[name()='string-group']
      let $prompt-this-lang := $string-group/child::*[@xml:lang=$lang][normalize-space(.)!='']
      let $prompt-base :=
         if (exists($prompt-this-lang))
         then $prompt-this-lang
         else $string-group/child::*[@xml:lang='en-us']
       let $p2 :=  db:attribute('global-content', $string-group/data(@key), 'key')/..[name()='prompt'][ancestor::*[last()]/name()='prompt-refs'][@package=$string-group/data(@package)][@pkg-version=$string-group/data(@pkg-version)]
       let $prompts := (
          for $prompt-match in $p2
          return  
                copy $copy := $prompt-base
                modify (
                   for $attr in $prompt-match/@*[name()!='package'
                      and name()!='pkg-version' and name()!='key']
                   return insert node $attr into $copy
                )
                return $copy
       )
       return
           $prompts    
   }</index>
     
   return
      if (db:exists($index-db))
      then
          db:replace($index-db, $prompt-file-name, $index)
      else
          db:create($index-db, $index, $prompt-file-name , map { 'updindex': true() })


 

--
France Baril
Architecte documentaire / Documentation architect
france.baril@architextus.com