2015-05-13 51 views
1

我有以下2 XML值,它是類似的並且被存儲在request_xml塔並且其CLOB數據類型:不能提取從XML值的數據在Oracle SQL

<?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個選擇查詢從這2個xml值中提取BillingGroupId。是否有可能或我想要使用單獨的選擇查詢來爲這2個xml值取出BillingGroupId? 我想從這個xml值中提取billingGroupID值,但它沒有返回任何東西。我在我無法識別的選擇查詢中犯了一個小錯誤。

這裏是我的查詢:

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

回答

2

你試圖訪問一個節點沒有命名空間前綴,所以在默認的命名空間,但有一個特定的名稱空間前綴,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                
+0

你好亞歷克斯其工作。我還有一個與此相關的問題,我可以編輯我的問題嗎? – Andrew

+0

@Rahul - 取決於它是否與答案有關,在這種情況下,您要求澄清作爲評論;或者是關於同一任務的無關問題,在這種情況下,您應該提出一個新問題。 –

+0

你好亞歷克斯我已經更新了我的問題一點點。我有2個版本的xml值有點相似。我想通過修改使用相同的查詢,並使其靈活地從這2個xml值中提取BillingGroupId。 – Andrew

3

另外一個變種

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 ; 
+0

好吧,我沒有注意到XML文本中的默認'xmlns'在XMLNameSpaces定義中被別名爲'ax2130'。可能有助於在你的答案中解釋。 –

+0

我認爲這是因爲name不是和ns一樣,所以它用作默認值。而且,我發現它在lineitem值,當解析XML文件:) –