简体   繁体   English

在XQuery表达式中使用外部变量

[英]Using outer variables in an XQuery expression

I am using BaseX database server with a Node.js application. 我正在使用带有Node.js应用程序的BaseX数据库服务器。 The application allows a user to input multiple strings in a textfield separated by a delimiter. 该应用程序允许用户在由定界符分隔的文本字段中输入多个字符串。 These multiple strings are then to be queried to the XML file to search for nodes having the same value. 然后,将这些多个字符串查询到XML文件中,以搜索具有相同值的节点。 I don't have any idea how to include the outer variable splitstring in the XQuery. 我不知道如何在XQuery中包括外部变量splitstring Here's my code: 这是我的代码:

exports.search = function(req, res){

var string = req.body.searchBox;
string = string.toLowerCase();
var splitstring = string.split(' ');
//console.log(splitstring);
var basex = require('basex');
var log = require("../node_modules/basex/debug");

// create session
var session = new basex.Session();
basex.debug_mode = false;

// create query instance
var inputquery = 'for $node in doc("./tags.xml")/images/image return $node/source';
var query = session.query(inputquery);

query.results(log.print);

// close query instance
query.close();

// close session
session.close(); 

I want to implement something like this: 我想实现这样的事情:

var inputquery = 'for $node in doc("./tags.xml")/images/image where $node/tag=' + <one of the strings in splitstring> + ' return $node/source';

Can something like this be done using BaseX and XQuery? 可以使用BaseX和XQuery完成类似的事情吗?

This absolutely is supported. 绝对支持。 See the test suite for the node.js BaseX library . 请参阅针对node.js BaseX库的测试套件

At the top of your query: 在查询的顶部:

declare variable $variable_name external;

In your code: 在您的代码中:

query.bind("variable_name", some_value);

Extending on what Charles Duffy already correctly suggested, here is an example to bind the complete string and tokenize it within XQuery. 扩展Charles Duffy已经正确建议的内容,这是一个绑定完整字符串并在XQuery中将其标记化的示例。 You bind the value and define the value as external within XQuery. 您绑定值,并将值定义为XQuery中的外部值。 Splitting a string within XQuery is simply done by using fn:tokenize() 只需使用fn:tokenize()在XQuery中拆分字符串即可

// create query instance
var inputquery = 'declare variable $string as xs:string external;' +
  'for $node in doc("./tags.xml")/images/image where $node/tag=tokenize($string, "\s") return $node/source';
var query = session.query(inputquery);
query.bind("string", string);
query.results(log.print);
query.close();

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

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