Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Xquery striping XML having problems separating records


Xquery striping XML having problems separating records

Author
Message
ShineBoy
ShineBoy
Valued Member
Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)

Group: General Forum Members
Points: 68 Visits: 154
Got a problem stripping XML, really just because I am lost on Xquery:
Heres what I am doing:

Declare @Xml XML
Set @Xml = '<Response xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Id>8991f860-c787-41bc-93d6-09f496f75169</Id>
<Status>OK</Status>
<ProviderName>API Previewer</ProviderName>
<DateTimeUTC>2012-11-28T09:14:12.2077238Z</DateTimeUTC>
<Invoices>
<Invoice>
<Contact>
<ContactID>cf36ef98</ContactID>
<ContactStatus>ACTIVE</ContactStatus>
<Name>Skyops Ltd</Name>
</Contact>
<Date>2012-11-15T00:00:00</Date>
<DueDate>2012-12-20T00:00:00</DueDate>
<Status>AUTHORISED</Status>
<LineAmountTypes>Exclusive</LineAmountTypes>
<LineItems>
<LineItem>
<Description>For Full ColourAdvertisment in 2012</Description>
</LineItem>
</LineItems>
<SubTotal>819.00</SubTotal>
<TotalTax>122.85</TotalTax>
<Total>941.85</Total>
<UpdatedDateUTC>2012-11-28T09:14:11.003</UpdatedDateUTC>
<CurrencyCode>NZD</CurrencyCode>
</Invoice>
<Invoice>
<Contact>
<ContactID>zzzzz98</ContactID>
<ContactStatus>ACTIVE</ContactStatus>
<Name>Spiedonme Ltd</Name>
</Contact>
</Invoice>
</Invoices>
</Response>'


SELECT @xml.query('/Response/Status').value('.','varchar(100)')

-- results from this one good, all I want to do is ensure that I get an 'OK'

SELECT @xml.query('//ContactID[1]').value('.','varchar(40)') as ContactId,
@xml.query('//Name[1]').value('.','varchar(200)') as CompanyName
FROM @xml.nodes('//ContactID') AS x(y)

--This is where I just don't get it.
My results are:

cf36ef98zzzzz98   Skyops LtdSpiedonme Ltd
cf36ef98zzzzz98   Skyops LtdSpiedonme Ltd

--Basically bunched to gether.
--I want something more like:
cf36ef98 Skyops Ltd
zzzzz98   Spiedonme Ltd


what am I doing wrong?
Ideally I want to put the results of the second query into a table so I can do more with it, But I reckon I can figure out how to do that , once I get the results in the right format.
Mark Cowne
Mark Cowne
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2090 Visits: 22778
Try this

SELECT x.r.value('(Contact/ContactID/text())[1]','VARCHAR(40)') AS ContactID,
x.r.value('(Contact/Name/text())[1]','VARCHAR(200)') AS Name
FROM @Xml.nodes('/Response/Invoices/Invoice') AS x(r)



____________________________________________________

Deja View - The strange feeling that somewhere, sometime you've optimised this query before

How to get the best help on a forum

http://www.sqlservercentral.com/articles/Best+Practices/61537




ShineBoy
ShineBoy
Valued Member
Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)

Group: General Forum Members
Points: 68 Visits: 154
Thanks heaps, that works well.
Haven't done much previously with XML data in T-SQL, but once you get the hang of it, it fast and easy enough, just mind numbing when you write For xml explicit queries.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search