如何从XML Clob中删除节点(标签)

时间:2019-04-18 21:48:33

标签: xml oracle nodes xmlnode

我已经编写了一个生成XML数据的PL / SQL(Oracle)脚本;当前,XML数据已写入DBMS_output。我有一个产生不需要的TAG的子查询XMLAGG,我正在验证是否有一种方法可以在输出结果之前删除该标签。

我曾经尝试使用DELETEXMLDELETE NODE,但是这是我第一次在测试时收到错误;我知道这是用户错误编码。

这是我写的:

declare
    c_xmltype xmltype;
    f_xmltype xmltype;
    c_ctx dbms_xmlgen.ctxhandle;

procedure print_clob(p_clob in clob) is
    v_offset number :=1;
    v_chunk_size number :=11000;
begin
    loop
        exit when v_offset > dbms_lob.getlength(p_clob);
        dbms_output.put_line(dbms_lob.substr(p_clob, v_chunk_size, v_offset));
        v_offset := v_offset + v_chunk_size;
    end loop;
end print_clob;

begin
    dbms_output.enable(null);
    c_ctx := dbms_xmlgen.newcontext('select
                                     a.id as id_number
                                     , (select xmlagg(
                                        xmlelement("DETAIL",
                                        xmlforest(
                                                   b.line_number as line_number
                                                  ,to_char(b.from_date, ''MM/DD/YYYY'') as begin_date
                                                  ,to_char(b.to_date, ''MM/DD/YYYY'') as end_date
                                                  ,b.r_code as reason_code
                                                  ,'''' as reason_code_2
                                                  ,'''' as ndc_code
                                                  ,''1'' as match_method
                                                  ,''01'' as unit_type
                                                  ,b.quantity as units
                                                  ,'''' as auth_class
                                                  ,'''' as auth_class_use
                                                  ,'''' as authorization_level
                                                  ,''D'' as status
                                                  ,''D58'' as status_reason
                                                  , 999902 as user_id
                                                  ,to_char(sysdate, ''MM/DD/YYYY'') as date_timestamp
                                                 )
                                        ))
                                        from table_b b
                                        inner join table_c c
                                        on c.id = b.id
                                        and c.line_number = b.line_number
                                        where b.claim_id = a.claim_id
                                        ) as remove_tag

                                     from table_a a

                                     where
                                     a.id = ''100''

                                     order by a.id
                                     '
                                    );

    dbms_xmlgen.setnullhandling(c_ctx, dbms_xmlgen.empty_tag);
    dbms_xmlgen.setrowsettag(c_ctx, 'AUTHORIZATIONS');
    dbms_xmlgen.setrowtag(c_ctx, 'AUTHORIZATION');

    c_xmltype := dbms_xmlgen.getxmltype(c_ctx);
    dbms_xmlgen.closecontext(c_ctx);

    print_clob(c_xmltype.getclobval);

end;

输出产生:

<AUTHORIZATIONS>
 <AUTHORIZATION>
  <ID>100</ID>
  <REMOVE_TAG>
            <AUTHORIZATION_SERVICES>
                <LINE_NUMBER>1</LINE_NUMBER>
                <BEGIN_DATE>01/14/2019</BEGIN_DATE>
                <END_DATE>01/14/2019</END_DATE>
                <REASON_CODE>99213</REASON_CODE>
                <REASON_CODE_2/>
                <NDC_CODE/>
                <MATCH_METHOD>1</MATCH_METHOD>
                <UNIT_TYPE>01</UNIT_TYPE>
                <UNITS>1</UNITS>
                <AUTH_CLASS/>
                <AUTH_CLASS_USE/>
                <AUTHORIZATION_LEVEL/>
                <STATUS>D</STATUS>
                <STATUS_REASON>D58</STATUS_REASON>
                <USER_ID>999902</USER_ID>
                <DATE_TIMESTAMP>04/18/2019</DATE_TIMESTAMP>
            </AUTHORIZATION_SERVICES>
            <AUTHORIZATION_SERVICES>
                <LINE_NUMBER>2</LINE_NUMBER>
                <BEGIN_DATE>01/14/2019</BEGIN_DATE>
                <END_DATE>01/14/2019</END_DATE>
                <REASON_CODE>17110</REASON_CODE>
                <REASON_CODE_2/>
                <NDC_CODE/>
                <MATCH_METHOD>1</MATCH_METHOD>
                <UNIT_TYPE>01</UNIT_TYPE>
                <UNITS>1</UNITS>
                <AUTH_CLASS/>
                <AUTH_CLASS_USE/>
                <AUTHORIZATION_LEVEL/>
                <STATUS>D</STATUS>
                <STATUS_REASON>D58</STATUS_REASON>
                <USER_ID>999902</USER_ID>
                <DATE_TIMESTAMP>04/18/2019</DATE_TIMESTAMP>
            </AUTHORIZATION_SERVICES>
  </REMOVE_TAG>
 </AUTHORIZATION>
</AUTHORIZATIONS>

XMLAGG子查询生成不需要的“额外”标签,我将其命名为“ REMOVE_TAG”。一旦结果显示在DBMS_Output中,我目前正在手动删除该标签。

是否可以添加到脚本并使用DELETEXMLDELETE NODE,以便在写到目录时可以完全自动化?

0 个答案:

没有答案