問題描述
我使用的是 11.2 版的 Oracle XE.
I am using Oracle XE in version 11.2.
我有這樣的 XML:
<root>
<x a="a"/>
<x a="b"/>
<x a="c"/>
</root>
不,我想將屬性 b
添加到每個元素 x
,其值取自序列,但它應該為每個元素采用新值.預期結果是:
No I would like to add attribute b
to each element x
with value taken from sequence, but it should take new value for each element. Expected result is:
<root>
<x a="a" b="1"/>
<x a="b" b="2"/>
<x a="c" b="3"/>
</root>
我發現要向 XML 添加屬性,我可以使用 insertchildxml
但在將相同的(第一個)值從序列添加到所有屬性 b
.我找不到如何為每個單獨的元素 x
調用此函數.
I've found that to add attribute to XML I can use insertchildxml
but in is adding the same (frist) value from sequence to all attributes b
. I can't find how to call this function for each individual element x
.
我將不勝感激.
推薦答案
我終于找到了一些解決方案,關鍵是使用 XMLTable() 函數.這是我的代碼:
I've finaly found some solution and key to it was to use XMLTable() function. Here is my code:
declare
v_inXML xmltype;
v_tmpXML xmltype;
v_withIdXML xmltype;
v_outXML xmltype;
BEGIN
v_inXML := XMLType('<root><x a="a"/><x a="b"/><x a="c"/></root>');
v_withIdXML := XMLType('<root/>');
v_outXML := XMLType('<root/>');
for c_rec in (
select *
from XMLTable('for $i in /root/x
return $i'
passing v_inXML
columns x xmltype path '/x'
)
)
loop
select insertchildxml(c_rec.x,'//x', '@b', pckg_ent_pk_seq.nextval) into v_tmpXML from dual;
select insertchildxml(v_withIdXML, '/root', 'x', v_tmpXML) into v_withIdXML from dual;
end loop;
select updatexml(v_outXML, '/root', v_withIdXML) into v_outXML from dual;
dbms_output.put_line(v_outXML.getClobVal());
END;
結果是:
<root><x a="a" b="92"/><x a="b" b="93"/><x a="c" b="94"/></root>
這篇關于使用取自序列的值將屬性添加到 xmltype的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!