Home All Groups Group Topic Archive Search About

How to output table as XML with elements on separate lines and indented?

Author
6 Apr 2007 11:54 PM
Laurence Neville
I would like to run a SQL command that outputs all the data in a table to
XML, for each column value to be an element, and for each element on a new
line and indented. For example:

   <customer>
    <CustomerID>ALFKI</CustomerID>
    <CompanyName>Alfreds Futterkiste</CompanyName>
    <ContactName>Maria Anders</ContactName>
    <ContactTitle>Sales Representative</ContactTitle>
    <Address>Obere Str. 57</Address>
    <City>Berlin</City>
    <PostalCode>12209</PostalCode>
    <Country>Germany</Country>
    <Phone>030-0074321</Phone>
    <Fax>030-0076545</Fax>
   </customer>

Using SELECT ...... FOR XML AUTO, ELEMENTS gets close but there are no new
lines or indenting. Is there any way to do this within SQL? If not, how can
I do it?

Author
7 Apr 2007 2:09 AM
Kent Tegels
Hello Laurence,

> I would like to run a SQL command that outputs all the data in a table
> to XML, for each column value to be an element, and for each element
> on a new line and indented. For example:

No and why would want to? Okay, I know, I know, because you want pretty printed
XML.

The point here is that to an XML parser, the formatting doesn't matter, it
gets discarded during processing. Since it doesn't matter to the processor,
SQL Server doesn't do it. When you see Managment format it, its just a pretty
print choice that tool made.

I know that's not what you wanted to hear, trust me. Been there, done that
and finally just moved on.

Thanks!
Kent Tegels
DevelopMentor
http://staff.develop.com/ktegels/
Author
7 Apr 2007 10:14 PM
Laurence Neville
Oh well. The reason I want the XML to be readable is this (and if anyone has
a better idea for this then let me know):

I want to version control the static/meta data in various tables in my
application's database. I want as simple a method as possible to populate a
table from whatever files are used to store the data in Source Safe, and
conversely as simple a method as possible for dumping the data from a table
into files. Most importantly I also want to be able to use the Diff function
in Source Safe and easily see the differences between versions of the data.

I looked at 3 ideas - BCPing the data into delimited files (batch files to
load and unload the data), scripting the data as a series of INSERT
commands, and scripting the data to XML files. Formatted XML would have been
the most readable format for the data, and possibly scripts to load and dump
the data would be the simplest.

So, any ideas about this?

Show quote
"Kent Tegels" <kteg***@develop.com> wrote in message
news:18f2bcb1ad2a8c946964ca238c5@news.microsoft.com...
> Hello Laurence,
>
>> I would like to run a SQL command that outputs all the data in a table
>> to XML, for each column value to be an element, and for each element
>> on a new line and indented. For example:
>
> No and why would want to? Okay, I know, I know, because you want pretty
> printed XML.
> The point here is that to an XML parser, the formatting doesn't matter, it
> gets discarded during processing. Since it doesn't matter to the
> processor, SQL Server doesn't do it. When you see Managment format it, its
> just a pretty print choice that tool made.
>
> I know that's not what you wanted to hear, trust me. Been there, done that
> and finally just moved on.
> Thanks!
> Kent Tegels
> DevelopMentor
> http://staff.develop.com/ktegels/
>
>
Author
7 Apr 2007 10:34 PM
Tom Moreau
It should be relatively straightforward to build a little app that takes the
XML and does the indentation for you.  Basically, it can loop through and
when it finds a non-root element tag, it indents by one.  When it finds a
closing tag, it decrements by one.

--
   Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
SQL Server MVP
Toronto, ON   Canada
https://mvp.support.microsoft.com/profile/Tom.Moreau


"Laurence Neville" <laurencenevi***@hotmail.com> wrote in message
news:eUe9mKWeHHA.1824@TK2MSFTNGP06.phx.gbl...
Oh well. The reason I want the XML to be readable is this (and if anyone has
a better idea for this then let me know):

I want to version control the static/meta data in various tables in my
application's database. I want as simple a method as possible to populate a
table from whatever files are used to store the data in Source Safe, and
conversely as simple a method as possible for dumping the data from a table
into files. Most importantly I also want to be able to use the Diff function
in Source Safe and easily see the differences between versions of the data.

I looked at 3 ideas - BCPing the data into delimited files (batch files to
load and unload the data), scripting the data as a series of INSERT
commands, and scripting the data to XML files. Formatted XML would have been
the most readable format for the data, and possibly scripts to load and dump
the data would be the simplest.

So, any ideas about this?

