Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 FOR XML EXPLICIT

Author  Topic 

daz_oldham
Starting Member

5 Posts

Posted - 2005-08-10 : 12:26:42
Hi Guys

I've been reading through the posts on here and have not found the answer so here goes.

I've got a view that returns product attributes, so for example:

id, attributeName, attributeValue, productID
1, test, 1, 200
2, test2, house, 200
3, test3, giraffe, 200

What I would like to do, is return to me xml where the element names come from the attribute names, and the values the attribute values.

e.g.
<test>1</test>
<test2>house</test2>
<test3>giraffe</test3>

Is this possible? And also, how would I enclose this in a parent tag

e.g.
<attributes>
....
</attributes>

(I don't understand the posts on here explaining how to do that!)

If anyone has a noddy guide to take me through XML EXPLICIT, I would be forever in your debt!

Best regards and many thanks in advance.

Daz


-----------------
Daz

nathans
Aged Yak Warrior

938 Posts

Posted - 2005-08-10 : 12:56:06
There are many references out there on SQL XML. Here is a great sample chapter from MS on this very subject.

Give it a shot and dont hesitate to post back here if you get stuck along the way. Those EXPLICIT queries can be tricky, but it looks like your XML schema is fairly simple, so shouldnt be too painful

[url]http://www.microsoft.com/mspress/books/sampchap/5178d.asp#116[/url]

You might have to pivot that data out of the name/value table into columns to use in the way you described. Is there a set number of attributeName?

Nathan Skerl
Go to Top of Page

nathans
Aged Yak Warrior

938 Posts

Posted - 2005-08-10 : 19:00:28
What I came up with...

set nocount on

declare @yourTable table ([id] int, attributeName varchar(10), attributeValue varchar(10), productID int)
insert into @yourTable
select 1, 'test', '1', 200 union
select 2, 'test2', 'house', 200 union
select 3, 'test3', 'giraffe', 200 union
select 4, 'test', '2', 400 union
select 5, 'test2', 'apartment', 400 union
select 6, 'test3', 'tiger', 400

-- dbcc traceon(257)

SELECT Tag = 1, Parent = NULL,
[attributes!1!productID] = d.productID,
[attributes!1!test!element] = d.test,
[attributes!1!test2!element] = d.test2,
[attributes!1!test3!element] = d.test3
FROM ( select productId,
max(case attributeName when 'test' then attributeValue end) as 'test',
max(case attributeName when 'test2' then attributeValue end) as 'test2',
max(case attributeName when 'test3' then attributeValue end) as 'test3'
from @yourTable
group by productID) d
FOR XML EXPLICIT


Nathan Skerl
Go to Top of Page

daz_oldham
Starting Member

5 Posts

Posted - 2005-08-11 : 04:18:05
Hi Nathan - thanks for the help - I have to go into a meeting but will have a look through this later.

There could be any number of attributes for the products - they think them up all the time basically! I will have a look through and see where I get, but as a last resort, the structure of the XML will have to change.

I am currently building the XML in a DTS package, but the problem with this is that looping etc etc is taking up too much resource as I also call a VB.COM object. I firmly believe that this is the way to get it done.

-----------------
Daz
Go to Top of Page

daz_oldham
Starting Member

5 Posts

Posted - 2005-08-11 : 10:59:38
I have tried and it has given me a headache - I think I am going to go for changing the structure! Lol

A nice simple statement that returns the xml like so:

<ProductAttribute AttributeStructureID="51">
<AttributeName>How Many Players</AttributeName>
<AttributeDetail>1 Player</AttributeDetail>
</ProductAttribute>

I just need to find out how to encase this in a parent element and then i am getting somewhere :)

-----------------
Daz
Go to Top of Page
   

- Advertisement -