简体   繁体   English

无法从Oracle SQL中的xml值提取数据

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

I have the following 2 xml value which is similar and which is stored in the request_xml column and which is clob data type: 我有以下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>

I want to use only 1 select query to extract the BillingGroupId from this 2 xml values. 我只想使用1个选择查询从这2个xml值中提取BillingGroupId。 Is it possible or do i want to use separate select query to extarct BillingGroupId for this 2 xml values? 是否可能或者我想使用单独的选择查询来为这2个xml值选择BillingGroupId? I want to extract the billingGroupID value from this xml value but its not returning anything. 我想从此xml值中提取billingGroupID值,但不返回任何内容。 I am doing a small mistake in my select query which i am not able to identify. 我在无法识别的选择查询中犯了一个小错误。

Here is my query : 这是我的查询:

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"
    )

Also one variant 也是一种

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 ;

You're trying to access a node with no namespace prefix, so in the default namespace, but within a node that has a specific namespace prefix, ns5 . 您尝试访问没有名称空间前缀的节点,因此要访问默认名称空间,但要访问具有特定名称空间前缀ns5 You could do that with local-name() : 您可以使用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                                                              

Or by wild-carding the namespace: 或通过通配符命名空间:

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

If you want it to be flexible you could just use the wild-card without referring to the containing node at all; 如果您希望它具有灵活性,则可以只使用通配符,而无需完全引用包含节点。 and then you don't need the XMLNameSpaces definition either. 然后您也不需要XMLNameSpaces定义。 With your two sample XMLs: 使用您的两个示例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                                                              

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM