Tuesday, February 21, 2012

Root node attributes

Hello,

I've been trying to craft some xml that has attributes on the root, but without any success.

For example :-

SELECT
Test1 "Test1",
Test2 "Test2"
FROM
TestTable
FOR
XML PATH('TestValue'),
ELEMENTS,
ROOT('TestValues test=1') <-- this is not allowed, but this is the kind of thing that i would like

To produce :-

<TestValues test=1>
<TestValue>
<Test1>asdf </Test1>
<Test2>asdf </Test2>
</TestValue>
<TestValue>
<Test1>asdf </Test1>
<Test2>asdf </Test2>
</TestValue>
</TestValues>

Any ideas?

Thanks
Cedric

Don't you hate it when you click on the send button and at the same time an idea pop's into your head?!

Just thought i'd answer my own question incase someone is interested...

SELECT 1 '@.Test',
(
SELECT
Test1 "Test1",
Test2 "Test2"
FROM
TestTable
FOR
XML PATH('TestValue'),
TYPE
)
FOR
XML PATH ('TestValues')

Clearly there was too much blood in my coffee stream...

Thanks

Cedric

No comments:

Post a Comment