2015-04-12 96 views
2

哦,今天是什麼。我試圖解析SQL Server中的某些XML,並且之前已經完成了一些非常龐大的文件,但對於這些文件,我遇到了最大的問題。我實際上是通過XML做了遞歸解析,這樣我就可以正確識別所有節點,所以我知道我並不瘋狂。SQL Server 2008/2012不解析XML Soap

我的代碼是這樣的。

DECLARE @XML XMl 

SELECT @XML = xmldata from testing where testingID = 3 

select T.c.value('Legs[1]', 'varchar(100)') as Legs 
/*t.c.value('(PoolId)[1]', 'varchar(100)') as [Pool], 
t.c.value('(./Model/@id)[1]', 'int') ModelID, 
t.c.value('(./Note)[1]', 'varchar(100)') Note, 
t.c.value('(./Part)[1]', 'varchar(15)') Part*/ 
from @xml.nodes('/Envelope/Body/GetEventDetailResponse/EventsResponse/EventDetail/Races/Race/Pools/Pool') as T(c) 

我留下了一些舊的代碼,我知道這些舊的XML文件的工作沒有問題,但評論說出來。

的XML是這個

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> 
<s:Body> 
    <GetEventDetailResponse xmlns="http://schema.unitedtote.com/ToteLink/2008/06/Program"> 
    <EventsResponse xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> 
     <RunId xmlns="http://schema.unitedtote.com/ToteLink/2008/06/Common">2613</RunId> 
     <GroupId xmlns="http://schema.unitedtote.com/ToteLink/2008/06/Common">TH1</GroupId> 
     <Source xmlns="http://schema.unitedtote.com/ToteLink/2008/06/Common"> 
      <SystemId>TSG</SystemId> 
      <SourceId>TH1TST</SourceId> 
     </Source> 
     <EventDetail> 
      <Races> 
       <Race> 
       <Pools> 
        <Pool> 
         <PoolId>WIN</PoolId> 
         <PoolNumber>1</PoolNumber> 
         <PoolName>Win</PoolName> 
         <PoolRaces>1</PoolRaces> 
         <Legs>1</Legs> 
         <Quick>true</Quick> 
         <Box>true</Box> 
         <SingleBetMinimum>2</SingleBetMinimum> 
         <MultipleBetMinimum>2</MultipleBetMinimum> 
         <WagerMinimum>2</WagerMinimum> 
         <Maximum>10000</Maximum> 
         <Probables>true</Probables> 
         <WBMaximum>10000</WBMaximum> 
         <Straight>true</Straight> 
        </Pool> 
        <Pool> 
         <PoolId>PLC</PoolId> 
         <PoolNumber>2</PoolNumber> 
         <PoolName>Place</PoolName> 
         <PoolRaces>1</PoolRaces> 
         <Legs>1</Legs> 
         <Quick>true</Quick> 
         <Box>true</Box> 
         <SingleBetMinimum>2</SingleBetMinimum> 
         <MultipleBetMinimum>2</MultipleBetMinimum> 
         <WagerMinimum>2</WagerMinimum> 
         <Maximum>10000</Maximum> 
         <WBMaximum>10000</WBMaximum> 
         <Straight>true</Straight> 
        </Pool> 
        <Pool> 
         <PoolId>EX</PoolId> 
         <PoolNumber>4</PoolNumber> 
         <PoolName>Exacta</PoolName> 
         <PoolRaces>1</PoolRaces> 
         <Legs>2</Legs> 
         <Quick>true</Quick> 
         <Wheel>true</Wheel> 
         <Box>true</Box> 
         <PowerBox>true</PowerBox> 
         <KeyWheel>true</KeyWheel> 
         <SingleBetMinimum>2</SingleBetMinimum> 
         <MultipleBetMinimum>1</MultipleBetMinimum> 
         <WagerMinimum>2</WagerMinimum> 
         <Maximum>10000</Maximum> 
         <WBMaximum>10000</WBMaximum> 
         <Straight>true</Straight> 
        </Pool> 
</Race> 
      </Races> 
      <EventId>XVD</EventId> 
      <RunId>2613</RunId> 
      <EventName>Vaal</EventName> 
      <TrackId>XV</TrackId> 
      <TrackName>Vaal</TrackName> 
      <EventTime>2015-04-02T16:30:00</EventTime> 
      <EventInfo>2 Apr 15</EventInfo> 
      <EventType>Current</EventType> 
      <TrackType>Thoroughbred</TrackType> 
      <CurrencyId>USD</CurrencyId> 
      <EventStatus>Open</EventStatus> 
      <RaceList>1-8</RaceList> 
      <BreakTo>0.1</BreakTo> 
     </EventDetail> 
    </EventsResponse> 
    </GetEventDetailResponse> 

這將是一個比很多在腳本級別寫出一個分析例程更有效。

回答

2

您必須指定命名空間,就像這樣:

;with xmlnamespaces(
    'http://schemas.xmlsoap.org/soap/envelope/' as s, 
    default 'http://schema.unitedtote.com/ToteLink/2008/06/Program' 
) 
select 
    T.c.value('Legs[1]', 'varchar(100)') as Legs 
from @xml.nodes('s:Envelope/s:Body/GetEventDetailResponse/EventsResponse/EventDetail/Races/Race/Pools/Pool') as T(c) 

sql fiddle demo