XPTY0004 error when calling http:send-request inside of a local function

Sunday, 11 October 2020 · 4 messages · last reply 12 Oct 2020
CG
Christian Grünin reply to Bridger Dyson-Smith, 11 Oct 2020, 23:4511 Oct 2020 22:06 #
Hi Bridger, As your query will either return an empty sequence (in the case of success) or a string (in the case of an error), you’ll need to use xs:string? as return type:
declare function local:grab(
  $url as xs:string,
  $path as xs:string,
  $fname as xs:string
) as xs:string? {
  ...
Hope this helps, cheers, Christian
Quoted text · Bridger Dyson-Smith · 17 lines
> let $req := http:send-request(<http:request method="get"/>, $url) > return > if (head($req)/@status = "200") > then file:write($path || $fname, tail($req), map { "method": "xml" }) > else ("something went wrong!") > }; > ``` > > but I've noticed that in version 9.4.3 this returns an XPTY error: > Stopped at /home/bridger/bin/basex-src/basex-943-xpty0004-error.xq, 1/18: > [XPTY0004] Cannot promote xs:string* to document-node(): (let $req_7 := http:send-request(<http:request method="get"/>, "https://dpla.lib.utk.edu/repox/OAIHa.... > > While this could be a bug, should I be writing this kind of local function differently? Or could I improve my typing on it? > > Thanks very much for any advice or insights you can provide. > Best, > Bridger
BD
Bridger Dyson-Smith11 Oct 2020 23:45 #
Hi all - I'm accustomed to writing something like the following:
declare function local:grab(
  $url as xs:string,
  $path as xs:string,
  $fname as xs:string
) as document-node() {
  let $req := http:send-request(<http:request method="get"/>, $url)
  return
    if (head($req)/@status = "200")
    then file:write($path || $fname, tail($req), map { "method": "xml" })
    else ("something went wrong!")
};
but I've noticed that in version 9.4.3 this returns an XPTY error: Stopped at /home/bridger/bin/basex-src/basex-943-xpty0004-error.xq, 1/18: [XPTY0004] Cannot promote xs:string* to document-node(): (let $req_7 := http:send-request(<http:request method="get"/>, " https://dpla.lib.utk.edu/repox/OAIHa.... While this could be a bug, should I be writing this kind of local function differently? Or could I improve my typing on it? Thanks very much for any advice or insights you can provide. Best, Bridger
BD
Bridger Dyson-Smith12 Oct 2020 00:03 #
And naturally I forgot to mention...
Quoted text · Bridger Dyson-Smith · 28 lines
On Sun, Oct 11, 2020 at 3:49 PM Bridger Dyson-Smith <bdysonsmith at gmail.com> wrote: > Hi all - > > I'm accustomed to writing something like the following: > > ```xquery > > declare function local:grab( > $url as xs:string, > $path as xs:string, > $fname as xs:string > ) as document-node() { > let $req := http:send-request(<http:request method="get"/>, $url) > return > if (head($req)/@status = "200") > then file:write($path || $fname, tail($req), map { "method": "xml" }) > else ("something went wrong!") > }; > ``` > > but I've noticed that in version 9.4.3 this returns an XPTY error: > Stopped at /home/bridger/bin/basex-src/basex-943-xpty0004-error.xq, 1/18: > [XPTY0004] Cannot promote xs:string* to document-node(): (let $req_7 := > http:send-request(<http:request method="get"/>, " > https://dpla.lib.utk.edu/repox/OAIHa.... > > that despite the error, the correct response is written to the filepath
specified.
Quoted text · Bridger Dyson-Smith · 5 lines
While this could be a bug, should I be writing this kind of local function > differently? Or could I improve my typing on it? > > Thanks very much for any advice or insights you can provide. >
Apologies for the omission!
Quoted text · Bridger Dyson-Smith · 3 lines
> Best, > Bridger >
BD
Bridger Dyson-Smithin reply to Christian Grün, 11 Oct 2020, 22:0612 Oct 2020 00:15 #
Christian, As always, thank you for your help! I had tried the following
declare function local:grab2(
  $url as xs:string,
  $path as xs:string,
  $fname as xs:string
) as document-node() {
  let $req := http:send-request(<http:request method="get"/>, $url)
  return
    if (head($req)/@status = "200")
    then file:write($path || $fname, tail($req), map { "method": "xml" })
    else document { element error { "something went wrong" } }
};
and had the same error - adding an occurrence indicator on the return type solves it for that trial, too. Much appreciated! Best, Bridger
Quoted text · Christian Grün · 41 lines
On Sun, Oct 11, 2020 at 4:06 PM Christian Grün <christian.gruen at gmail.com> wrote: > Hi Bridger, > > As your query will either return an empty sequence (in the case of > success) or a string (in the case of an error), you’ll need to use > xs:string? as return type: > > declare function local:grab( > $url as xs:string, > $path as xs:string, > $fname as xs:string > ) as xs:string? { > ... > > Hope this helps, cheers, > Christian > > > > > let $req := http:send-request(<http:request method="get"/>, $url) > > return > > if (head($req)/@status = "200") > > then file:write($path || $fname, tail($req), map { "method": "xml" }) > > else ("something went wrong!") > > }; > > ``` > > > > but I've noticed that in version 9.4.3 this returns an XPTY error: > > Stopped at /home/bridger/bin/basex-src/basex-943-xpty0004-error.xq, 1/18: > > [XPTY0004] Cannot promote xs:string* to document-node(): (let $req_7 := > http:send-request(<http:request method="get"/>, " > https://dpla.lib.utk.edu/repox/OAIHa.... > > > > While this could be a bug, should I be writing this kind of local > function differently? Or could I improve my typing on it? > > > > Thanks very much for any advice or insights you can provide. > > Best, > > Bridger >

⚡Generated with XQuery