无法从 oracle sql 中的 xml 值中提取数据

Can't extract the data from xml values in oracle sql

我有以下 2 个 xml 值,它们相似并且存储在 request_xml 列中并且是 clob 数据类型:

<?xml version='1.0' encoding='utf-8'?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
      <ns5:updateRechargeTicket xmlns:ns5="http://service.soap.CDRator.com" xmlns:ns2="http://core.result.service.soap.CDRator.com/xsd" xmlns="http://core.data.soap.CDRator.com/xsd" xmlns:ns4="http://data.soap.CDRator.com/xsd" xmlns:ns3="http://payment.result.service.soap.CDRator.com/xsd">
         <ns5:contextUser>
            <ns4:brandKey>FNC</ns4:brandKey>
         </ns5:contextUser>
         <ns5:rechargeTicket>
            <id xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
            <billingGroupId>200907111603122893</billingGroupId>
            <code>TIME_DIRECTDEBIT</code>
            <dateCreated xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true" />
            <dayOfMonth>1</dayOfMonth>
            <nextRechargeDate>2015-06-01+02:00</nextRechargeDate>
            <rechargeAmount>20</rechargeAmount>
         </ns5:rechargeTicket>
      </ns5:updateRechargeTicket>
   </S:Body>
</S:Envelope>


<?xml version='1.0' encoding='utf-8'?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body><ns5:addDirectDebitPayment xmlns:ns5="http://service.soap.CDRator.com" xmlns:ns2="http://core.result.service.soap.CDRator.com/xsd" xmlns="http://core.data.soap.CDRator.com/xsd" xmlns:ns4="http://data.soap.CDRator.com/xsd" xmlns:ns3="http://payment.result.service.soap.CDRator.com/xsd"><ns5:contextUser><ns4:brandKey>FNC</ns4:brandKey></ns5:contextUser><ns5:billingGroupId>201008141448491784</ns5:billingGroupId><ns5:amount>10.0</ns5:amount></ns5:addDirectDebitPayment></S:Body></S:Envelope>

我只想使用 1 个 select 查询从这 2 个 xml 值中提取 BillingGroupId。是否有可能或者我想使用单独的 select 查询来提取这 2 个 xml 值的 BillingGroupId? 我想从此 xml 值中提取 billingGroupID 值,但它不返回任何内容。我在 select 查询中犯了一个我无法识别的小错误。

这是我的查询:

SELECT xt_billingGroupId.BILLING_GROUP_ID
FROM TEMP_SOAP_MONITORING_TOPUP sm
CROSS JOIN XMLTable(XMLNAMESPACES (
      'http://schemas.xmlsoap.org/soap/envelope/' AS "soapenv",
      'http://service.soap.CDRator.com' as "ns",
      'http://core.data.soap.CDRator.com/xsd' as "ax2130",
      'http://webshop.result.service.soap.CDRator.com/xsd' as "ax2147",
      'http://core.signup.data.soap.CDRator.com/xsd' as "ns3",
      'http://service.soap.CDRator.com' as "ns5",
      'http://core.result.service.soap.CDRator.com/xsd' as "ax232"
    ),
    'for $i in //ns5:billingGroupId return $i'
    passing XMLType(sm.REQUEST_XML)
    columns "BILLING_GROUP_ID" VARCHAR2(100) path '/') xt_billingGroupId    
CROSS JOIN XMLTable(XMLNAMESPACES (
      'http://core.result.service.soap.CDRator.com/xsd' as "ax232"
    )

您正在尝试访问没有名称空间前缀的节点,因此在默认名称空间中,但在具有特定名称空间前缀的节点中,ns5。你可以用 local-name():

SELECT xt_billingGroupId.BILLING_GROUP_ID
FROM TEMP_SOAP_MONITORING_TOPUP sm
CROSS JOIN XMLTable(XMLNAMESPACES (
      'http://schemas.xmlsoap.org/soap/envelope/' AS "S",
      'http://service.soap.CDRator.com' as "ns",
      'http://core.data.soap.CDRator.com/xsd' as "ax2130",
      'http://webshop.result.service.soap.CDRator.com/xsd' as "ax2147",
      'http://core.signup.data.soap.CDRator.com/xsd' as "ns3",
      'http://service.soap.CDRator.com' as "ns5",
      'http://core.result.service.soap.CDRator.com/xsd' as "ax232"
    ),
    '//ns5:rechargeTicket/*[local-name()="billingGroupId"]'
    passing XMLType(sm.REQUEST_XML)
    columns "BILLING_GROUP_ID" VARCHAR2(100) path '/') xt_billingGroupId;

BILLING_GROUP_ID                                                               
--------------------------------------------------------------------------------
200907111603122893                                                              

或者通过通配符命名空间:

...
'//ns5:rechargeTicket/*:billingGroupId'
passing XMLType(sm.REQUEST_XML)
columns "BILLING_GROUP_ID" VARCHAR2(100) path '/') xt_billingGroupId    

如果你想让它更灵活,你可以只使用通配符而不引用包含节点;然后您也不需要 XMLNameSpaces 定义。使用您的两个示例 XML:

SELECT xt_billingGroupId.BILLING_GROUP_ID
FROM TEMP_SOAP_MONITORING_TOPUP sm
CROSS JOIN XMLTable('//*:billingGroupId'
    passing XMLType(sm.REQUEST_XML)
    columns "BILLING_GROUP_ID" VARCHAR2(100) path '/') xt_billingGroupId    

BILLING_GROUP_ID                                                               
--------------------------------------------------------------------------------
200907111603122893                                                              
201008141448491784                                                              

还有一种变体

select xt_billingGroupId.* from x,
XMLTable(XMLNAMESPACES (
      'http://schemas.xmlsoap.org/soap/envelope/' AS "soapenv",
      'http://service.soap.CDRator.com' as "ns",
      'http://core.data.soap.CDRator.com/xsd' as "ax2130",
      'http://webshop.result.service.soap.CDRator.com/xsd' as "ax2147",
      'http://core.signup.data.soap.CDRator.com/xsd' as "ns3",
      'http://service.soap.CDRator.com' as "ns5",
      'http://core.result.service.soap.CDRator.com/xsd' as "ax232"
    ),
    '//ns5:rechargeTicket'
    passing x
    columns "BILLING_GROUP_ID" VARCHAR2(100) path 'ax2130:billingGroupId',
    lineitem  XMLType  PATH '/') xt_billingGroupId ;