Re: [basex-talk] XPTY0004 error when calling http:send-request inside of a local function
Christian, As always, thank you for your help! I had tried the following ```xquery declare function local:grab2( $url as xs:string, $path as xs:string, $fname as xs:string ) as document-node() { let $req := http:send-request(, $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 On Sun, Oct 11, 2020 at 4:06 PM Christian Grün 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(, $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(, " > 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 >
Re: [basex-talk] XPTY0004 error when calling http:send-request inside of a local function
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(, $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(, > "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
Re: [basex-talk] XPTY0004 error when calling http:send-request inside of a local function
And naturally I forgot to mention... On Sun, Oct 11, 2020 at 3:49 PM Bridger Dyson-Smith 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(, $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(, " > https://dpla.lib.utk.edu/repox/OAIHa > > that despite the error, the correct response is written to the filepath specified. 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! > Best, > Bridger >