Oracle UpdateXML() 更改 XML 结构?

发布于 2024-12-13 04:37:49 字数 1055 浏览 0 评论 0原文

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

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

债姬 2024-12-20 04:37:49

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

我不明白 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);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文