0
votes

I am currently trying to get into properly using a BaseX Database in combination with the NodeJS package "basex-node".

By writing .js Scripts that connect to the BaseX-Database-Server that I have running on my local-machine, I was already able to create a new database, add and delete single files and messing around with some xquery-queries.

My problem is the following: When I use

var input = for $doc in collection(\'test_db\') where matches(document-uri($doc), \'test_db/a1.xml') return $doc;
var query = client.query(input);

query.execute(log.print);

to insert my query into a variable, bind it to a basex-node Query and execute it, I will get the contents of my document as a proper response, as I would expect.

Now what I want to do is to not have "a1.xml" as the fixed document I want to look for, but give an external variable instead. So I changed my code to the following:

var input = "declare variable $foo external; for $doc in collection(\'test_db\') where matches(document-uri($doc), \'test_db/$foo') return $foo";
var query = client.query(input);

query.bind("foo", "a1.xml", "", log.print);
query.execute(log.print);

So basically I use $foo as an external variable and bind "a1.xml" to it (I want to use that to later on find dynamic documents or specific nodes, as soon as I get to know how to use and call parameters with my .js script).

But the result I recieve when doing that is just "result:''" instead of the document.

I already tried changing the input to:

var input = "declare variable $foo external; return $foo"; 

And I recieved "result: 'a1.xml'", so I'm guessing I bound the variable in a correct way.

But it doesn't seem to work that way, so I assume I made either a huge mistake in understanding how all of this works, or a small stupid mistake in the code. Or maybe both.

1
Small hint: in XQuery, you can use both single quotes ' and double quotes " to escape strings, they're equal. Saves you from escaping qutoes when constructing queries elsewhere. - Jens Erat

1 Answers

0
votes

Variables are not resolved inside strings. Instead of 'test_db/$foo' use 'test_db/'|| $foo, which is shorthand for concat('test_db/', $foo).

For example, this will return foo$bar

let $bar := 'bar'
return 'foo$bar'

while

let $bar := 'bar'
return 'foo' || $bar

will return the expected foobar.