1

I am using BaseX version 8.6.6 i am getting the error while updating database

expression must all be updating or return empty sequence

below is the code:

declare  %private %updating function local:ingest-job()
{
  
let $contentpath := 'D:\2019\bloomsbury-ingest-content\TEI.zip'
let $archive := file:read-binary($contentpath)

for $entry in archive:entries($archive)[fn:ends-with(., '.xml')]
let $rootNode := fn:name(fn:parse-xml(archive:extract-text($archive, $entry))/*)
return
  let $docId := fn:parse-xml(archive:extract-text($archive, $entry))/*/@xml:id/string()[$rootNode='TEI']
  let $cid := fn:replace($docId,'[a-zA-z-]','')
  let $jobID := fn:concat($cid,'-',fn:string(fn:format-dateTime(fn:current-dateTime(), '[Y0001][M01][D01][H01][m01][s01][f01]')))
  let $jobChunk := <job>
    <job-info>
      <id>{$jobID}</id>
      <cid>{$cid}</cid>
    </job-info>
  </job>
  return (
    db:add('testdb', $jobChunk, fn:concat('/jobs/',$jobID,'.xml')),
    db:output(<result><status>Success</status><message>Job created</message><jobid>{$jobID}</jobid></result>))
};

<results>{local:ingest-job()}</results>

current output:

<result>
  <status>Success</status>
  <message>Job created</message>
  <jobid>9781784604387-2019102816303069</jobid>
</result>
<result>
  <status>Success</status>
  <message>Job created</message>
  <jobid>9781784604417-2019102816303069</jobid>
</result>

expected output:

<results>
<result>
  <status>Success</status>
  <message>Job created</message>
  <jobid>9781784604387-2019102816303069</jobid>
</result>
<result>
  <status>Success</status>
  <message>Job created</message>
  <jobid>9781784604417-2019102816303069</jobid>
</result>
</results>

what is going wrong here?

c00kiemon5ter
  • 16,994
  • 7
  • 46
  • 48

2 Answers2

2

As the error message indicates you are mixing updating and non-updating expressions here. You avoid doing this within your function by using db:output(), but you do it in the main part:

<results>{local:ingest-job()}</results>

This constructs the results element and within it you have an updating function. The XQUF spec does not allow this and as BaseX tries to be standards compliant you are not allowed to do that.

You have several options how to avoid that:

  1. You only transform/add the nodes in main memory using transform expressions.
  2. You simply call local:ingest-job() instead of <results>{local:ingest-job()}</results>. This way you have no non-updating expression. However, then you will have no surrounding results element.
  3. You turn on MIXUPDATES

These options are also described within the BaseX wiki.

dirkk
  • 6,160
  • 5
  • 33
  • 51
  • Thanks for the reply dirkk . if i simply call local:ingest-job() instead of {local:ingest-job()} then how can i wrap current output within elements. – Dharmendra Kumar Singh Oct 29 '19 at 08:59
  • Thanks lot drikk if i turn on MIXUPDATES it worked {local:ingest-job()} and i removed db:output from the main part. – Dharmendra Kumar Singh Oct 29 '19 at 09:26
  • Yeah, when using the second solution you will lose the results element. I edited my answer to clarify. If you are happy with MIXUPDATES then you are certainly good to go. However, keep in mind this is a non-standard behaviour and BaseX specific (i.e. you can't simply switch to another XQuery processor) and also there are some good reasons why the spec disallows this (basically you now leave the realm of functional programming) – dirkk Oct 29 '19 at 09:45
0

If you wanted to avoid the use of the custom MIXUPDATES option, one general approach is to use your functions to collect the all the information you will need for the save but defer the actual execution to your top level call.

For your code that might look something like:

(: info required to save job for $doc map{jobID, jobChunk} :)
declare  %private function local:ingest-job($doc as document-node()) as map(*)
{
let $docId :=$doc/*/@xml:id/string()[fn:name($doc/*)='TEI']
let $cid := fn:replace($docId,'[a-zA-z-]','')
let $jobID := fn:concat($cid,'-',fn:string(fn:format-dateTime(fn:current-dateTime(), '[Y0001][M01][D01][H01][m01][s01][f01]')))
let $jobChunk := <job>
                  <job-info>
                      <id>{$jobID}</id>
                      <cid>{$cid}</cid>
                  </job-info>
              </job>
 return map{
           jobID: $jobID,
           jobChunk: $jobChunk 
         }
};

let $contentpath := 'D:\2019\bloomsbury-ingest-content\TEI.zip'
let $archive := file:read-binary($contentpath)
let $docs:=archive:entries($archive)[fn:ends-with(., '.xml')]!fn:parse-xml(archive:extract-text($archive, .))

let $jobs:=$docs!local:ingest-job(.)
return (
       $jobs!db:add('testdb', ?jobChunk, fn:concat('/jobs/',?jobID,'.xml')),
       
       update:output(<results>{
           $jobs!<result><status>Success</status><message>Job created</message><jobid>{?jobID}</jobid></result>
         }</results>)
       )
Andy Bunce
  • 171
  • 6