Home All Groups Group Topic Archive Search About

changing the results header with for xml

Author
5 May 2009 10:51 AM
Tristan

Hi,

I'm getting results out of sql server 2005 as xml in the desired format
except the results header which is put before the xml. Any ideas how to
remove or modify the results header when returning data for xml? For example,
with the following query...

select top 1 *
from OrderHeader
WHERE limitprice is null
for xml auto, elements xsinil, ROOT('myorders')

....the xml returned (below) is headed with this row:
"XML_F52E2B61-18A1-11d1-B105-00805F49916B".

I can change column headings normally with sql results but how can it be
done for the single result column of a for xml query? My xml is below. I
don't know what to call this row other than a header so can't find a prevoius
solution to this problem so apologies if I am reproducing a many-times-asked
question.

Thanks,

Tristan


p.s. an example result
XML_F52E2B61-18A1-11d1-B105-00805F49916B
<myorders xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<OrderHeader>
<OrderHeaderID>37424</OrderHeaderID>
<OrderRef>37424</OrderRef>
<ProductTypeID>10</ProductTypeID>
<LimitPrice xsi:nil="true"/>...

--
Managing knowledge is just as important as managing technology.
Author
5 May 2009 11:47 AM
Martin Honnen
Tristan wrote:

Show quoteHide quote
> I'm getting results out of sql server 2005 as xml in the desired format
> except the results header which is put before the xml. Any ideas how to
> remove or modify the results header when returning data for xml? For example,
> with the following query...
>
> select top 1 *
> from OrderHeader
> WHERE limitprice is null
> for xml auto, elements xsinil, ROOT('myorders')
>
> ...the xml returned (below) is headed with this row:
> "XML_F52E2B61-18A1-11d1-B105-00805F49916B".
>
> I can change column headings normally with sql results but how can it be
> done for the single result column of a for xml query?

Try like this:

SELECT (SELECT TOP 1 *
FROM Orders
FOR XML AUTO, ROOT('ORDERS'), TYPE) AS XmlCol;

so for your query that should be

SELECT (select top 1 *
from OrderHeader
WHERE limitprice is null
for xml auto, elements xsinil, ROOT('myorders'), TYPE) AS XmlCol;

where 'XmlCol' is the column name you want to have.



--

    Martin Honnen --- MVP XML
    http://msmvps.com/blogs/martin_honnen/
Are all your drivers up to date? click for free checkup

Author
5 May 2009 12:27 PM
Tristan
Martin's solution (thanks Martin!) can be adapted to give the XML declaration
as the first line in xml produced by a 'for xml' query:
e.g.
select (
select top 1 *
from tablename
for xml auto, elements xsinil, ROOT('myXml'))
as '<?xml version="1.0" encoding="utf-8"?>'

....gives:

<?xml version="1.0" encoding="utf-8"?>
<myXml xmlns:xsi="http://www.w3.org/2001/XMLSchema-Instance">
<OrderHeader>
<OrderHeaderID>37424</OrderHeaderID>
....

Thanks for the quick reply Martin.
Tristan

--
Managing knowledge is just as important as managing technology.


Show quoteHide quote
"Martin Honnen" wrote:

> Tristan wrote:
>
> > I'm getting results out of sql server 2005 as xml in the desired format
> > except the results header which is put before the xml. Any ideas how to
> > remove or modify the results header when returning data for xml? For example,
> > with the following query...
> >
> > select top 1 *
> > from OrderHeader
> > WHERE limitprice is null
> > for xml auto, elements xsinil, ROOT('myorders')
> >
> > ...the xml returned (below) is headed with this row:
> > "XML_F52E2B61-18A1-11d1-B105-00805F49916B".
> >
> > I can change column headings normally with sql results but how can it be
> > done for the single result column of a for xml query?
>
> Try like this:
>
> SELECT (SELECT TOP 1 *
> FROM Orders
> FOR XML AUTO, ROOT('ORDERS'), TYPE) AS XmlCol;
>
> so for your query that should be
>
> SELECT (select top 1 *
> from OrderHeader
> WHERE limitprice is null
> for xml auto, elements xsinil, ROOT('myorders'), TYPE) AS XmlCol;
>
> where 'XmlCol' is the column name you want to have.
>
>
>
> --
>
>     Martin Honnen --- MVP XML
>     http://msmvps.com/blogs/martin_honnen/
>

Bookmark and Share

Post Thread options