简体   繁体   English

BaseX中的XQuery Update查询成功,但是没有任何更改写入文件

[英]XQuery Update queries in BaseX are succesful, but no changes are written to the file

I am trying to use XQuery and BaseX to manage some files.xml as databases. 我正在尝试使用XQuery和BaseX来管理一些files.xml作为数据库。

I am using basexclient to send queries to basexserver. 我正在使用basexclient将查询发送到basexserver。 For example I have a simple file called library.xml like the following: 例如,我有一个名为library.xml的简单文件,如下所示:

<library>
   <book>gone with the wind</book>
   <book>the thornbirds</book>
</library>

and I would like to add a node, so from basexclient terminal I send: 并且我想添加一个节点,因此我从basexclient终端发送:

XQUERY insert node <book>Dracula</book> as last into doc('library.xml')//library

and then I get the message 然后我得到消息

Query executed in 10.5 ms 在10.5毫秒内执行查询

But nothing has changed into the file. 但是文件没有任何变化。 Is there a command to commit changes? 是否有提交更改的命令?

By default, updates on a memory instances of your file will not be propagated back to disk. 默认情况下,文件的内存实例上的更新不会传播回磁盘。 You can change this by turn the WRITEBACK option on. 您可以通过打开WRITEBACK选项来更改此设置。 I agree that the behavior is surprising; 我同意这种行为令人惊讶; it was introduced to prevent users from accidentally changing local files. 引入它是为了防止用户意外更改本地文件。

A general note: If you want to update local files, it may be better to use BaseX in standalone mode. 一般说明:如果要更新本地文件,最好在独立模式下使用BaseX。 If you are working with the client/server architecture (ie, with basexclient and not basex ), the server could have been started from a different working directory, and it may not be able to resolve the relative path in your doc() function. 如果使用的是客户机/服务器体系结构(即使用basexclient而不是basex ),则服务器可能是从其他工作目录启动的,因此它可能无法解析doc()函数中的相对路径。

Performant updates to (serialized) XML files are barely possible. 对(序列化的)XML文件进行高性能更新几乎是不可能的。 BaseX and also the other XML databases on the market all use special, internal representations with indexes for fast querying, and especially updates. 市场上的BaseX以及其他XML数据库都使用特殊的内部表示形式和索引来进行快速查询,尤其是更新。

If you already created a database from the file, your query updates the database, not the original XML file. 如果已经从该文件创建了数据库 ,则查询将更新数据库,而不是原始XML文件。 You should be able to observe the changes (possibly multiple times, one node added for each attempt to run the query) in the database representation, which you can verify by running 您应该能够观察数据库表示形式中的更改(可能多次,对于每次尝试运行查询都添加一个节点),可以通过运行来验证

doc('library.xml')

and checking its contents. 并检查其内容。 To serialize the changes to a file, use BaseX' EXPORT command . 要将更改序列化为文件,请使用BaseX的EXPORT命令

If you didn't create a database yet and query a file on your hard disk, a temporary in-memory database gets created, which indeed is successfully updated - but instantly discarded. 如果您尚未创建数据库并查询硬盘上的文件,则会创建一个临时的内存数据库,该数据库确实已成功更新-但立即被丢弃。 Create a database before running updates. 在运行更新之前创建数据库

See Christian Grün's answer discussing the WRITEBACK option , wich enables you to update a file without creating databases. 请参阅ChristianGrün的答案,其中讨论了WRITEBACK选项 ,这使您无需创建数据库即可更新文件。

I'm writing a simple sync service between a system and baseX. 我在系统和baseX之间编写一个简单的同步服务。 I have a similar issue in that I'm doing multiple queries using PHP Client and making multiple queries on the same session or multiple that succeed without showing results as viewed from baseX dba. 我有一个类似的问题,即我正在使用PHP Client进行多个查询,并在同一会话或多个成功进行多个查询的情况下成功执行,而没有显示从baseX dba查看的结果。 I open, execute, get results if any and close each query. 我打开,执行,获取结果(如果有)并关闭每个查询。 If I execute each query separately, I get the result updated (after a refresh) in dba. 如果我分别执行每个查询,则将在dba中更新结果(刷新后)。 I'm running this as a service with port 1894 open to localhost. 我将其作为服务运行,端口1894向本地主机开放。 Each query has the general form of 每个查询的一般形式为

try {
$myXML22 = 'somegenerated xml' 
$filepathname = "mydoc/mydoc_doc22.xml";
$docID = "22";
$dbName = "myDB";
$input = 'let $xml := \''.$myXML22.'\' '.
         'return db:replace("'.$dbName.'","'.$filepathname.'",$xml)';
$query = $session->query($input);
$log .= "Updated/added $filepathname to basex db ".$dbName." \n";
} catch (Exception $e) {
  error_log("somemessage".$e->getMessage());
}

Here is a logging of the service. 这是服务的日志记录。

  1. Db myDB exist in baseX db myDB存在于baseX中
  2. Found 24 mydoc editions in myDB db in baseX 在baseX的myDB数据库中找到24个mydoc版本
  3. Checked manifest exists in myDB db in baseX 检查的清单存在于baseX的myDB数据库中
  4. Found 5 timeStamp entries in manifest of myDB db in baseX 在baseX的myDB数据库清单中找到5个timeStamp条目
  5. Created XML for doc22 为doc22创建XML
  6. Validated against RNG for doc22 已针对RNG对doc22进行了验证
  7. Updated/added myxml/mydoc_doc22.xml to basex db myDB 已将myxml / mydoc_doc22.xml更新/添加到basex db myDB
  8. Inserted timeStamp node for mydoc 22 into manifest for basex db myDB 将mydoc 22的timeStamp节点插入到basex db myDB的清单中

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

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