从XML中提取数据(CLOB类型)

时间:2015-04-07 22:13:10

标签: sql xml oracle select soap

我需要从表BATCH_ID和列BODY(数据类型:CLOB)中提取FND_SOA_BODY_PIECE的数据,其中包含XML数据

    <?xml version="1.0" encoding="UTF-8"?>        
      <SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
        <SOAP-ENV:Header>
        </SOAP-ENV:Header>
        <SOAP-ENV:Body>
            <tns1:InputParameters xmlns:tns1="http://xmlns.oracle.com/apps/gl/soaprovider/plsql/geas_gl_journal_pkg/geas_get_gl_data/">
                  <tns1:P_BATCH_TBL>
                         <tns1:P_BATCH_TBL_ITEM>
                              <tns1:BATCH_ID>85380</tns1:BATCH_ID>
                         </tns1:P_BATCH_TBL_ITEM>
                         <tns1:P_BATCH_TBL_ITEM>
                              <tns1:BATCH_ID>85345</tns1:BATCH_ID>
                         </tns1:P_BATCH_TBL_ITEM>
                         <tns1:P_BATCH_TBL_ITEM>
                              <tns1:BATCH_ID>85364</tns1:BATCH_ID>
                         </tns1:P_BATCH_TBL_ITEM>
                         <tns1:P_BATCH_TBL_ITEM>
                              <tns1:BATCH_ID>85356</tns1:BATCH_ID>
                         </tns1:P_BATCH_TBL_ITEM>
                         <tns1:P_BATCH_TBL_ITEM>
                              <tns1:BATCH_ID>85374</tns1:BATCH_ID>
                         </tns1:P_BATCH_TBL_ITEM>
                         <tns1:P_BATCH_TBL_ITEM>
                              <tns1:BATCH_ID>85358</tns1:BATCH_ID>
                         </tns1:P_BATCH_TBL_ITEM>
                  </tns1:P_BATCH_TBL>
            </tns1:InputParameters>
        </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>

我在下面尝试了这个,但它只给了我空值

    SELECT xmltype(fsa.body).EXTRACT('/Envelope/Body/InputParameters/P_BATCH_TBL/P_BATCH_TBL_ITEM/BATCH_ID').getStringVal() FROM fnd_soa_body_piece;

有人可以帮忙解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

首先,请发布有效的XML,因为您显示的内容看起来像浏览器的屏幕,并且缺少结束标记并且包含无效字符。如果您的xml具有名称空间引用,则需要将它们包含在XPath中,并作为extract()函数的参数。这是一个例子:

