问题描述
当我呼叫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>
我认为您可以通过将空文本替换为临时值,更新所有其他文本,然后将临时值替换为空值来避免此问题. /p>
我不了解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);
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);
这篇关于Oracle UpdateXML()会更改XML结构吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!