When I call UpdateXML()
I find that empty nodes are being converted to shorthand XML. Is there a way to prevent UpdateXML()
from behaving this way, perhaps a flag or setting or alternate XPath expression to tell it to preserve the original structure?
/* Example 1 */
SELECT UpdateXML(xmlData, '/TEST/VALUE/text()', 'hello') as "Example 1"
FROM (SELECT XMLType('<TEST><VALUE>hi</VALUE></TEST>') as xmlData
FROM DUAL);
Example 1
---------
<TEST><VALUE>hello</VALUE></TEST>
/* Example 2 */
SELECT UpdateXML(xmlData, '/TEST/VALUE/text()', 'hello') as "Example 2"
FROM (SELECT XMLType('<TEST><VALUE></VALUE></TEST>') as xmlData
FROM DUAL);
Example 2
---------
<TEST><VALUE/></TEST>
What I would like to see:
/* Desired Output, vs. Example 2 */
SELECT UpdateXML(xmlData, '/TEST/VALUE/text()', 'hello') as "Desired Output"
FROM (SELECT XMLType('<TEST><VALUE></VALUE></TEST>') as xmlData
FROM DUAL);
Desired Output
--------------
<TEST><VALUE></VALUE></TEST>
I think you can avoid the problem by replacing empty text with a temporary value, updating all the other text, and then replacing the temporary value with a null.
I don't understand XPath, there's probably a much better way to do this, but this seems to work:
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);