How to retrieve HTTP request parameters and use them in XQuery with BaseX

Thursday, 30 December 2010 · 7 messages · last reply 5 Jan 2011
SD
software developer30 Dec 2010 14:01 #
Hi, eXist DB offers this facility via HTTP related functions: http://en.wikibooks.org/wiki/XQuery/Checking_for_Required_Parameters What I want to achieve is pass parameters to XQuery dynamically (not necessarily by using HTTP request parameters, although that appears to be one way of achieving this). Is that possible in BaseX; if yes, how? Thanks.
AW
Andreas Weiler30 Dec 2010 14:18 #
Hi, passing parameters to xquery dynamically can be done with the xquery java bindings. So the parameter comes directly from your java class. for example you can create a class like the following (i did create it in the org.basex package): package org.basex;
public class Test {
   public Test() {
   }
   public static int calc(final int t) {
     return  t * t;
   }
}
and run a query like the following:

declare namespace test = "java:org.basex.Test";
for $i in 1 to 10 return test:calc(xs:int($i))
so the results are: 1 4 9 16 25 36 49 64 81 100 If you want to avoid static methods you can do it as follows: class:
package org.basex;
public class Test {
   public Test() {
   }
   public int calc(final int t) {
     return  t * t;
   }
}
query:
declare namespace test = "java:org.basex.Test";
for $i in 1 to 10
let $t :=  test:new()
return test:calc($t, xs:int($i))
note you have to pass a created test-object to the calc-method then... I hope this helps, kind regards, Andreas
Quoted text · software developer · 17 lines
Am 30.12.10 14:01, schrieb software developer: > Hi, > > eXist DB offers this facility via HTTP related functions: > http://en.wikibooks.org/wiki/XQuery/Checking_for_Required_Parameters > > What I want to achieve is pass parameters to XQuery dynamically (not > necessarily by using HTTP request parameters, although that appears to > be one way of achieving this). Is that possible in BaseX; if yes, how? > > Thanks. > > > _______________________________________________ > BaseX-Talk mailing list > BaseX-Talk at mailman.uni-konstanz.de > https://mailman.uni-konstanz.de/mailman/listinfo/basex-talk
AW
Andreas Weiler30 Dec 2010 19:24 #
Forwarding to the mailing-list... -------- Original-Nachricht -------- Betreff: Re: [basex-talk] How to retrieve HTTP request parameters and use them in XQuery with BaseX Datum: Thu, 30 Dec 2010 13:59:57 +0000
Quoted text · software developer · 20 lines
Von: software developer <computer.software.developer at gmail.com> An: Andreas Weiler <andreas.weiler at uni-konstanz.de> Well, then the problem changes to 'how to pass dynamic parameters from an HTTP client to the java class in question - java class is running on a different machine' - that bridge was to be the HTTP request parameters - what else can serve to pass dynamic parameters to the xquery? I invoke BaseX via jax-rx and xquery is stored on server-side (cannot put that in the URL itself) - xquery is invoked using run param to jax-rx invocation. Your solution has xquery passing the param to the java class, in my case an external HTTP client is to send the parameters...so, how would the java class receive the HTTP request param or alternatively, how would the xquery receive request param directly via jax-rx? On Thu, Dec 30, 2010 at 1:18 PM, Andreas Weiler <andreas.weiler at uni-konstanz.de <mailto:andreas.weiler at uni-konstanz.de>> wrote: Hi, passing parameters to xquery dynamically can be done with the xquery java bindings. So the parameter comes directly from your java class.
LW
Leonard Wörteler30 Dec 2010 23:17 #
Hi,
Quoted text · Andreas Weiler · 13 lines
Am 30.12.2010 13:59, schrieb software developer: > Well, then the problem changes to 'how to pass dynamic parameters from > an HTTP client to the java class in question - java class is running on > a different machine' - that bridge was to be the HTTP request parameters > - what else can serve to pass dynamic parameters to the xquery? I > invoke BaseX via jax-rx and xquery is stored on server-side (cannot put > that in the URL itself) - xquery is invoked using run param to jax-rx > invocation. > > Your solution has xquery passing the param to the java class, in my case > an external HTTP client is to send the parameters...so, how would the > java class receive the HTTP request param or alternatively, how would > the xquery receive request param directly via jax-rx?
Currently you can bind values to external variables via JAX-RX (at least in BaseX), using the 'var' parameter.
Quoted text · Andreas Weiler · 4 lines
Assuming your query is > declare variable $x as xs:integer external; > declare variable $y as xs:integer external; > $x * $y
then the easiest way is to append the parameter
Quoted text · Andreas Weiler · 1 line
> &var=$x=21%01$y=2
to your JAX-RX URI, thus binding 21 to $x and 2 to $y. Note that you have to glue multiple bindings together with the 'SOH' ASCII char, '\1' in Java and %01 in the URL encoding. [1] is a working example. If you also want to specify the type of the bound value, this can be done by appending it to the binding, separated by another '=':
Quoted text · Andreas Weiler · 1 line
> &var=$x=14=xs:integer%01$y=3=xs:integer
SD
software developer31 Dec 2010 16:18 #
Hi, The parameters are not bound to variables when a query is invoked via the run parameter. For example, I store my query in myquery.xq on the server side; if the query contains current-time() invocation of the URL http://localhost:8984/basex/jax-rx?wrap=no&run=myquery.xq&var=$x=21%01$y=2 results in display of current date-time
but if the query contains
declare variable $x as xs:integer external;
declare variable $y as xs:integer external;
$x*$y
I get the error Stopped at line 1, column 34 in myquery.xq: [XQDY0002] No value defined for "$x as xs:integer". The parameters are correctly bound to variables when a query is invoked via the query parameter named query as you described, but that is not what I am after. Is this a bug? Is there a workaround?
AW
Andreas Weiler31 Dec 2010 16:52 #
Hi, this feature isnt enabled for the run command yet. We put that on our todo-list and get it done as fast as possible. You could also have a look into our language bindings (for other ways then REST) [1], here you can also bind variables to queries. If your query is not to large, i guess the network traffic for sending the query for each request should not be too much. Kind regards, Andreas [1] http://www.inf.uni-konstanz.de/dbis/basex/bindings
Quoted text · software developer · 29 lines
Am 31.12.10 16:18, schrieb software developer: > Hi, > > The parameters are not bound to variables when a query is invoked via > the run parameter. > > For example, I store my query in myquery.xq on the server side; if the > query contains > current-time() > > invocation of the URL > http://localhost:8984/basex/jax-rx?wrap=no&run=myquery.xq&var=$x=21%01$y=2 > <http://localhost:8984/basex/jax-rx?wrap=no&run=myquery.xq&var=$x=21%01$y=2> > > results in display of current date-time > > but if the query contains > declare variable $x as xs:integer external; > declare variable $y as xs:integer external; > $x*$y > > I get the error > Stopped at line 1, column 34 in myquery.xq: > [XQDY0002] No value defined for "$x as xs:integer". > > The parameters are correctly bound to variables when a query is > invoked via the query parameter named query as you described, but that > is not what I am after. Is this a bug? Is there a workaround? >
AW
Andreas Weiler5 Jan 2011 10:51 #
-------- Original-Nachricht -------- Betreff: Re: [basex-talk] Fwd: Re: How to retrieve HTTP request parameters and use them in XQuery with BaseX Datum: Wed, 5 Jan 2011 09:51:07 +0000
Quoted text · software developer · 53 lines
Von: software developer <computer.software.developer at gmail.com> An: Andreas Weiler <andreas.weiler at uni-konstanz.de> Hi Andreas, Wonderful, thanks a lot!! Great work. On Mon, Jan 3, 2011 at 10:02 AM, Andreas Weiler <andreas.weiler at uni-konstanz.de <mailto:andreas.weiler at uni-konstanz.de>> wrote: Hi there, i fixed the bug and attached the newest version of our api. Don't hesitate to ask for more help, Kind regards Am 31.12.10 16:52, schrieb Andreas Weiler: Hi, this feature isnt enabled for the run command yet. We put that on our todo-list and get it done as fast as possible. You could also have a look into our language bindings (for other ways then REST) [1], here you can also bind variables to queries. If your query is not to large, i guess the network traffic for sending the query for each request should not be too much. Kind regards, Andreas [1] http://www.inf.uni-konstanz.de/dbis/basex/bindings Am 31.12.10 16:18, schrieb software developer: Hi, The parameters are not bound to variables when a query is invoked via the run parameter. For example, I store my query in myquery.xq on the server side; if the query contains current-time() invocation of the URL http://localhost:8984/basex/jax-rx?wrap=no&run=myquery.xq&var=$x=21%01$y=2 <http://localhost:8984/basex/jax-rx?wrap=no&run=myquery.xq&var=$x=21%01$y=2> <http://localhost:8984/basex/jax-rx?wrap=no&run=myquery.xq&var=$x=21%01$y=2 <http://localhost:8984/basex/jax-rx?wrap=no&run=myquery.xq&var=$x=21%01$y=2>> results in display of current date-time but if the query contains declare variable $x as xs:integer external; declare variable $y as xs:integer external; $x*$y I get the error Stopped at line 1, column 34 in myquery.xq: [XQDY0002] No value defined for "$x as xs:integer". The parameters are correctly bound to variables when a query is invoked via the query parameter named query as you described, but that is not what I am after. Is this a bug? Is there a workaround? _______________________________________________ BaseX-Talk mailing list BaseX-Talk at mailman.uni-konstanz.de <mailto:BaseX-Talk at mailman.uni-konstanz.de> https://mailman.uni-konstanz.de/mailman/listinfo/basex-talk

⚡Generated with XQuery