Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Generate XML from SQL


Generate XML from SQL

Author
Message
system243trd
system243trd
Grasshopper
Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)

Group: General Forum Members
Points: 22 Visits: 117
I am fairly new to XML and have been doing some research.

I need to be able to create the following XML output from a couple of SQL tables

Table 1
Fielda (primary key)
Fieldb
Fieldc

Table
FieldD (foreign key)
fieldE

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0">
<xs:element name="examplea " >
<xs:element name="schemaVersion" type="xs:decimal" use="required" fixed="1.0" />
<xs:complexType >
<xs:sequence>
<xs:element name="confirmationDetail" minoccurs="1" maxoccurs="3000">
<xs:complexType>
<xs:sequence >
<xs:element name="fielda" type="string" maxlength="50"/>
<xs:element name="fieldb" type="string" maxlength="50"/>
<xs:element name="fieldc" type="date"/>
<xs:element name="fieldd" type="integer" maxlength="10"/>
<xs:element name="fielde" type="string" maxlength="20"/>
<xs:restriction>
<xs:enumeration value="A"/>
<xs:enumeration value="B"/>
<xs:enumeration value="C"/>
</xs:restriction>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

Is it possible to create this output in SQL Server?
SwePeso
SwePeso
SSCrazy
SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)

Group: General Forum Members
Points: 2469 Visits: 3431
SELECT Field1 AS [@AttributeNameHere]


N 56°04'39.16"
E 12°55'05.25"
system243trd
system243trd
Grasshopper
Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)

Group: General Forum Members
Points: 22 Visits: 117
SELECT Field1 AS [@AttributeNameHere]

Can you elaborate on this?
SwePeso
SwePeso
SSCrazy
SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)

Group: General Forum Members
Points: 2469 Visits: 3431
See your post here http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=187048
and my answer.


N 56°04'39.16"
E 12°55'05.25"
system243trd
system243trd
Grasshopper
Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)

Group: General Forum Members
Points: 22 Visits: 117
I've seen your answer but can you explain your answer please?
SwePeso
SwePeso
SSCrazy
SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)

Group: General Forum Members
Points: 2469 Visits: 3431
If you prefix your column name with "@", the value becomes an attribute instead of an element.


N 56°04'39.16"
E 12°55'05.25"
system243trd
system243trd
Grasshopper
Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)

Group: General Forum Members
Points: 22 Visits: 117
Thanks SwePeso - please could you give an example as it doesn't seem to be working for me.
SwePeso
SwePeso
SSCrazy
SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)

Group: General Forum Members
Points: 2469 Visits: 3431
DECLARE   @Sample TABLE
   (
      Col1 VARCHAR(20) NOT NULL,
      Col2 INT NOT NULL
   Wink;

INSERT   @Sample
   (
      Col1,
      Col2
   Wink
VALUES   ('SwePeso', 1),
   ('Demo', 2);

SELECT   Col2 AS [Element/@Age],
   Col1 AS [Element]
FROM   @Sample
FOR XML   PATH(''),
   ROOT('Data');




N 56°04'39.16"
E 12°55'05.25"
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search