如何使用 Oracle sql 在 xml 字段中提取字符串

How to extract string in xml field using Oracle sql

我想从我的 Oracle table 中的 CLOB 数据类型 xml 字符串中提取错误代码。但是当我试图提取它时,它给我一个错误

ORA-19114: XPST0003 - error during parsing the XQuery expression: 
LPX-00801: XQuery syntax error at 'return'
1   .w3.org/2001/XMLSchema-instance";for $i in //<Header errorCode= return $i

这是我的 xml 字符串,它存储在 PROVISIONING_LOG table 的 RESPONSE 列中:

<?xml version="1.0" encoding="ISO-8859-15"?>
<Order xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="sp.xsd">
  <Header dateOfExecution="2014-10-01 00:03:06" externalId1="" messageId="1" orderId="0021417905" serviceProviderId="SP605" status="V" type="RES" errorCode="224" errorString="Subscription 201407162076606 not found." />
  <Body>
    <Oli>
      <OliControl oliId="1" subscriptionId="201407162076606" errorCode="224" errorString="Subscription 201407162076606 not found." status="V" />
      <DEASUB />
    </Oli>
  </Body>
</Order>

这是我试过的查询:

 select x.*
from   TEMP_PROVISIONING_LOG PL
       CROSS JOIN XMLTable(XMLNAMESPACES (
      'http://core.signup.data.soap.CDRator.com/xsd' as "Header"),
                            'for $i in //Order return $i'
                            passing XMLType(PL.RESPONSE)
                            columns error_code varchar2(100) path 'Header/@errorCode') x;

我想你在找这个:

with sample_data as (select xmltype('<?xml version="1.0" encoding="ISO-8859-15"?>
<Order xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="sp.xsd">
  <Header dateOfExecution="2014-10-01 00:03:06" externalId1="" messageId="1" orderId="0021417905" serviceProviderId="SP605" status="V" type="RES" errorCode="224" errorString="Subscription 201407162076606 not found." />
  <Body>
    <Oli>
      <OliControl oliId="1" subscriptionId="201407162076606" errorCode="224" errorString="Subscription 201407162076606 not found." status="V" />
      <DEASUB />
    </Oli>
  </Body>
</Order>') response from dual)
select x.*
from   sample_data tpl
       cross join xmltable (XMLNAMESPACES ('http://www.w3.org/2001/XMLSchema-instance' as "Header"),
                            '/Order' passing tpl.response
                            columns error_code varchar2(100) path 'Header/@errorCode') x;

ERROR_CODE                                                                      
--------------------------------------------------------------------------------
224              

预计到达时间:试试这个:

with TEMP_PROVISIONING_LOG as (select '<?xml version="1.0" encoding="ISO-8859-15"?>
<Order xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="sp.xsd">
  <Header dateOfExecution="2014-10-01 00:03:06" externalId1="" messageId="1" orderId="0021417905" serviceProviderId="SP605" status="V" type="RES" errorCode="224" errorString="Subscription 201407162076606 not found." />
  <Body>
    <Oli>
      <OliControl oliId="1" subscriptionId="201407162076606" errorCode="224" errorString="Subscription 201407162076606 not found." status="V" />
      <DEASUB />
    </Oli>
  </Body>
</Order>' response from dual)
select *
from   TEMP_PROVISIONING_LOG PL,
       XMLTable(XMLNAMESPACES (
      'http://core.signup.data.soap.CDRator.com/xsd' as "Header"),
                            'for $i in //Order return $i'
                            passing XMLType.createxml(PL.RESPONSE)
                            columns error_code varchar2(100) path 'Header/@errorCode') x;