dbTalk Databases Forums  

Help with XQuery

microsoft.public.sqlserver.xml microsoft.public.sqlserver.xml


Discuss Help with XQuery in the microsoft.public.sqlserver.xml forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Ben
 
Posts: n/a

Default Help with XQuery - 04-20-2010 , 11:53 AM






Hi All,

I have an XML that has a layout as follows:
<books>
<book id='12' name='test1'>
<authors>
<author id='1' name = 'jill' />
<author id='2' name = 'jack' />
<author id='3' />
</authors>
</book>
</books>

I want to use XQuery to get data as follows:
book_id author_id author_name
12 1 jill
12 2 jack
12 3 null

How can I accomplish this?
Thanks in advance,
Ben



*** Sent via Developersdex http://www.developersdex.com ***

Reply With Quote
  #2  
Old   
Martin Honnen
 
Posts: n/a

Default Re: Help with XQuery - 04-20-2010 , 12:40 PM






Ben wrote:
Quote:
Hi All,

I have an XML that has a layout as follows:
books
<book id='12' name='test1'
<authors
<author id='1' name = 'jill' /
<author id='2' name = 'jack' /
author id='3' /
</authors
/book
/books

I want to use XQuery to get data as follows:
book_id author_id author_name
12 1 jill
12 2 jack
12 3 null

How can I accomplish this?
DECLARE @x xml;
SET @x = N'<books>
<book id="12" name="test1">
<authors>
<author id="1" name = "jill" />
<author id="2" name = "jack" />
<author id="3" />
</authors>
</book>
</books>';

SELECT
T.a.value('../../@id', 'int') as book_id,
T.a.value('@id', 'int') as author_id,
T.a.value('@name', 'nvarchar(20)') as author_name
FROM @x.nodes('books/book/authors/author') T(a);


--

Martin Honnen --- MVP Data Platform Development
http://msmvps.com/blogs/martin_honnen/

Reply With Quote
  #3  
Old   
Ben
 
Posts: n/a

Default Re: Help with XQuery - 04-20-2010 , 12:59 PM



That works !! Thanks much
Ben

*** Sent via Developersdex http://www.developersdex.com ***

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.