Show quote
"Kent Tegels" <kteg***@develop.com> wrote in message
news:18f2bcb1ad2a8c946964ca238c5@news.microsoft.com...
> Hello Laurence,
>
>> I would like to run a SQL command that outputs all the data in a table
>> to XML, for each column value to be an element, and for each element
>> on a new line and indented. For example:
>
> No and why would want to? Okay, I know, I know, because you want pretty
> printed XML.
> The point here is that to an XML parser, the formatting doesn't matter, it
> gets discarded during processing. Since it doesn't matter to the
> processor, SQL Server doesn't do it. When you see Managment format it, its
> just a pretty print choice that tool made.
>
> I know that's not what you wanted to hear, trust me. Been there, done that
> and finally just moved on.
> Thanks!
> Kent Tegels
> DevelopMentor
> http://staff.develop.com/ktegels/
>
>
Author
8 Apr 2007 7:10 PM
Laurence Neville
This sounds useful ... an XSLT that does the formatting:

http://www.xml.com/lpt/a/1681

Now I just need to figure out how to apply the XSLT to the XML output of my
query, hopefully all within SQL ....


Show quote
"Tom Moreau" <tom@dont.spam.me.cips.ca> wrote in message
news:%23Xx$QTWeHHA.4172@TK2MSFTNGP05.phx.gbl...
> It should be relatively straightforward to build a little app that takes
> the
> XML and does the indentation for you.  Basically, it can loop through and
> when it finds a non-root element tag, it indents by one.  When it finds a
> closing tag, it decrements by one.
>
> --
>   Tom
>
> ----------------------------------------------------
> Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
> SQL Server MVP
> Toronto, ON   Canada
> https://mvp.support.microsoft.com/profile/Tom.Moreau
>
>
> "Laurence Neville" <laurencenevi***@hotmail.com> wrote in message
> news:eUe9mKWeHHA.1824@TK2MSFTNGP06.phx.gbl...
> Oh well. The reason I want the XML to be readable is this (and if anyone
> has
> a better idea for this then let me know):
>
> I want to version control the static/meta data in various tables in my
> application's database. I want as simple a method as possible to populate
> a
> table from whatever files are used to store the data in Source Safe, and
> conversely as simple a method as possible for dumping the data from a
> table
> into files. Most importantly I also want to be able to use the Diff
> function
> in Source Safe and easily see the differences between versions of the
> data.
>
> I looked at 3 ideas - BCPing the data into delimited files (batch files to
> load and unload the data), scripting the data as a series of INSERT
> commands, and scripting the data to XML files. Formatted XML would have
> been
> the most readable format for the data, and possibly scripts to load and
> dump
> the data would be the simplest.
>
> So, any ideas about this?
>
> "Kent Tegels" <kteg***@develop.com> wrote in message
> news:18f2bcb1ad2a8c946964ca238c5@news.microsoft.com...
>> Hello Laurence,
>>
>>> I would like to run a SQL command that outputs all the data in a table
>>> to XML, for each column value to be an element, and for each element
>>> on a new line and indented. For example:
>>
>> No and why would want to? Okay, I know, I know, because you want pretty
>> printed XML.
>> The point here is that to an XML parser, the formatting doesn't matter,
>> it
>> gets discarded during processing. Since it doesn't matter to the
>> processor, SQL Server doesn't do it. When you see Managment format it,
>> its
>> just a pretty print choice that tool made.
>>
>> I know that's not what you wanted to hear, trust me. Been there, done
>> that
>> and finally just moved on.
>> Thanks!
>> Kent Tegels
>> DevelopMentor
>> http://staff.develop.com/ktegels/
>>
>>
>
>
Author
9 Apr 2007 3:31 AM
Mike C#
http://blogs.msdn.com/mrorke/archive/2005/06/28/433471.aspx

Show quote
"Laurence Neville" <laurencenevi***@hotmail.com> wrote in message
news:usbIhIheHHA.284@TK2MSFTNGP05.phx.gbl...
> This sounds useful ... an XSLT that does the formatting:
>
> http://www.xml.com/lpt/a/1681
>
> Now I just need to figure out how to apply the XSLT to the XML output of
> my query, hopefully all within SQL ....
>
Author
9 Apr 2007 9:33 AM
Laurence Neville
Thanks Mike. Ideally I'd like something that works on both SQL 2000 and
2005, but after researching it looks like the only way to apply an XSLT to
XML is through code, e.g. in an ASP page, VB app etc.

