简体   繁体   English

使用XQuery和BaseX将更新写入xml文件

[英]Writing updates to xml file with XQuery and BaseX

I have a file.xml storing protocols whose structure is the following: 我有一个file.xml存储协议,其结构如下:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<economato> 
  <protocollo>
    <numero>1</numero>
    <data>2014-12-15</data>
    <oggetto>Trasmissione prospetti di rendiconto chiusura esercizio 2012 - beni mobili proprietà dello stato</oggetto>
    <destinatario>Ragioneria Provinciale Como</destinatario>
    <operatore>MAESTRI</operatore>
    <valido>true</valido>
  </protocollo>
  ...
</economato>

and I would need to change/update the value of the tag "valido", for example from 'true' to 'false' of the protocollo number 1 and I would like this update to be written into the file. 并且我需要更改/更新标签“ valido”的值,例如从协议编号1的“ true”更改为“ false”,并且我希望将此更新写入文件中。 As I am using BaseX, following the documentation I have tried to write this query: 当我使用BaseX时,根据文档,我尝试编写此查询:

xquery let $update := doc('C:\Users\Lorenzo Enzino  Vinci\Desktop\ECONOMATO\databases\2014.xml')//economato/protocollo[numero = 1] return replace value of node $update/valido with 'false' into doc('C:\Users\Lorenzo Enzino Vinci\Desktop\ECONOMATO\databases\2014.xml')//economato

but I get an error like 但我收到类似的错误

[XPST0003] Unexpected end of query: 'into doc('C:\Users\Lorenzo Enzino Vinci\Desktop\ECONOMATO\databases\2014.xml')//economato'

So my query is wrong but I do not know where. 所以我的查询是错误的,但我不知道在哪里。 Can you help me? 你能帮助我吗?

It fails, because it is invalid XQuery Update syntax. 它失败,因为它是无效的XQuery Update语法。 There is not replace ... with ... into ... command, there only is replace ... with ... , as the error message already indicates. 错误消息已经表明,没有replace ... with ... into ...命令中,只有replace ... with ... into ... replace ... with ... It would also be unlogical, because $update already holds a reference to the correct xml fragment. 这也是不合逻辑的,因为$update已经保存了对正确xml片段的引用。

So you simply have to use the following XQuery: 因此,您只需使用以下XQuery:

let $update := doc('C:\Users\Lorenzo Enzino  Vinci\Desktop\ECONOMATO\databases\2014.xml')//economato/protocollo[numero = 1]
return replace value of node $update/valido with 'false'

Also, please note that updates are not automatically written back to files in the file system (to avoid accidentally overwriting files), eg by starting it with basex -u . 另外,请注意,更新不会自动写回到文件系统中的文件(以避免意外覆盖文件),例如通过使用basex -u启动它。 This is explained in detail in the BaseX wiki BaseX Wiki中对此进行了详细说明。

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

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