How to rename an Oracle XMLTYPE node(如何重命名 Oracle XMLTYPE 节点)
问题描述
我在 PL/SQL 中有一个 XMLType,我需要重命名一些节点和一些值.例如:
I have an XMLType in PL/SQL and I need to rename some of the nodes and some of the values. For example:
<root>
<fields>
<a>foo</a>
<b>bar</b>
</fields>
</root>
我想把上面的变成这样:
I want to turn the above into this:
<root>
<fields>
<a>foo</a>
<c>baz</c>
</fields>
</root>
我知道我可以像这样更新值:
I know I can update the value like this:
SELECT UpdateXML(my_xml, '/root/fields/b/text()', 'baz')
INTO my_xml_updated
FROM DUAL;
结果是:
<root>
<fields>
<a>foo</a>
<b>baz</b>
</fields>
</root>
但是如何将节点名称从 更新为 (不影响节点的内容)?b>
But how can I update the node name from <b> to <c> (without affecting the contents of the node)?
推荐答案
一种选择是使用 XMLTRANSFORM 重命名节点.另见例如使用 XSLT 重命名节点.
One option is to use XMLTRANSFORM to rename a node. See also e.g. Rename nodes with XSLT.
with
xmldata as (select xmltype('<root>
<fields>
<a>foo</a>
<b>bar</b>
</fields>
</root>') val from dual),
stylesheet as (select '<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Identity transformation -->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<!-- Identity transformation overridden for element b -->
<xsl:template match="b">
<xsl:element name="c">
<xsl:apply-templates select="node()|@*"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>' val from dual)
select xmltransform(x.val, s.val) from xmldata x, stylesheet s;
输出:
XMLTRANSFORM(X.VAL,S.VAL)
--------------------------------------------------------------------------------
<root>
<fields>
<a>foo</a>
<c>bar</c>
</fields>
</root>
这篇关于如何重命名 Oracle XMLTYPE 节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何重命名 Oracle XMLTYPE 节点
基础教程推荐
- 什么是 orradiag_<user>文件夹? 2022-01-01
- 在多列上分布任意行 2021-01-01
- 在 MySQL 中:如何将表名作为存储过程和/或函数参数传递? 2021-01-01
- 二进制文件到 SQL 数据库 Apache Camel 2021-01-01
- 表 './mysql/proc' 被标记为崩溃,应该修复 2022-01-01
- oracle区分大小写的原因? 2021-01-01
- 如何在 SQL 中将 Float 转换为 Varchar 2021-01-01
- 如何根据该 XML 中的值更新 SQL 中的 XML 2021-01-01
- mysql选择动态行值作为列名,另一列作为值 2021-01-01
- MySQL 中的类型:BigInt(20) 与 Int(20) 2021-01-01
