Skip to content

Instantly share code, notes, and snippets.

@tuurma
Last active March 7, 2019 09:58
Show Gist options
  • Save tuurma/1dbe54a2cab665be3a154a9aeef5d96f to your computer and use it in GitHub Desktop.
Save tuurma/1dbe54a2cab665be3a154a9aeef5d96f to your computer and use it in GitHub Desktop.
XQuery update examples
(: insert attribute :)
for $i in collection('/db/apps/my-data')//nym[not(@cert)]
return
update insert attribute cert {'high'} into $i
(: insert more than one node :)
update insert (<a/>, <b/>) into //foo
(: make sure to explicitly specify namespaces when inserting/replacing nodes :)
update insert <surname xmlns="http://www.tei-c.org/ns/1.0">Huo</surname> into //tei:name[.='Otmar']
(: replace text content of an element :)
declare namespace tei="http://www.tei-c.org/ns/1.0";
for $i in collection("/db/apps/my-data")//tei:m[@n="3"][.="τ"]
return update replace $i/text() with '(V)τ'
(: but better do the same with update VALUE :)
for $i in collection("/db/apps/my-data")//tei:m[@n="3"][.="τ"]
return update value $i with '(V)τ'
(: not only text content can be updated with update value :)
for $i in //name[. = "Otmar"]
return update value $i with <forename>Oti</forename>
(: RENAME node :)
for $email in //email
update rename $email as 'mail'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment