Cannot convert xs:string to xs:anyURI

Monday, 13 August 2018 · 7 messages · last reply 14 Aug 2018
AM
Andreas Mixich13 Aug 2018 17:57 #
Hi, whenever I define the type of a parameter as xs:anyURI and pass a string to it, I get the error "Cannot convert xs:string to xs:anyURI". I am aware, that xs:anyURI gets automatically cast to xs:string, whenever needed. But why not the other way around? Especially, since in the hierarchy of types, xs:string is ancestor to xs:anyURI.
-- Minden jót, all the best, Alles Gute, Andreas Mixich
AM
Andreas Mixich13 Aug 2018 18:35 #
I should have added, that this also does not succeed:
    declare variable $local:uri1 as xs:anyURI :=  xs:anyURI("
http://example.com/batz/mongo/blah/index.html");
which leaves me even more baffled.
-- Minden jót, all the best, Alles Gute, Andreas Mixich
CG
Christian Grün13 Aug 2018 19:25 #
Quoted text · Andreas Mixich · 1 line
> whenever I define the type of a parameter as xs:anyURI and pass a string
to it, I get the error "Cannot convert xs:string to xs:anyURI". It might comfort you to hear that the behavior is indeed compliant with the spec. As it would take some time to recite the reasons for this, feel free to scan the following sections: https://www.w3.org/TR/xquery-31/#id-function-conversion-rules https://www.w3.org/TR/xquery-31/#promotion
Quoted text · Andreas Mixich · 1 line
>
I should have added, that this also does not succeed:
Quoted text · Andreas Mixich · 2 lines
> ... >
which leaves me even more baffled. Me too, because the following query works on my environment:
  declare variable $local:uri1 as xs:anyURI :=
    xs:anyURI("http://example.com/batz/mongo/blah/index.html");
  $local:uri1
GS
Graydon Saundersin reply to Andreas Mixich, 13 Aug 2018, 18:3513 Aug 2018 21:31 #
Is it not the case that because xs:anyURI is derived from xs:string, you can always go "up" the derivation, because the derived type is less general and always remains an instance of the more general type, but the more general type (casting "down" the path of derivation) is not necessarily an instance of the more derived type? "``````````" is an entirely legitimate string, but it's not a URI, for example. So I think you might have to use (in your second example) one of the "make a URI" functions like iri-to-uri() or encode-for-uri() which make sure the string fits in the URI type. -- Graydon
Quoted text · Andreas Mixich · 13 lines
On Mon, Aug 13, 2018 at 12:35 PM Andreas Mixich <mixich.andreas at gmail.com> wrote: > I should have added, that this also does not succeed: > > declare variable $local:uri1 as xs:anyURI := xs:anyURI(" > http://example.com/batz/mongo/blah/index.html"); > > which leaves me even more baffled. > > -- > Minden jót, all the best, Alles Gute, > Andreas Mixich >
AM
Andreas Mixichin reply to Christian Grün, 13 Aug 2018, 19:2513 Aug 2018 22:24 #
Quoted text · Christian Grün · 5 lines
On Mon, Aug 13, 2018 at 7:25 PM Christian Grün <christian.gruen at gmail.com> wrote: > https://www.w3.org/TR/xquery-31/#id-function-conversion-rules > https://www.w3.org/TR/xquery-31/#promotion >
Thanks for the links. Will check.
Quoted text · Christian Grün · 6 lines
> Me too, because the following query works on my environment: > > declare variable $local:uri1 as xs:anyURI := > xs:anyURI("http://example.com/batz/mongo/blah/index.html"); > $local:uri1 >
Indeed, now it does, for me too. I also found the error I made. Thanks.
-- Minden jót, all the best, Alles Gute, Andreas Mixich
AM
Andreas Mixichin reply to Christian Grün, 13 Aug 2018, 19:2514 Aug 2018 10:58 #
So, I just came around this:
declare variable $local:uri as xs:anyURI := xs:anyURI("
HTtpS://reGexr.com/more/less/path/foo.php?q=bar&same[12]=xxx&same[11]=#sldns13123nfdwdw
");
$local:uri
The error I get is [XPST0003] Invalid entity: '&same[12]=xxx&same[1...'. so I encoded the '&' to '&amp;'.
declare variable $local:uri as xs:anyURI := xs:anyURI("
HTtpS://reGexr.com/more/less/path/foo.php?q=bar&amp;same[12]=xxx&amp;same[11]=#sldns13123nfdwdw
");
$local:uri
which results in: Error: Stopped at C:/Users/user/Desktop/file3, 1/55: [FORG0001] Cannot convert xs:string to xs:anyURI: " HTtpS://reGexr.com/more/less/path/foo.p.... The only way I found to circumvent this, was to encode it via the entity reference and then use encode-for-uri(), but that function/topic seems to be a beast for itself...
Quoted text · Christian Grün · 26 lines
On Mon, Aug 13, 2018 at 7:25 PM Christian Grün <christian.gruen at gmail.com> wrote: > > whenever I define the type of a parameter as xs:anyURI and pass a > string to it, I get the error "Cannot convert xs:string to xs:anyURI". > > It might comfort you to hear that the behavior is indeed compliant with > the spec. As it would take some time to recite the reasons for this, feel > free to scan the following sections: > > https://www.w3.org/TR/xquery-31/#id-function-conversion-rules > https://www.w3.org/TR/xquery-31/#promotion > > > > I should have added, that this also does not succeed: > > ... > > > which leaves me even more baffled. > > Me too, because the following query works on my environment: > > declare variable $local:uri1 as xs:anyURI := > xs:anyURI("http://example.com/batz/mongo/blah/index.html"); > $local:uri1 > > >
-- Minden jót, all the best, Alles Gute, Andreas Mixich
CG
Christian Grün14 Aug 2018 11:32 #
Quoted text · Andreas Mixich · 4 lines
> > [FORG0001] Cannot convert xs:string to xs:anyURI: " > HTtpS://reGexr.com/more/less/path/foo.p.... >
Your URI string contains invalid characters. The first query yields a result, the second doesn’t:
  xs:anyURI("[")
  xs:anyURI("%5b")
There are various ways to get this cleaned up. For example, you can use fn:encode-for-uri [1]. Or have a look at the web:create-url convenience function of BaseX:
  web:create-url(
    'HTtpS://reGexr.com/more/less/path/foo.php',
    map { 'q': 'bar', 'same[12]': 'xxx' }
  )
Quoted text · Andreas Mixich · 36 lines
> The only way I found to circumvent this, was to encode it via the entity > reference and then use encode-for-uri(), but that function/topic seems to > be a beast for itself... > > On Mon, Aug 13, 2018 at 7:25 PM Christian Grün <christian.gruen at gmail.com> > wrote: > >> > whenever I define the type of a parameter as xs:anyURI and pass a >> string to it, I get the error "Cannot convert xs:string to xs:anyURI". >> >> It might comfort you to hear that the behavior is indeed compliant with >> the spec. As it would take some time to recite the reasons for this, feel >> free to scan the following sections: >> >> https://www.w3.org/TR/xquery-31/#id-function-conversion-rules >> https://www.w3.org/TR/xquery-31/#promotion >> >> > >> I should have added, that this also does not succeed: >> > ... >> > >> which leaves me even more baffled. >> >> Me too, because the following query works on my environment: >> >> declare variable $local:uri1 as xs:anyURI := >> xs:anyURI("http://example.com/batz/mongo/blah/index.html"); >> $local:uri1 >> >> >> > > -- > Minden jót, all the best, Alles Gute, > Andreas Mixich >

⚡Generated with XQuery