用 0 替换空节点的值
Replace value of an empty node with 0
以下是我的样本XML:
<root>
<item>old_value</item>
<item/>
<root>
我在 XMLTYPE 列中有一个 table 和 XML 数据,到目前为止我已经设法查询了至少有一个空 item
的行节点,如上所示。
现在,我想用 <item>0</item>
替换空节点,这样我的 XML 看起来像:
<root>
<item>old_value</item>
<item>0</item>
<root>
create table test_xml(id number, doc xmltype);
insert into test_xml values(1,xmltype('<root><item>old_value</item><item/><item1/><item2/></root>'));
insert into test_xml values(2,xmltype('<root><item>old_value</item><item/><item1/><item2/></root>'));
UPDATE test_xml SET doc =
UPDATEXML(doc,
'/root/item[not(text())]',xmltype('<item>0</item>')) where id =1;
---
UPDATE test_xml SET doc =
appendChildXML(doc,
'/root/*[not(text())]',xmltype('<any>0</any>').extract('/any/text()'))
第一次更新用零项替换空项
第二次更新将 text-node =0 附加到所有空节点。
问题中 link 1 中的示例仅适用于文档中的一个元素 item。如果有多个 item 元素,如示例;应该使用稍微修改过的 XMLQuery:
select xmlquery(
'copy $d := .
modify (
for $i in $d/root/item
where string-length($i) = 0
return replace value of node $i with $val
)
return $d'
passing xmlparse(document '<root><item>old_value</item><item/><item/></root>')
,'new_value' as "val"
returning content
) as result
from dual;
<root><item>old_value</item><item>new_value</item><item>new_value</item></root>
以下是我的样本XML:
<root>
<item>old_value</item>
<item/>
<root>
我在 XMLTYPE 列中有一个 table 和 XML 数据,到目前为止我已经设法查询了至少有一个空 item
的行节点,如上所示。
现在,我想用 <item>0</item>
替换空节点,这样我的 XML 看起来像:
<root>
<item>old_value</item>
<item>0</item>
<root>
create table test_xml(id number, doc xmltype);
insert into test_xml values(1,xmltype('<root><item>old_value</item><item/><item1/><item2/></root>'));
insert into test_xml values(2,xmltype('<root><item>old_value</item><item/><item1/><item2/></root>'));
UPDATE test_xml SET doc =
UPDATEXML(doc,
'/root/item[not(text())]',xmltype('<item>0</item>')) where id =1;
---
UPDATE test_xml SET doc =
appendChildXML(doc,
'/root/*[not(text())]',xmltype('<any>0</any>').extract('/any/text()'))
第一次更新用零项替换空项
第二次更新将 text-node =0 附加到所有空节点。
问题中 link 1 中的示例仅适用于文档中的一个元素 item。如果有多个 item 元素,如示例;应该使用稍微修改过的 XMLQuery:
select xmlquery(
'copy $d := .
modify (
for $i in $d/root/item
where string-length($i) = 0
return replace value of node $i with $val
)
return $d'
passing xmlparse(document '<root><item>old_value</item><item/><item/></root>')
,'new_value' as "val"
returning content
) as result
from dual;
<root><item>old_value</item><item>new_value</item><item>new_value</item></root>