BaseX: Updates are not written back

954 views Asked by At

I am trying to run the following query in BaseX,

let $c := doc('t.xq')//entry return (replace value of node $c/author with 'BaseX' )

The input file t.xq is

<entry>
    <title>Transform expression example</title>
    <author>BaseX Team</author>
  </entry>

I expect it to return the modified data, but it executes and return nothing. it says Updates are not written back

How can I see the modified entry? which commands return the altered data?

1

There are 1 answers

2
Ahmad On BEST ANSWER

Cited from http://docs.basex.org/wiki/XQuery_Update:

In BaseX, all updates are performed on database nodes or in main memory. By default, update operations do not affect the original input file (the info string "Updates are not written back" appears in the query info to indicate this). The following solutions exist to write XML documents and binary resources to disk:

  • Updates on main-memory instances of files that have been retrieved via fn:doc or fn:collection will be propagated back to disk when the WRITEBACK option is turned on. This option can also be activated on command line via -u. Make sure you back up the original documents before running your queries.
  • Functions like fn:put or file:write can be used to write single XML documents to disk. With file:write-binary, you can write binary resources.
  • The EXPORT command can be used write all resources of a databases to disk.

However, if you only need to get the output of an update command you can copy it to a variable (in memory) and transform this variable as follows:

copy $c := doc('t.xq')//entry
modify (
  replace value of node $c/author with 'BaseX'
)
return $c

You can also use update which is a convenience operator for writing simple transform expressions.

doc('t.xq')//entry update replace value of node ./author with 'BaseX'