change value of element by xquery

11,334

if your engine supports updates and scripting:

declare variable $x := 
   <category>
     <catid>1</catid>
     <cattext> sport </cattext>
   </category>;

replace value of node $x/cattext with "art";
$x;

or if you don't want to persist the changes you can transform a copy of it:

let $x := 
   <category>
     <catid>1</catid>
     <cattext> sport </cattext>
   </category>
return
   copy $changedx := $x
   modify (replace value of node $changedx/cattext with "art")
   return $changedx 

these code snippets successfully run on http://try.zorba.io/

If your XQuery processor doesn't support updates Alejandro's solution is top.

Share:
11,334

Related videos on Youtube

Halaas
Author by

Halaas

Updated on June 04, 2022

Comments

  • Halaas
    Halaas almost 2 years
    <category>
         <catid>1</catid>
         <cattext> sport </cattext>
    </category>
    

    I want change text of element <cattext> into another like "art" instead of sport by using xquery