Update xml element value in Oracle

axl g picture axl g · Apr 2, 2012 · Viewed 9.9k times · Source

Having a XMLTYPE column in an Oracle table, I would like to update the values of some xml elements using UpdateXML method but I have troubles doing so because of a namespace which is applied on an xml element which is not parent. The xml structure of my elmenets looks something like:

<a>
  <b xmlns="urn:www.someSite.com/myModel">
    <c>my value</c>
  </b>
</a>

and an update of the following form it does not work:

UPDATE myTable 
  SET myColumn = UpdateXML(myColumn, '/a/b/c','other value', 'xmlns="urn:www.someSite.com/myModel"');

Answer

A.B.Cade picture A.B.Cade · Apr 2, 2012

Pretty much the same as this post but uglier...

UPDATE myTable
   SET myColumn = updatexml(myColumn ,
                 '/a/*',
                 updatexml(extract(myColumn , '/a/*'),
                           'b/c/text()',
                           'my new value',
                           'xmlns=urn:www.someSite.com/myModel'));

EDIT: If you have more then one b element in a you'll have to change the whole text within a and not for each child, so you can try:

UPDATE myTable
   SET myColumn = updatexml(myColumn ,
                 '/a/text()',
                 updatexml(extract(myColumn , '/a/*'),
                           'b/c/text()',
                           'my new value',
                           'xmlns=urn:www.someSite.com/myModel'));