Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
|
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 24th, 2004, 02:09 AM
Authorized User
 
Join Date: Sep 2004
Posts: 67
Thanks: 1
Thanked 0 Times in 0 Posts
Default XML from SQL Server data

Hi There

I'm wondering how it would be possible to create an XML schema that formats XML results returned from a query to a SQL Server in a customized format? Eg. The results would be returned like this:

<People>
 <Name>Bart Simpson</Name>
 <Age>10</Age>
 <City>Springfield</City>
</People>

Rather than the way SQL Server generates it's own XML which is like:

<People name="Bart Simpson" Age="10" City="Springfield"></People>


 
Old September 24th, 2004, 03:34 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Firstly XML Schema do not format XML, XSLT does that.

Secondly you could use an XSLT to change it from an attribute style to an element style.

Thirdly you could also change the SQL query to return the data as you wish, SQL server can do virtually any format you need. In this instance it maybe as simple as changing the end of the query to read FOR XML AUTO, ELEMENTS.

Which would suit you better a transformation of the results or a modification to the underlying query?

--

Joe (Co-author Beginning XML, 3rd edition)
 
Old September 24th, 2004, 08:54 PM
Authorized User
 
Join Date: Sep 2004
Posts: 67
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Thanks for that, it worked perfectly!

Although can I be a pain and ask if I am able to rename the tags?
Currently, the tags are named after the SQL Database columns and I want to be able to give them different names. Is this possible?

 
Old September 25th, 2004, 03:02 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

You have two main choices, the easier option is to alter you SQL query, if you want Name to be FullName:
Select Name as FullName, ...
FROM People FOR XML AUTO, ELEMENTS.
If you choose illegal element names, e.g. with spaces, the SQL Server will use a hex encoding for those, it looks very ugly.

The more difficult option is to use FOR XML EXPLICIT



--

Joe (Co-author Beginning XML, 3rd edition)
 
Old September 25th, 2004, 04:49 AM
Authorized User
 
Join Date: Sep 2004
Posts: 67
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Thanks for that!

After browsing your link to MSDN plus doing a 'Google' on FOR XML EXPLICIT I think that this will offer the best flexibility for what I am trying to achieve.

I just have to try and get my head around it first, your not wrong when you say "difficult option" :)

 
Old September 25th, 2004, 05:18 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Good luck. The main thing to remember when writing the query is that the internal code that produces the xml uses a forward only cursor. This means that you must be careful to order yur results in a way that reflects the nesting.



--

Joe (Co-author Beginning XML, 3rd edition)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Relational SQL Data to XML - Vet SQL/ASP - New XML JimiTheJett XML 1 December 4th, 2008 06:06 PM
SQL Server 2005 XML: FOR XML PATH -> cdata? stoves SQL Server 2005 1 July 8th, 2008 02:40 AM
retrieve data from SQL server wih XML Thebravehearth XML 3 October 7th, 2004 02:07 AM
inser xml data to sql server from asp ak Classic ASP Databases 7 February 25th, 2004 01:05 PM
inserting some data from xml file to sql server ak Classic ASP XML 1 February 25th, 2004 10:29 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.