Xquery递归查询不设置变量

Xquery recursive query does not set variable

[使用更完整的成功与错误示例进行编辑]

在 Xquery 3 (eXist 4.7) 中,我正在使用 public API (Zotero),它使用此 GET 请求提供书目列表:https://api.zotero.org/groups/2304628/items?format=atom&content=tei&v=3

提供者将每个响应分成 25 个项目(预期为 3202,如第一个响应中所示),因此我必须使用参数在循环中获取接下来的 25、25、25...。 API 响应有助于提供完整的 URLs 参数以发出下一个请求:

 <link rel="self" type="application/atom+xml"
            href="https://api.zotero.org/groups/2304628/items?content=tei&amp;format=atom"/>
 <link rel="next" type="application/atom+xml"
            href="https://api.zotero.org/groups/2304628/items?content=tei&amp;format=atom&amp;start=25"/>
 <link rel="last" type="application/atom+xml"
            href="https://api.zotero.org/groups/2304628/items?content=tei&amp;format=atom&amp;start=3200"/>

我正在尝试构建一个查询,它递归地为 next URL 发送 GET,并且每个 'recursion' 检查 $current-url 是否相同作为 $last-url。当它们匹配时,结束递归。

以下产生错误err:XPDY0002 variable '$next-url' is not set

xquery version "3.1";

module namespace zotero="/db/apps/thema/modules/zotero";
declare namespace tei="http://www.tei-c.org/ns/1.0";
declare namespace atom = "http://www.w3.org/2005/Atom";

declare function zotero:get-recursive($current-url as xs:string)
{
  let $APIdoc := httpclient:get($current-url,true(),<headers/>)
  let $next-url := $APIdoc//atom:link[@rel="next"]/data(@href)
  let $last-url := $APIdoc//atom:link[@rel="last"]/data(@href)

  (: perform db insert from API data:)
  let $bibdoc := doc("db/apps/myapp/data/list_bibliography.xml")
  let $insert-doc := for $content in $APIdoc//atom:content
                let $x := parse-xml($content/text())
                return update insert $x//tei:biblStruct into $bibdoc//tei:listBibl

  return 
        if ($current-url = $last-url)
            then "finished"
            else zotero:get-recursive($next-url)         
};

删除递归函数成功插入数据和returns正确的next-url:

xquery 版本“3.1”;

module namespace zotero="/db/apps/thema/modules/zotero";
declare namespace tei="http://www.tei-c.org/ns/1.0";
declare namespace atom = "http://www.w3.org/2005/Atom";

declare function zotero:get-recursive($current-url as xs:string)
{
  let $APIdoc := httpclient:get($current-url,true(),<headers/>)
  let $next-url := $APIdoc//atom:link[@rel="next"]/data(@href)
  let $last-url := $APIdoc//atom:link[@rel="last"]/data(@href)

  let $bibdoc := doc("db/apps/myapp/data/list_bibliography.xml")
  let $insert-doc := for $content in $APIdoc//atom:content
                let $x := parse-xml($content/text())
                return update insert $x//tei:biblStruct into $bibdoc//tei:listBibl 
  return ($insert-doc, $next-url)
};

xquery 递归中是否存在干扰变量 setting/use 的内容?还是我的做法完全错误?

非常感谢。

我会切换到不同的 http 客户端:http://expath.org/modules/http-client/

社区推荐使用此版本,因为存在 4.1+ 版本。


declare function zotero:get-recursive($current-url as xs:string)
{
  let $response := http:send-request(<http:request href="{$current-url}" method="get" />)
  (: try catch or other error handling would be good here :)
  (: assuming status 200 :)
  let $APIdoc := $response[2]
  let $next-url := $APIdoc//atom:link[@rel="next"]/data(@href)
  let $last-url := $APIdoc//atom:link[@rel="last"]/data(@href)

  (: perform db insert from API data:)
  let $bibdoc := doc("db/apps/myapp/data/list_bibliography.xml")
  let $insert-doc := for $content in $APIdoc//atom:content
                let $x := parse-xml($content/text())
                return update insert $x//tei:biblStruct into $bibdoc//tei:listBibl

  return 
        if ($current-url = $last-url)
            then "finished"
            else zotero:get-recursive($next-url)         
};