[英]Insert xmltype into xmltype in specified place [PL/SQL]
我在將 xmltype 插入到 pl/sql 中指定位置的另一個 xmltype 時遇到問題。
第一個變量 v_xml 具有以下形式:
<ord>
<head>
<ord_code>123</ord_code>
<ord_date>01-01-2015</ord_date>
</head>
</ord>
第二個 v_xml2:
<pos>
<pos_code>456</pos_code>
<pos_desc>description</pos_desc>
</pos>
我的目的是得到這樣的東西:
<ord>
<head>
<ord_code>123</ord_code>
<ord_date>01-01-2015</ord_date>
</head>
<!-- put the second variable in this place - after closing <head> tag -->
<pos>
<pos_code>456</pos_code>
<pos_desc>description</pos_desc>
</pos>
</ord>
我應該如何處理我的代碼?
declare
v_xml xmltype;
v_xml2 xmltype;
begin
-- some code
-- some code
-- v_xml and v_xml2 has the form as I define above
end;
有人能幫我解決這個問題嗎? 據我所知,有像 insertchildxml、appendchildxml 這樣的函數……我在純 SQL 中幾乎找不到解決方案,但我不知道如何在 PL/SQL 中移動它。
謝謝!
您可以使用提到的appendChildXML ,如下所示:
declare
v_xml xmltype := xmltype('<ord>
<head>
<ord_code>123</ord_code>
<ord_date>01-01-2015</ord_date>
</head>
</ord>');
v_xml2 xmltype:= xmltype('<pos>
<pos_code>456</pos_code>
<pos_desc>description</pos_desc>
</pos>');
v_output xmltype;
begin
select appendChildXML(v_xml, 'ord', v_xml2)
into v_output from dual;
-- output result
dbms_output.put_line( substr( v_output.getclobval(), 1, 1000 ) );
end;
輸出:
<ord>
<head>
<ord_code>123</ord_code>
<ord_date>01-01-2015</ord_date>
</head>
<pos>
<pos_code>456</pos_code>
<pos_desc>description</pos_desc>
</pos>
</ord>
appendChildXML在 12.1 已棄用
所以這是一個使用XMLQuery的解決方案
DECLARE
l_head_xml XMLTYPE := XMLTYPE.CREATEXML('<ord>
<head>
<ord_code>123</ord_code>
<ord_date>01-01-2015</ord_date>
</head>
</ord>');
l_pos_xml XMLTYPE := XMLTYPE.CREATEXML('<pos>
<pos_code>456</pos_code>
<pos_desc>description</pos_desc>
</pos>');
l_complete_xml XMLTYPE;
BEGIN
SELECT XMLQUERY('for $i in $h/ord/head
return <ord>
{$i}
{for $j in $p/pos
return $j}
</ord>'
PASSING l_head_xml AS "h",
l_pos_xml AS "p"
RETURNING CONTENT)
INTO l_complete_xml
FROM dual;
dbms_output.put_line(l_complete_xml.getstringval());
END;
這是使用 XMLQuery 的一種解決方案
DECLARE
l_src XMLTYPE:=XMLTYPE('<ord>
<head>
<ord_code>123</ord_code>
<ord_date>01-01-2015</ord_date>
</head>
</ord>');
l_dst XMLTYPE:=XMLTYPE('<pos>
<pos_code>456</pos_code>
<pos_desc>description</pos_desc>
</pos>');
l_final CLOB;
BEGIN
SELECT XMLQUERY('copy $tmp := $p1 modify
insert node $p2 as last into $tmp
return $tmp' PASSING l_src as "p1",l_dst as "p2" RETURNING CONTENT).getClobval() into l_final from dual;
DBMS_OUTPUT.PUT_LINE(l_final);
END;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.