Marklogic如何在抛出捕获异常后继续循环

Marklogic How to continue looping after throw catching exception

我想知道如何在抛出异常和文档总计失败后继续循环。

示例:文档 010291.xml 在计数 4000 处失败并再次继续循环。

xquery version "1.0-ml";
try {
  let $uris := cts:uris((),(),
                 cts:and-query(
                   cts:collection-query("/TRA")
                 )
  )[1 to 200000]

  for $uri in $uris
  return    
    if (fn:exists(doc($uri))) then ()
    else $uri,

  xdmp:elapsed-time()
} catch($err) { 
  "received the following exception: ", $err
}

很可能有更好的方法来执行您尝试使用该代码执行的任何操作,但特别是要在出现错误的情况下完成迭代,您需要应用 try/catch在 for 下方,您希望抛出异常的调用周围:

let $uris := 
  cts:uris((),'limit=200000',
    cts:and-query(
     cts:collection-query("/TRA")
    ))
for $uri in $uris
let $result :=
  try { fn:exists(doc($uri)) }
  catch($err) { $err }
return
  typeswitch($result)
  case element(error:error) return ("received the following exception: ", $result)
  default return $result
, 
xdmp:elapsed-time()

使用 try/catch 会产生一些开销,因此您可能会注意到由于对序列中的每个项目都调用一次,所以此查询变慢了。

将 try-catch 语句放入循环中

xquery version "1.0-ml";

let $uris := cts:uris((),(),
               cts:and-query(
                 cts:collection-query("/TRA")
               )
)[1 to 200000]

for $uri in $uris
return
  try{(
        if (fn:exists(doc($uri))) 
        then ()   
        else $uri,
        xdmp:elapsed-time()
      )
  } catch($err) { 
    "received the following exception: ", $err
  }