Passing a map to an external variable

Thursday, 9 September 2021 · 5 messages · last reply 23 Sep 2021
GS
Graydon Saunders9 Sep 2021 20:57 #
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
CG
Christian Grün22 Sep 2021 10:52 #
Hi Graydon, I guess this question didn’t get any response so far:
Quoted text · Graydon Saunders · 1 line
> 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
CG
Christian Grünin reply to Graydon Saunders, 22 Sep 2021, 22:3322 Sep 2021 21:15 #
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/QueryBindExample.py
Quoted text · Graydon Saunders · 43 lines
On Wed, Sep 22, 2021 at 8:32 PM Graydon Saunders <graydonish at 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 at 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
GS
Graydon Saundersin reply to Christian Grün, 22 Sep 2021, 10:5222 Sep 2021 22:33 #
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
Quoted text · Christian Grün · 32 lines
On Wed, Sep 22, 2021 at 4:52 AM Christian Grün <christian.gruen at 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 >
G
Graydonin reply to Christian Grün, 22 Sep 2021, 21:1523 Sep 2021 05:19 #
Quoted text · Christian Grün · 7 lines
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

⚡Generated with XQuery