使用Oracle pl / sql使用命名空间从xml clob中提取值

时间:2017-06-13 08:00:08

标签: xml oracle clob

我目前正在尝试从clob列中提取值,但始终将结果设为null

我尝试了很多可能的方案,但对我而言,它总是以null返回。

附件是实际的xml

<TenderOffer xmlns="http://xmlns.oracle.com/apps/otm">
    <Shipment>
        <ShipmentHeader/>
        <SEquipment/>
        <ShipmentStop/>
        <ShipmentStop/>
        <Location/>
        <Location/>
        <Release/>
        <RATE_OFFERING>
            <RATE_OFFERING_ROW>
                <USER_CLASSIFICATION3>ZXF</USER_CLASSIFICATION3>
            </RATE_OFFERING_ROW>
        </RATE_OFFERING>
    </Shipment>
    </TenderOffer>

以下是实际查询,

select 
        itc.element_name,
        extractvalue(XMLTYPE(XML_BLOB), '/TenderOffer/Shipment/RATE_OFFERING/RATE_OFFERING_ROW/USER_CLASSIFICATION3/text()'),
    XMLTYPE(XML_BLOB)
        from i_transaction itc
        where itc.i_transaction_no = 31553115
            and rownum = 1

2 个答案:

答案 0 :(得分:2)

您更新的XML具有命名空间,最终揭示了该问题。你需要specify the namespace as part of the XML extraction,这对于XMLTable方法来说更简单;在这种情况下,您可以将其视为默认命名空间:

select itc.element_name, x.user_classification3
from i_transaction itc
cross join xmltable(
  xmlnamespaces(default 'http://xmlns.oracle.com/apps/otm'),
    '/TenderOffer/Shipment/RATE_OFFERING/RATE_OFFERING_ROW'
  passing xmltype(itc.xml_blob)
  columns user_classification3 varchar2(10) path 'USER_CLASSIFICATION3'
) x
where itc.i_transaction_no = 31553115
and rownum = 1;

ELEMENT_NA USER_CLASS
---------- ----------
dummy      ZXF       

或使用XMLQuery:

select itc.element_name, xmlquery(
  'declare default element namespace "http://xmlns.oracle.com/apps/otm"; (: :)
    /TenderOffer/Shipment/RATE_OFFERING/RATE_OFFERING_ROW/USER_CLASSIFICATION3/text()'
  passing xmltype(itc.xml_blob)
  returning content
) x
from i_transaction itc
where itc.i_transaction_no = 31553115
and rownum = 1;

ELEMENT_NA X                                                                               
---------- --------------------------------------------------------------------------------
dummy      ZXF                                                                             

如果你想继续使用已弃用的extractvalue()函数,你可以再次提供命名空间作为参数as shown in the documentation

select itc.element_name,
  extractvalue(xmltype(xml_blob),
    '/TenderOffer/Shipment/RATE_OFFERING/RATE_OFFERING_ROW/USER_CLASSIFICATION3/text()',
    'xmlns="http://xmlns.oracle.com/apps/otm"')
from i_transaction itc where itc.i_transaction_no = 31553115 and rownum = 1;

答案 1 :(得分:0)

在您的代码中,您使用XML_BLOB作为变量 - 所以我假设了BLOB数据类型,需要转换为CLOB。但是,如果您有CLOB值,则可以跳过使用BLOB_TO_CLOB函数:

Oracle 11安装程序

CREATE OR REPLACE FUNCTION BLOB_TO_CLOB( b BLOB )
RETURN CLOB 
IS
  c            CLOB;
  n            INTEGER     := 1;
  w   CONSTANT PLS_INTEGER := 32767;
  len CONSTANT INTEGER     := LENGTH( b );
BEGIN
  IF b IS NULL THEN
    RETURN NULL;
  END IF;

  IF len = 0 THEN
    RETURN EMPTY_CLOB();
  END IF;

  DBMS_LOB.CREATETEMPORARY( c, TRUE );
  WHILE ( n + w <= len ) LOOP
    DBMS_LOB.WRITEAPPEND( c, w, UTL_RAW.CAST_TO_VARCHAR2( DBMS_LOB.SUBSTR( b, w, n ) ) );
    n := n + w;
  END LOOP;
  DBMS_LOB.WRITEAPPEND( c, len - n + 1, UTL_RAW.CAST_TO_VARCHAR2( DBMS_LOB.SUBSTR( b, len - n + 1, n ) ) );
  RETURN c;
END;
/

CREATE TABLE blob_test ( value BLOB );

INSERT INTO blob_test VALUES (
  UTL_RAW.CAST_TO_RAW(
    '<Header>' ||
      '<SecondHeader>' ||
        '<ThirdHeader>' ||
          '<FourthHeader>' ||
            '<Value1>abcd</Value1>' ||
            '<Value2>xyz</Value2>' ||
            '<Value3>abxy</Value3>' ||
          '</FourthHeader>' ||
        '</ThirdHeader>' ||
      '</SecondHeader>' ||
    '</Header>'
  )
);

<强>查询

不推荐使用

EXTRACTEXTRACTVALUE - 请改为使用XMLTABLE

SELECT Value1, Value2, Value3
FROM   blob_test b
       CROSS JOIN
       XMLTABLE(
         '/Header/SecondHeader/ThirdHeader/FourthHeader'
         PASSING XMLTYPE( BLOB_TO_CLOB( b.value ) )
         COLUMNS Value1 VARCHAR2(50) PATH 'Value1/text()',
                 Value2 VARCHAR2(50) PATH 'Value2/text()',
                 Value3 VARCHAR2(50) PATH 'Value3/text()'
       ) x;

<强>输出

VALUE1 VALUE2 VALUE3
------ ------ ------
abcd   xyz    abxy

查询2

如果您确实要使用EXTRACTVALUE,可以从XPath中移除/text(),因为EXTRACTVALUE会为您执行此操作(但它仍然可以在那里使用)并转换BLOBCLOB

SELECT EXTRACTVALUE(
         XMLTYPE( BLOB_TO_CLOB( value ) ), 
         '/Header/SecondHeader/ThirdHeader/FourthHeader/Value3'
       )
FROM   blob_test

查询 - 更新

@AlexPoole的简化。由于XMLTYPE constructor可以使用BLOB_TO_CLOB和字符集ID,因此无需调用BLOB

SELECT Value1, Value2, Value3
FROM   blob_test b
       CROSS JOIN
       XMLTABLE(
         '/Header/SecondHeader/ThirdHeader/FourthHeader'
         PASSING XMLTYPE( b.value, NLS_CHARSET_ID('UTF8') )
         COLUMNS Value1 VARCHAR2(50) PATH 'Value1/text()',
                 Value2 VARCHAR2(50) PATH 'Value2/text()',
                 Value3 VARCHAR2(50) PATH 'Value3/text()'
       ) x;