[英]xmltype - delete parent node without child
是否可以删除xmltype变量中没有子节点的父节点?
例如:
declare
v1 xmltype := xmltype('<root>
<parent1>
<child>1</child>
<child>2</child>
</parent1>
<parent2>
<child2>3</child2>
</parent2>
</root>');
begin
-- some code...
dbms_output.put_line(v1.getClobVal());
end;
我的目的是在输出中获取此信息:
<root>
<child>1</child>
<child>2</child>
<parent2>
<child2>3</child2>
</parent2>
</root>
我尝试使用deleteXML函数,但它也会删除子节点。
因此,如果有人能够帮助我,我将不胜感激:)
可以通过使用DELETEXML来删除节点,例如:
select DELETEXML(v1,'/root/parent1') into v1 from dual;
但是,删除parent1也会删除父1的所有子节点。如果要保留parent1的子节点,则首先必须复制这些节点,然后再删除parent1。 您可以这样操作:
declare
v1 xmltype := xmltype('<root>
<parent1>
<child>1</child>
<child>2</child>
</parent1>
<parent2>
<child2>3</child2>
</parent2>
</root>');
begin
--Append all child nodes of parent1 as child nodes of root before the parent2 node
select insertxmlbefore(v1, '/root/parent2',extract(v1,'/root/parent1/child::node()')) into v1 from dual;
--remove parent 1
select DELETEXML(v1,'/root/parent1') into v1 from dual;
dbms_output.put_line(v1.getClobVal());
end;
这将为您提供:
<root>
<child>1</child>
<child>2</child>
<parent2>
<child2>3</child2>
</parent2>
</root>
希望能有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.