Solved

Problem with For XML Explicit formatting

Posted on 2008-06-11
5
168 Views
Last Modified: 2010-03-19
I am having a problem formating the XML structure for my SQL query. I am using FOR XML EXplicit clause and  Auto is not an options.

I have provided an excerpt of my query with its results. There is no linkage between the Author and book data.

Author.author_name as [Authors!1!Author_name]
Author.Author_id as [Authors!1!Author_id]
Book.name as [Books!2!name!element]
Book.author_id as [Books!2!author_id!element]



  <Authors Author_name="Bell" Author_ID="150"/>
  <Authors Author_name="James" Author_ID="151"/>
  <Authors Author_name="Henry" Author_ID="152"/>
  <Books>
     <Name>Bell_book1</Name>
     <Author_ID>150</Author_ID>
   </Books>
   <Books>
      <name>James_book2</Name>
      <Author_ID>151</Author_ID>      
    </Books>      
  <Books>
     <Name>Bell_book1</Name>
     <Author_ID>150</Author_ID>
   </Books>
   <Books>
      <name>Henry_book2</Name>
      <Author_ID>152</Author_ID>
   </Books>      




Ideally I want it to looking like my XML example below.  Does anyone have any suggestion for FOR XML syntax modifications or can this be done at all.


<Authors Author_name="Bell" Author_ID="150"/>
    <Books>
      <Name>Bell_book1</Name>
    </Books>
    <Books>
      <name>Bell_book2</name>
    </Books>


<Authors Author_name="James" Author_ID="151"/>
    <Books>
      <Name>James_book1</Name>
    </Books>
    <Books>
      <name>James_book2</Name>
    </Books>
0
Comment
Question by:drumph
  • 3
  • 2
5 Comments
 
LVL 17

Expert Comment

by:xDJR1875
ID: 21762243
can you provide your full query for us?
0
 

Author Comment

by:drumph
ID: 21762358
please see query below

select
1 as tag,
null as parent,
Author.author_name as [Authors!1!Author_name],
Author.Author_id as [Authors!1!Author_id],
Book.name as [Books!2!name!element],
Book.author_id as [Books!2!author_id!element],

from authors
join books on authors.author_id = books.authors_id

Union

select
2 as tag,
1 as parent,
Author.author_name,
Author.Author_id,
Book.name,
Book.author_id,

from authors
join books on authors.author_id = books.authors_id

For xml explicit , root ('my_books')

0
 
LVL 17

Expert Comment

by:xDJR1875
ID: 21762918
The following query returns closer to what you want and eliminates your union.

select
1 as tag,
null as parent,
Author.au_lname as [Authors!1!Author_name],
Author.Au_id as [Authors!1!Author_id],
Book.name as [Authors!1!Book!element]--,
--Book.author_id as [Authors!1!author_id!element]

from authors author
join books book on author.au_id = book.author_id

For xml explicit

One of the things we have done in the past is pull the related data bits into a temp table that is laid out more closely to what you need the results to be. And then pull the info from the temp table.
0
 

Author Comment

by:drumph
ID: 21763149
This looks a little better..   still repeating values.. and no nesting.
But what is I want nested Nodes?Author will all related books under also without repeating the Author name.  

Author -> Bil
               <book1
               <book2
0
 

Accepted Solution

by:
drumph earned 0 total points
ID: 21771154
This is the solution I came up with:

declare @authors table
( Author_ID integer,
  Author_name varchar(15))
insert into @authors
select 150, 'Bell' union all
select 151, 'James' union all
select 152, 'Henry'
--select * from @authors

 

declare @books table
( Author_ID integer,
  Name varchar(15)
)
insert into @books
select 150, 'Bell_book1' union all
select 150, 'Bell_book2' union all
select 151, 'James_book1' union all
select 151, 'James_book2' union all
select 152, 'Henry_book1'
--select * from @books

 
select
a.Author_name as [Author_name],
a.Author_id as [Author_ID],
( select
b.Name as [Books/Name]
from @books b
where a.author_id = b.author_id
for xml path(''), type
)
from @authors a
for xml auto, root ('Library'), elements
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
Hi, I am very much excited today since I'm going to share something very exciting Tool used for Analytical Reporting and that's nothing but MICROSTRATEGY. Actually there are lot of other tools available in the market for Reporting Such as Co…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now