sql >> Base de Datos >  >> RDS >> Oracle

Oracle UpdateXML () cambia la estructura XML?

Creo que puede evitar el problema reemplazando el texto vacío con un valor temporal, actualizando el resto del texto y luego reemplazando el valor temporal con un valor nulo.

No entiendo XPath, probablemente haya una manera mucho mejor de hacer esto, pero parece funcionar:

SELECT
    --#3: Replace the temporary value with null, this keeps the start and end tag
    UpdateXML(
        --#2: Replace everything but the temporary value
        UpdateXML(
            --#1: Replace empty text with a temporary value
            UpdateXML(xmlData, '/TEST/VALUE[not(text())]', '<VALUE>TEMPORARY VALUE</VALUE>')
        ,'/TEST/VALUE[text()!="TEMPORARY VALUE"]/text()', 'hello')
    ,'/TEST/VALUE[text()="TEMPORARY VALUE"]/text()', null) examle
FROM (SELECT XMLType('<TEST><VALUE>hi</VALUE><VALUE>hola</VALUE><VALUE></VALUE></TEST>') as xmlData FROM DUAL);