XQuery/Inserting and Updating Attributes

MotivationEdit

You want to insert or update attributes in a document.

(Note: The XQuery Update syntax below is specific to eXist and is not necessarily identical to that in the W3C XQuery Update spec. Full documentation of eXist's XQuery Update syntax can be found at http://exist-db.org/exist/apps/doc/update_ext.xml)

Example Input DocumentEdit

<root>
    <message>Hello World</message>
</root>

Example of Attribute InsertEdit

xquery version "1.0";
 
let $doc := doc('/db/test.xml')/root
 
let $update := update insert attribute foo {'bar'} into $doc
return 
    $doc

Result DocumentEdit

<root foo="bar">
    <message>Hello World</message>
</root>

Example of Attribute UpdateEdit

let $doc := doc('/db/test/update-attribute/root.xml')/root
 
return update value $doc/@foo with 'new-value'

Result DocumentEdit

<root foo="bar">
    <message>Hello!</message>
</root>
Last modified on 25 March 2014, at 13:30