with fnd_soa_body_piece as (
select xmltype(
      '<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
           xmlns:xsd="http://www.w3.org/2001/XMLSchema"
           xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" 
           xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
        <SOAP-ENV:Header>
        <SOAP-ENV:Body>
            <tns1:InputParameters xmlns:tns1="http://xmlns.oracle.com/apps/gl/soaprovider/plsql/geas_gl_journal_pkg/geas_get_gl_data/">
                  <tns1:P_BATCH_TBL>
                        <tns1:P_BATCH_TBL_ITEM>
                              <tns1:BATCH_ID>85380</tns1:BATCH_ID>
                         </tns1:P_BATCH_TBL_ITEM>
                        <tns1:P_BATCH_TBL_ITEM>
                              <tns1:BATCH_ID>85345</tns1:BATCH_ID>
                        </tns1:P_BATCH_TBL_ITEM>
                        <tns1:P_BATCH_TBL_ITEM>
                              <tns1:BATCH_ID>85364</tns1:BATCH_ID>
                        </tns1:P_BATCH_TBL_ITEM>
                        <tns1:P_BATCH_TBL_ITEM>
                              <tns1:BATCH_ID>85356</tns1:BATCH_ID>
                        </tns1:P_BATCH_TBL_ITEM>
                        <tns1:P_BATCH_TBL_ITEM>
                              <tns1:BATCH_ID>85374</tns1:BATCH_ID>
                        </tns1:P_BATCH_TBL_ITEM>
                        <tns1:P_BATCH_TBL_ITEM>
                              <tns1:BATCH_ID>85358</tns1:BATCH_ID>
                        </tns1:P_BATCH_TBL_ITEM>
                  </tns1:P_BATCH_TBL>
            </tns1:InputParameters>
        </SOAP-ENV:Body>
        </SOAP-ENV:Header>
    </SOAP-ENV:Envelope>') body from dual
    )
    select extract(body,'/SOAP-ENV:Envelope/SOAP-ENV:Header/SOAP-ENV:Body/tns1:InputParameters/tns1:P_BATCH_TBL/tns1:P_BATCH_TBL_ITEM/tns1:BATCH_ID',
    '  xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" 
       xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" 
       xmlns:tns1="http://xmlns.oracle.com/apps/gl/soaprovider/plsql/geas_gl_journal_pkg/geas_get_gl_data/"').getStringVal()
  from fnd_soa_body_piece

更新

要将结果作为SQL行集返回,您需要使用XMLTABLE函数,例如:

with fnd_soa_body_piece as (
select xmltype(
      '<?xml version="1.0" encoding="UTF-8"?> 
      <SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
           xmlns:xsd="http://www.w3.org/2001/XMLSchema"
           xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" 
           xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
        <SOAP-ENV:Header>
        <SOAP-ENV:Body>
            <tns1:InputParameters xmlns:tns1="http://xmlns.oracle.com/apps/gl/soaprovider/plsql/geas_gl_journal_pkg/geas_get_gl_data/">
                  <tns1:P_BATCH_TBL>
                        <tns1:P_BATCH_TBL_ITEM>
                              <tns1:BATCH_ID>85380</tns1:BATCH_ID>
                         </tns1:P_BATCH_TBL_ITEM>
                        <tns1:P_BATCH_TBL_ITEM>
                              <tns1:BATCH_ID>85345</tns1:BATCH_ID>
                        </tns1:P_BATCH_TBL_ITEM>
                        <tns1:P_BATCH_TBL_ITEM>
                              <tns1:BATCH_ID>85364</tns1:BATCH_ID>
                        </tns1:P_BATCH_TBL_ITEM>
                        <tns1:P_BATCH_TBL_ITEM>
                              <tns1:BATCH_ID>85356</tns1:BATCH_ID>
                        </tns1:P_BATCH_TBL_ITEM>
                        <tns1:P_BATCH_TBL_ITEM>
                              <tns1:BATCH_ID>85374</tns1:BATCH_ID>
                        </tns1:P_BATCH_TBL_ITEM>
                        <tns1:P_BATCH_TBL_ITEM>
                              <tns1:BATCH_ID>85358</tns1:BATCH_ID>
                        </tns1:P_BATCH_TBL_ITEM>
                  </tns1:P_BATCH_TBL>
            </tns1:InputParameters>
        </SOAP-ENV:Body>
        </SOAP-ENV:Header>
    </SOAP-ENV:Envelope>') body from dual
    )
    select t.batch_id
    from fnd_soa_body_piece,
    xmltable( xmlnamespaces(
              'http://schemas.xmlsoap.org/soap/encoding/' as "SOAP-ENC",
              'http://schemas.xmlsoap.org/soap/envelope/' as "SOAP-ENV",
              'http://xmlns.oracle.com/apps/gl/soaprovider/plsql/geas_gl_journal_pkg/geas_get_gl_data/' as "tns1" ),
              '/SOAP-ENV:Envelope/SOAP-ENV:Header/SOAP-ENV:Body/tns1:InputParameters/tns1:P_BATCH_TBL/tns1:P_BATCH_TBL_ITEM/tns1:BATCH_ID'
              passing fnd_soa_body_piece.body columns batch_id number(10) PATH 'text()') t