oracle XMLQUERY -当不为空时插入节点

zbwhf8kr  于 2023-11-17  发布在  Oracle
关注(0)|答案(1)|浏览(100)

示例代码:

declare
l_xml_response xmltype;
i_cl clob;
l_flag number(1) := 1;
l_product_type varchar2(5) := 'P';
begin
  l_xml_response := xmltype('<?xml version="1.0"?>
<main-node>
  <node1>
    <node2>
      <node3 product-type="PGZ">
        <id>3</id>
      </node3>
    </node2>
  </node1>
</main-node>');

        SELECT  XMLQUERY
                ('
                   copy $res := $req
                   modify insert nodes
                   (
                      <amount>{$amount1}</amount>,
                      <amount-1>{$amount2}</amount-1>,
                      <amount-2>{$amount3}</amount-2>,
                      <amount-3>{$amount4}</amount-3>
                   )
                   after $res/main-node/node1/node2/node3/id
                      
                   return $res
                 '
                 PASSING l_xml_response    AS "req",
                         1    AS "amount1",
                         2    AS "amount2",
                         3    AS "amount3",
                         CASE
                           WHEN NVL(l_flag, 0) = 1 AND l_product_type = 'P' THEN 456
                         END               AS "amount4"
                 RETURNING CONTENT
                )
        INTO    l_xml_response
        FROM    dual;
   
   SELECT  XMLSERIALIZE
            (
              CONTENT l_xml_response
              AS CLOB
              VERSION '1.0'
              INDENT SIZE = 2 
              HIDE DEFAULTS
            )
  INTO    i_cl
  FROM    dual;

  dbms_output.put_line( i_cl);
end;
/

字符串
我想做的是只在不为空的情况下添加节点amount4。这个xquery添加了空节点(<amount-3/>)。它并不是破坏任何东西的东西,只是试图不发送不必要的空节点。
也许有人有一些快速,聪明的解决办法。
谢谢.

cld4siwp

cld4siwp1#

在FLWOR表达式中使用if .. then .. else ..

declare
l_xml_response xmltype;
i_cl clob;
l_flag number(1) := 1;
l_product_type varchar2(5) := 'P';
begin
  l_xml_response := xmltype('<?xml version="1.0"?>
<main-node>
  <node1>
    <node2>
      <node3 product-type="PGZ">
        <id>3</id>
      </node3>
    </node2>
  </node1>
</main-node>');

        SELECT  XMLQUERY
                ('
                   copy $res := $req
                   modify insert nodes
                   (
                     <amount>{$amount1}</amount>,
                     <amount-1>{$amount2}</amount-1>,
                     <amount-2>{$amount3}</amount-2>,
                     if ($amount4)
                     then <amount-3>{$amount4}</amount-3>
                     else ()
                   )
                   after $res/main-node/node1/node2/node3/id
                   return $res
                 '
                 PASSING l_xml_response    AS "req",
                         1    AS "amount1",
                         2    AS "amount2",
                         3    AS "amount3",
                         NULL AS "amount4"
                 RETURNING CONTENT
                )
        INTO    l_xml_response
        FROM    dual;
   
   SELECT  XMLSERIALIZE
            (
              CONTENT l_xml_response
              AS CLOB
              VERSION '1.0'
              INDENT SIZE = 2 
              HIDE DEFAULTS
            )
  INTO    i_cl
  FROM    dual;

  dbms_output.put_line( i_cl);
end;
/

字符串
产出:

<?xml version="1.0"?>
<main-node>
  <node1>
    <node2>
      <node3 product-type="PGZ">
        <id>3</id>
        <amount>1</amount>
        <amount-1>2</amount-1>
        <amount-2>3</amount-2>
      </node3>
    </node2>
  </node1>
</main-node>


fiddle

相关问题