当我调用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>
最佳答案
我认为可以通过将空文本替换为临时值,更新所有其他文本,然后将临时值替换为空值来避免此问题。
我不了解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);