Show quote
"Mike C#" <x**@xyz.com> wrote in message
news:%23D1E7eleHHA.4564@TK2MSFTNGP03.phx.gbl...
> http://blogs.msdn.com/mrorke/archive/2005/06/28/433471.aspx
>
> "Laurence Neville" <laurencenevi***@hotmail.com> wrote in message
> news:usbIhIheHHA.284@TK2MSFTNGP05.phx.gbl...
>> This sounds useful ... an XSLT that does the formatting:
>>
>> http://www.xml.com/lpt/a/1681
>>
>> Now I just need to figure out how to apply the XSLT to the XML output of
>> my query, hopefully all within SQL ....
>>
>
>
Author
10 Apr 2007 1:44 AM
Mike C#
"Laurence Neville" <laurencenevi***@hotmail.com> wrote in message
news:usglD9oeHHA.3960@TK2MSFTNGP02.phx.gbl...
> Thanks Mike. Ideally I'd like something that works on both SQL 2000 and
> 2005, but after researching it looks like the only way to apply an XSLT to
> XML is through code, e.g. in an ASP page, VB app etc.

Anything you could cook up in SQL 2000 would not be worth the effort of
transporting over to SQL 2005, especially since SQL 2005 has all this great
new XML and SQLCLR functionality built right in.  The path of least
resistance for "either/or" would be via a middle tier webservice, web page,
or other application.  A SQL 2000-only server-side solution would probably
involve a lot of jumping through COM hoops and/or customer extended
procedures.
Author
22 Apr 2007 2:24 PM
AndrewC
Mike C# wrote:
Show quote
> "Laurence Neville" <laurencenevi***@hotmail.com> wrote in message
> news:usglD9oeHHA.3960@TK2MSFTNGP02.phx.gbl...
>> Thanks Mike. Ideally I'd like something that works on both SQL 2000 and
>> 2005, but after researching it looks like the only way to apply an XSLT to
>> XML is through code, e.g. in an ASP page, VB app etc.
>
> Anything you could cook up in SQL 2000 would not be worth the effort of
> transporting over to SQL 2005, especially since SQL 2005 has all this great
> new XML and SQLCLR functionality built right in.  The path of least
> resistance for "either/or" would be via a middle tier webservice, web page,
> or other application.  A SQL 2000-only server-side solution would probably
> involve a lot of jumping through COM hoops and/or customer extended
> procedures.
>
>
Why not try HTMLTidy? (yes, it tidies XML too, of course) There is an
example on its use within SQL Server here on Simple-Talk which works a
treat. Fast too!
http://www.simple-talk.com/sql/t-sql-programming/reading-and-writing-files-in-sql-server-using-t-sql/
Author
21 Apr 2007 3:56 PM
Joe Fawcett
I understand what you want but formatting the XML isn't really going to
help. A file diff and an XML diff are two very different things. There are
tools that carry out XML diffs, these are based on the XML infoset though,
not on what, in XML, could be just cosmetic changes, such as whether
attributes have double or single quotes, not on what a file diff tool would
show up.

--

Joe Fawcett (MVP - XML)
http://joe.fawcett.name

Show quote
"Laurence Neville" <laurencenevi***@hotmail.com> wrote in message
news:eUe9mKWeHHA.1824@TK2MSFTNGP06.phx.gbl...
> Oh well. The reason I want the XML to be readable is this (and if anyone
> has a better idea for this then let me know):
>
> I want to version control the static/meta data in various tables in my
> application's database. I want as simple a method as possible to populate
> a table from whatever files are used to store the data in Source Safe, and
> conversely as simple a method as possible for dumping the data from a
> table into files. Most importantly I also want to be able to use the Diff
> function in Source Safe and easily see the differences between versions of
> the data.
>
> I looked at 3 ideas - BCPing the data into delimited files (batch files to
> load and unload the data), scripting the data as a series of INSERT
> commands, and scripting the data to XML files. Formatted XML would have
> been the most readable format for the data, and possibly scripts to load
> and dump the data would be the simplest.
>
> So, any ideas about this?
>
> "Kent Tegels" <kteg***@develop.com> wrote in message
> news:18f2bcb1ad2a8c946964ca238c5@news.microsoft.com...
>> Hello Laurence,
>>
>>> I would like to run a SQL command that outputs all the data in a table
>>> to XML, for each column value to be an element, and for each element
>>> on a new line and indented. For example:
>>
>> No and why would want to? Okay, I know, I know, because you want pretty
>> printed XML.
>> The point here is that to an XML parser, the formatting doesn't matter,
>> it gets discarded during processing. Since it doesn't matter to the
>> processor, SQL Server doesn't do it. When you see Managment format it,
>> its just a pretty print choice that tool made.
>>
>> I know that's not what you wanted to hear, trust me. Been there, done
>> that and finally just moved on.
>> Thanks!
>> Kent Tegels
>> DevelopMentor
>> http://staff.develop.com/ktegels/
>>
>>
>
>

AddThis Social Bookmark Button