Oracle UpdateXML() changes XML structure?

本秂侑毒 提交于 2020-01-01 14:26:07

问题


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>

回答1:


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);


来源:https://stackoverflow.com/questions/7971345/oracle-updatexml-changes-xml-structure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!