将xmltype插入指定位置的xmltype [PL / SQL]

问题描述 投票:3回答:3

我在将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中移动它。

谢谢!

sql oracle plsql oracle11g
3个回答
2
投票

您可以使用此处提到的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>

2
投票

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;

0
投票

这里是使用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;
© www.soinside.com 2019 - 2024. All rights reserved.