Binding external variable to sequence
Friday, 8 February 2013 · 2 messages · last reply 9 Feb 2013
LS
Lansing, Jeff J CIV SPAWARSYSCEN-PACIFIC, 562508 Feb 2013 22:40 #
How can I bind an external variable to a sequence?
I see the syntax trick for escaping commas, but I still don't get a bound sequence. I tried this:
import org.basex.core.*;
import org.basex.core.cmd.*;
import org.junit.*;
public class BindingTest {
String var = "$ids=";
String seq = "('a',,'b',,'c')";
String query = ""
+ "declare variable $ids external; "
+ "let $count := count($ids) "
+ "return if($count gt 1) then element ids { $ids } "
+ "else error(QName('urn:err', 'not_enough_ids'), xs:string($count)) "
;
/**
* test binding external variable to sequence
*/
@Test
public void test() {
try {
Context context = new Context();
new Set(Prop.BINDINGS, var + seq).execute(context);
String result = new XQuery(query).execute(context);
context.close();
System.out.println(result);
} catch(Exception e) {
System.err.println(e);
}
}
}I expect to get <ids>...</ids> but instead I get the error. I'm using v7.5, Java 7u13.
CG
Christian Grün9 Feb 2013 11:20 #
Hi Jeff,
the
BINDINGS options can only be used to bind single items to variables. Some options to solve this problem are to..
– split your strings within the query, using a unique delimiter character.. declare variable $ids external;
let $ids := tokenize($ids, "~")
let $count := count($ids)
return ...– go one level deeper and create a QueryProcessor instance that gives you full access to the query architecture:
Context context = new Context();
String query = "declare variable $ids external; $ids";
QueryProcessor qp = new QueryProcessor(query, context);
String[] vals = { "a", "b", "c" };
qp.bind("ids", vals);
System.out.println(qp.value());
qp.close();– have a look at the XQJ API (currently client/server-based, to be extended to local/embedded mode soon).
http://xqj.net/basex/
Hope this helps,
Christian
Quoted text · 56250 · 46 lines
On Fri, Feb 8, 2013 at 4:54 PM, Lansing, Jeff J CIV SPAWARSYSCEN-PACIFIC, 56250 <jeff.lansing at navy.mil> wrote: > How can I bind an external variable to a sequence? > I see the syntax trick for escaping commas, but I still don't get a bound > sequence. > I tried this: > > import org.basex.core.*; > import org.basex.core.cmd.*; > import org.junit.*; > > public class BindingTest { > > String var = "$ids="; > String seq = "('a',,'b',,'c')"; > String query = "" > + "declare variable $ids external; " > + "let $count := count($ids) " > + "return if($count gt 1) then element ids { $ids } " > + "else error(QName('urn:err', 'not_enough_ids'), xs:string($count)) " > ; > /** > * test binding external variable to sequence > */ > @Test > public void test() { > try { > Context context = new Context(); > new Set(Prop.BINDINGS, var + seq).execute(context); > String result = new XQuery(query).execute(context); > context.close(); > System.out.println(result); > } catch(Exception e) { > System.err.println(e); > } > } > } > I expect to get <ids>...</ids> but instead I get the error. I'm using v7.5, > Java 7u13. > > > _______________________________________________ > BaseX-Talk mailing list > BaseX-Talk at mailman.uni-konstanz.de > https://mailman.uni-konstanz.de/mailman/listinfo/basex-talk >
Written for BaseX 7.6. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery