简体   繁体   English

带有basex-node数据库的XQuery可以通过外部变量提供其他结果吗?

[英]XQuery with basex-node Database gives other results with an external variable?

I am currently trying to get into properly using a BaseX Database in combination with the NodeJS package " basex-node ". 我目前正在尝试结合使用BaseX数据库和NodeJS包“ 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. 通过编写连接到我在本地计算机上运行的BaseX-Database-Server的.js脚本,我已经能够创建一个新的数据库,添加和删除单个文件以及处理一些xquery查询。

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. 将我的查询插入变量,将其绑定到basex节点查询并执行它,正如我期望的那样,我将获得文档的内容作为适当的响应。

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. 现在,我要做的就是不要将“ a1.xml”作为我要查找的固定文档,而是提供一个外部变量。 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). 因此,基本上,我将$foo用作外部变量,并将其绑定到“ a1.xml”(我想在以后知道动态文档或特定节点时使用它,以便尽快了解如何使用和调用参数) .js脚本)。

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. 而且我收到“结果:'a1.xml'”,所以我猜我以正确的方式绑定了变量。

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. 或两者兼而有之。

Variables are not resolved inside strings. 变量不能在字符串内部解析。 Instead of 'test_db/$foo' use 'test_db/'|| $foo 代替'test_db/$foo'使用'test_db/'|| $foo 'test_db/'|| $foo , which is shorthand for concat('test_db/', $foo) . 'test_db/'|| $fooconcat('test_db/', $foo)简写。

For example, this will return foo$bar 例如,这将返回foo$bar

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

while

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

will return the expected foobar . 将返回预期的foobar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM