2016-09-25 63 views
1

我想在SQL Server 2008中運行一些查詢,並且沒有得到結果。我做了一些在線調查,並提出了以下查詢。我玩過,並能夠返回根節點,但我需要內部的值,並有一個呼叫節點數組,我需要它的值。節點陣列上的SQL Server 2008 xpath

WITH XMLNAMESPACES ('http://api.myapi.com/resource' as r) 
select c.value('@AgentCall','varchar(max)') as value 
from mytable cl 
outer apply cl.callinfoxml.nodes('//Call') as q(c) 

示例XML:

<r:ResourceList xmlns:r="http://api.myapi.com/resource" xmlns="http://api.myapi.com/data" totalResults="1"> 
    <Call id="1123570170003"> 
     <FromNumber>14062618272</FromNumber> 
     <ToNumber>14062618272</ToNumber> 
     <State>READY</State> 
     <BatchId>12827094003</BatchId> 
     <BroadcastId>14633834003</BroadcastId> 
     <ContactId>818582749003</ContactId> 
     <Inbound>false</Inbound> 
     <Created>2016-09-22T06:22:18Z</Created> 
     <Modified>2016-09-22T06:22:18Z</Modified> 
     <AgentCall>false</AgentCall> 
    </Call> 
</r:ResourceList> 

回答

1

你不注意你的文檔中的默認XML命名空間 - 你需要引用爲好。試試這個代碼:

WITH XMLNAMESPACES (DEFAULT 'http://api.myapi.com/data', 
          'http://api.myapi.com/resource' as r) 
SELECT 
    c.value('@AgentCall', 'varchar(max)') AS value 
FROM 
    mytable cl 
OUTER APPLY 
    cl.callinfoxml.nodes('/r:ResourceList/Call') as q(c) 
+0

謝謝。這與將選擇值更改爲'c.value('(AgentCall)[1]','varchar(max)')AS值' – Silas