gpt4 book ai didi

xml - Oracle UpdateXML() 更改 XML 结构?

转载 作者:数据小太阳 更新时间:2023-10-29 02:46:20 50 4
gpt4 key购买 nike

当我调用 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);

关于xml - Oracle UpdateXML() 更改 XML 结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7971345/

50 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com