Hello --
I suspect I am being a cabbage somehow, but I'm not figuring this out.
If I have a query in the GUI (9.6.1) which declares an external variable:
declare variable $targetMap external;
and set the value via the GUI $x button to:
map {'structure' : '/some/path/name'}
It doesn't work. Typing the variable as map(*) doesn't work.
declare variable $targets external; declare variable $targetMap as map(*) := xquery:eval($targets);
does work, but makes me think I must be doing something wrong.
How does one go about binding a literal map to an external variable? (I am going to want to do this in a script later; the GUI version is strictly for testing.)
Thanks! Graydon
Hi Graydon,
I guess this question didn’t get any response so far:
How does one go about binding a literal map to an external variable? (I am going to want to do this in a script later; the GUI version is strictly for testing.)
External variable bindings via the GUI are limited to atomic items. If you use Java for scripting, you could proceed as follows:
import org.basex.core.*; import org.basex.query.*; import org.basex.query.value.*;
public class BindMap { public static void main(String... whatever) throws Exception { Context ctx = new Context(); String input = "map { 'structure': '/some/path/name' }"; Value map = new QueryProcessor(input, ctx).value();
String query = "declare variable $map external; $map"; QueryProcessor qp = new QueryProcessor(query, ctx); System.out.println(qp.bind("map", map).value()); } }
Hope this helps, Christian
Hi Christian --
Thank you!
I have been finding that passing in a single XML config file and using xquery:eval() on the XPath-syntax maps therein has been working.
(Locally, we use Python for scripting, so I am going to save the pattern but keep the config files for now. :)
Much appreciated!
-- Graydon
On Wed, Sep 22, 2021 at 4:52 AM Christian Grün christian.gruen@gmail.com wrote:
Hi Graydon,
I guess this question didn’t get any response so far:
How does one go about binding a literal map to an external variable? (I
am going to want to do this in a script later; the GUI version is strictly for testing.)
External variable bindings via the GUI are limited to atomic items. If you use Java for scripting, you could proceed as follows:
import org.basex.core.*; import org.basex.query.*; import org.basex.query.value.*;
public class BindMap { public static void main(String... whatever) throws Exception { Context ctx = new Context(); String input = "map { 'structure': '/some/path/name' }"; Value map = new QueryProcessor(input, ctx).value();
String query = "declare variable $map external; $map"; QueryProcessor qp = new QueryProcessor(query, ctx); System.out.println(qp.bind("map", map).value());
} }
Hope this helps, Christian
If I remember correctly, it should be possible with the Python binding [1] to pass on JSON data and specify 'json' as input type:
query.bind("$input", '{ 1: "abc" }', 'json')
Hope this helps, Christian
[1] https://github.com/BaseXdb/basex/blob/master/basex-api/src/main/python/Query...
On Wed, Sep 22, 2021 at 8:32 PM Graydon Saunders graydonish@gmail.com wrote:
Hi Christian --
Thank you!
I have been finding that passing in a single XML config file and using xquery:eval() on the XPath-syntax maps therein has been working.
(Locally, we use Python for scripting, so I am going to save the pattern but keep the config files for now. :)
Much appreciated!
-- Graydon
On Wed, Sep 22, 2021 at 4:52 AM Christian Grün christian.gruen@gmail.com wrote:
Hi Graydon,
I guess this question didn’t get any response so far:
How does one go about binding a literal map to an external variable? (I am going to want to do this in a script later; the GUI version is strictly for testing.)
External variable bindings via the GUI are limited to atomic items. If you use Java for scripting, you could proceed as follows:
import org.basex.core.*; import org.basex.query.*; import org.basex.query.value.*;
public class BindMap { public static void main(String... whatever) throws Exception { Context ctx = new Context(); String input = "map { 'structure': '/some/path/name' }"; Value map = new QueryProcessor(input, ctx).value();
String query = "declare variable $map external; $map"; QueryProcessor qp = new QueryProcessor(query, ctx); System.out.println(qp.bind("map", map).value());
} }
Hope this helps, Christian
On Wed, Sep 22, 2021 at 09:15:30PM +0200, Christian Grün scripsit:
If I remember correctly, it should be possible with the Python binding [1] to pass on JSON data and specify 'json' as input type:
query.bind("$input", '{ 1: "abc" }', 'json')
Hope this helps,
It will!
Much appreciated!
-- Graydon
basex-talk@mailman.uni-konstanz.de