0

I want table output as XML. My table result is

enter image description here

I need result like

enter image description here

Query

Declare @colorTable table (Category varchar(100),Attribute varchar(100))
insert into @colorTable values ('Color','Red')
insert into @colorTable values ('Color','Blue')
insert into @colorTable values ('Color','Green')
insert into @colorTable values ('Transport','Bus')
insert into @colorTable values ('Transport','Car')
insert into @colorTable values ('Transport','Twoweeler')

select * from @colorTable
FOR XML PAth(''), ROOT ('xml'), ELEMENTS; 

Thanks,

S.Sundar

4
  • Have you tried anything so far? For example, using select ... for xml. Commented Sep 2, 2015 at 14:56
  • Yes I tried. not getting result like what i expected Commented Sep 2, 2015 at 15:03
  • 1
    If you will show some of your attempts then someone could point to the errors you've made. Otherwise it looks like "please write code for me" request. Commented Sep 2, 2015 at 15:04
  • I tried like this Declare @colorTable table (Category varchar(100),Attribute varchar(100)) insert into @colorTable values ('Color','Red') insert into @colorTable values ('Color','Blue') insert into @colorTable values ('Color','Green') insert into @colorTable values ('Transport','Bus') insert into @colorTable values ('Transport','Car') insert into @colorTable values ('Transport','Twoweeler') select * from @colorTable FOR XML PAth(''), ROOT ('xml'), ELEMENTS; Commented Sep 2, 2015 at 15:31

4 Answers 4

1

we have to write a query using GROUP BY, XML path not allowed to write sql within xml path(not allowed select statement)

Declare @colorTable table (Category varchar(100),Attribute varchar(100))
insert into @colorTable values ('Color','Red')
insert into @colorTable values ('Color','Blue')
insert into @colorTable values ('Color','Green')
insert into @colorTable values ('Transport','Bus')
insert into @colorTable values ('Transport','Car')
insert into @colorTable values ('Transport','Twoweeler')

select T1.Category as '@Value',
       (
       select T2.Attribute as '@Value'
       from @colorTable as T2
       where T2.Category = T1.Category
       group by T2.Attribute
       for xml path('Attribute'), type
       )
from @colorTable as T1
group by Category
for xml path('Category'), root('xml')

OUTPUT

<xml>
  <Category Value="Color">
    <Attribute Value="Blue" />
    <Attribute Value="Green" />
    <Attribute Value="Red" />
  </Category>
  <Category Value="Transport">
    <Attribute Value="Bus" />
    <Attribute Value="Car" />
    <Attribute Value="Twoweeler" />
  </Category>
</xml>
Sign up to request clarification or add additional context in comments.

Comments

0

Well, in your particular case to achieve your goal you should made three subselects for your three subbranches Color, Transport and Electronics and then join that xml parts under xml root.

You can do it using following query:

select
    (
        select
            Attribute as 'attribute/@value'
        from @colorTable
        where Category = 'Color'
        for xml path(''), type
    ) as Color,
    (
        select
            Attribute as 'attribute/@value'
        from @colorTable
        where Category = 'Transports'
        for xml path(''), type
    ) as Transports,
    (
        select
            Attribute as 'attribute/@value'
        from @colorTable
        where Category = 'Electronics'
        for xml path(''), type
    ) as Electronics
for xml path(''), root('xml')

2 Comments

color , transport will come dynamically. that is not static
@user2354274 Having dynamic element names in your xml looks like a bad idea. So if it should be dynamic, consider changing your xml structure from <Color>...</Color> to <Category name="Color">...</Category>.
0

Take a look at EXPLICIT Mode with FOR XML MSDN. It can be a pain , but you can control the output ;-)

Comments

0

Finally I put code like this

Select Category as value , (SELECT Attribute as value from @colorTable where Category= a.Category
FOR XML raw('attribute'), TYPE) from @colorTable as a group by Category FOR XML raw('category'), ROOT ('xml'), type;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.