Database Reference
In-Depth Information
creating a node and then adding its attributes all in separate updates is not a good
idea. So, try to avoid this (we'll get to the exact syntax later):
let $ elm as element () := doc ( '/db/Path/To/Some/Document.xml' )/ *
return (
update insert <NEW/> into $ elm ,
update insert attribute x { 'y' } into $ elm / * [ last ()],
update insert attribute a { 'b' } into $ elm / * [ last ()]
)
Instead, create the node with its attributes as an XML fragment first and update your
document in one go:
let $ elm as element () := doc ( '/db/Path/To/Some/Document.xml' )/ *
return
update insert <NEW x = " y " a = " b " /> into $ elm
Or:
let $ elm as element () := doc ( '/db/Path/To/Some/Document.xml' )/ *
let $ new-element as element () := <NEW x = " y " a = " b " />
return
update insert $ new-element into $ elm
Also be aware that updating documents is not done in isolation. Updating something
that another running XQuery script is using at the same time may lead to unanticipa‐
ted results.
eXist has two mechanisms to directly update XML documents:
The eXist XQuery update extension
This is an eXist-specific extension to XQuery (based on an early draft of the
XQuery update specification ) that allows you to write XQuery statements that
alter the database. This is the preferred mechanism for performing in-place
updates.
XUpdate
You specify your desired alterations to the database in an XML document using
the XUpdate syntax, and then pass it to the XUpdate processor to have them
applied. The XUpdate specification is no longer maintained and so using this
mechanism is discouraged, but it's still present mainly for backward compatibil‐
ity reasons.
eXist's XQuery Update Extension
eXist defines its own XQuery language constructs to update documents in the data‐
base. These language constructs follow a proposal from Patrick Lehti .
To get a feeling for it, here is an example of an XQuery update adding a log message
to an XML logfile:
Search WWH ::




Custom Search