Oracle UpdateXML()改变了XML结构?

时间:2011-11-01 18:48:14

标签: xml oracle oracle11g xmltype

当我调用UpdateXML()时,我发现空节点正在转换为速记XML。有没有办法阻止UpdateXML()以这种方式运行,可能是标志或设置或备用XPath表达式告诉它保留原始结构?

/* 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>

我想看到的内容:

/* 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 个答案:

答案 0 :(得分:0)

我认为你可以通过用临时值替换空文本,更新所有其他文本,然后用空值替换临时值来避免这个问题。

我不了解XPath,可能有更好的方法来做到这一点,但这似乎有效:

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