Hi,
I have a stored Procedure that returns XML using FOR XML Explicit clause.
The xml is then validated (by appending header etc. in a c# application)
using a XSD schema.
The problem I am facing is that if I leave date and numeric fields as NULL
the schema validation fails.
How can I set the output to contain the xsi:nil="true" attribute?
Somewhere on this forum I saw that this can be achieved using FOR XML
ELEMENTS XSINIL, but on sql 2005.
How can I achieve this on sQL 2000?Two ways:
1. add the necessary columns to your FOR XML EXPLICIT query to generate the
xmlns:xsi namespace declaration and the xsi:nil = true or false attribute on
the element.
E.g.,
select 1 as tag, 0 as parent,
CustomerID as "Customer!1!id",
'http://www.w3.org/2001/XMLSchema-instance' as "Customer!1!xmlns:xsi",
NULL as "Region!2!xsi:nil",
NULL as "Region!2!"
from Customers
union all
select 2 as tag, 1 as parent,
CustomerID,
NULL,
CASE WHEN Region is NULL THEN
'true'
ELSE
'false'
END,
Region
from Customers
order by "Customer!1!id"
for xml explicit
or (of course recommended ;-))
2. upgrade to SQL Server 2005.
Best regards
Michael
"Nabeel Moeen" <NabeelMoeen@.discussions.microsoft.com> wrote in message
news:F2C7CF7F-2947-4398-AF86-952D71EBFCEF@.microsoft.com...
> Hi,
> I have a stored Procedure that returns XML using FOR XML Explicit clause.
> The xml is then validated (by appending header etc. in a c# application)
> using a XSD schema.
> The problem I am facing is that if I leave date and numeric fields as NULL
> the schema validation fails.
> How can I set the output to contain the xsi:nil="true" attribute?
> Somewhere on this forum I saw that this can be achieved using FOR XML
> ELEMENTS XSINIL, but on sql 2005.
> How can I achieve this on sQL 2000?
>|||Is there an easier way to do this in the C# code once i have the XML result
from the stored procedure?
"Michael Rys [MSFT]" wrote:
> Two ways:
> 1. add the necessary columns to your FOR XML EXPLICIT query to generate th
e
> xmlns:xsi namespace declaration and the xsi:nil = true or false attribute
on
> the element.
> E.g.,
> select 1 as tag, 0 as parent,
> CustomerID as "Customer!1!id",
> 'http://www.w3.org/2001/XMLSchema-instance' as "Customer!1!xmlns:xsi",
> NULL as "Region!2!xsi:nil",
> NULL as "Region!2!"
> from Customers
> union all
> select 2 as tag, 1 as parent,
> CustomerID,
> NULL,
> CASE WHEN Region is NULL THEN
> 'true'
> ELSE
> 'false'
> END,
> Region
> from Customers
> order by "Customer!1!id"
> for xml explicit
>
>
> or (of course recommended ;-))
> 2. upgrade to SQL Server 2005.
> Best regards
> Michael
> "Nabeel Moeen" <NabeelMoeen@.discussions.microsoft.com> wrote in message
> news:F2C7CF7F-2947-4398-AF86-952D71EBFCEF@.microsoft.com...
>
>|||Depends on what you consider easy. You would have to scan through the XML
and identify missing elements or elements with an empty or specially marked
content and change them... you could do it using an XSLT transform or some
C# code. I would consider that more complex, but then I am used to a
declarative way of generating data.
Best regards
Michael
"Nabeel Moeen" <NabeelMoeen@.discussions.microsoft.com> wrote in message
news:26970D6F-D704-4DD9-A43F-39A8CCE77FBA@.microsoft.com...
> Is there an easier way to do this in the C# code once i have the XML
> result
> from the stored procedure?
> "Michael Rys [MSFT]" wrote:
>|||Michael,
I have been at this thing for hours but can't figure out how to Query the
following table, [Customers] to result in the required XML (below):
[Customers]
ID RegionName
CustomerName
________________________________________
______________________
1 North
Nabeel
1 NULL
Nabeel
2 North
NULL
3 NULL
NULL
3 South
Nabeel
[XML OUTPUT]
<root>
<Customer>
<Id>1</Id>
<Region>
<CustomerName>Nabeel</CustomerName>
<RegionName>North</RegionName>
</Region>
<Region>
<CustomerName>Nabeel</CustomerName>
<RegionName xsi:nil="true"/>
</Region>
</Customer>
<Customer>
<Id>2</Id>
<Region>
<CustomerName xsi:nil="true"/>
<RegionName>North</RegionName>
</Region>
</Customer>
<Customer>
<Id>3</Id>
<Region>
<CustomerName xsi:nil="true"/>
<RegionName xsi:nil="true"/>
</Region>
<Region>
<CustomerName>Nabeel</CustomerName>
<RegionName>South</RegionName>
</Region>
</Customer>
</root>
"Michael Rys [MSFT]" wrote:
> Two ways:
> 1. add the necessary columns to your FOR XML EXPLICIT query to generate th
e
> xmlns:xsi namespace declaration and the xsi:nil = true or false attribute
on
> the element.
> E.g.,
> select 1 as tag, 0 as parent,
> CustomerID as "Customer!1!id",
> 'http://www.w3.org/2001/XMLSchema-instance' as "Customer!1!xmlns:xsi",
> NULL as "Region!2!xsi:nil",
> NULL as "Region!2!"
> from Customers
> union all
> select 2 as tag, 1 as parent,
> CustomerID,
> NULL,
> CASE WHEN Region is NULL THEN
> 'true'
> ELSE
> 'false'
> END,
> Region
> from Customers
> order by "Customer!1!id"
> for xml explicit
>
>
> or (of course recommended ;-))
> 2. upgrade to SQL Server 2005.
> Best regards
> Michael
> "Nabeel Moeen" <NabeelMoeen@.discussions.microsoft.com> wrote in message
> news:F2C7CF7F-2947-4398-AF86-952D71EBFCEF@.microsoft.com...
>
>|||Hi Nabeel, sorry for the late reply... I hope this is still useful.
Here is the explicit mode solution. Note that you need a uniquefier for the
Region ID so you get different region elements.
select 1 as tag, NULL as parent,
1 as "root!1!id!hide",
'http://www.w3.org/2001/XMLSchema-instance' as
"root!1!xmlns:xsi",
NULL as "Customer!2!Id!element",
NULL as "Region!3!dummy!hide",
NULL as "CustomerName!4!",
NULL as "CustomerName!4!xsi:nil",
NULL as "RegionName!5!",
NULL as "RegionName!5!xsi:nil"
union all
select 2 as tag, 1 as parent,
1, NULL, /*root*/
c1.ID, /*Customer*/
NULL, /*Region*/
NULL, NULL, /*CustomerName*/
NULL, NULL /*RegionName*/
from (select distinct ID from Customers) c1
union all
select 3 as tag, 2 as parent,
1, NULL, /*root*/
ID, /*Customer*/
CAST(ID as varchar(100))+
CASE WHEN RegionName IS NULL
THEN '**NULL**'
ELSE RegionName END, /*Region*/
NULL, NULL, /*CustomerName*/
NULL, NULL /*RegionName*/
from Customers
union all
select 4 as tag, 3 as parent,
1, NULL, /*root*/
ID, /*Customer*/
CAST(ID as varchar(100))+
CASE WHEN RegionName IS NULL
THEN '**NULL**'
ELSE RegionName END, /*Region*/
CustomerName,
CASE WHEN CustomerName is NULL
THEN 'true'
ELSE NULL END, /*CustomerName*/
NULL, NULL /*RegionName*/
from Customers
union all
select 5 as tag, 3 as parent,
1, NULL, /*root*/
ID, /*Customer*/
CAST(ID as varchar(100))+
CASE WHEN RegionName IS NULL
THEN '**NULL**'
ELSE RegionName END, /*Region*/
NULL, NULL, /*CustomerName*/
RegionName,
CASE WHEN RegionName is NULL
THEN 'true'
ELSE NULL END /*RegionName*/
from Customers
order by "root!1!id!hide", "Customer!2!Id!element", "Region!3!dummy!hide",
tag
for xml explicit
and here for people using SQL Server 2005, the much simpler FOR XML PATH.
select c1.ID as "Id",
(select CustomerName, RegionName
from Customers c2
where c2.ID=c1.ID
for xml path('Region'), type, elements xsinil)
from (select distinct ID from Customers) c1
for xml path('Customer'), root
Best regards
Michael
"Nabeel Moeen" <NabeelMoeen@.discussions.microsoft.com> wrote in message
news:A189B17C-814E-4BD1-8A73-B5BA304E2985@.microsoft.com...
> Michael,
> I have been at this thing for hours but can't figure out how to Query the
> following table, [Customers] to result in the required XML (below):
> [Customers]
> ID RegionName
> CustomerName
> ________________________________________
______________________
> 1 North
> Nabeel
> 1 NULL
> Nabeel
> 2 North
> NULL
> 3 NULL
> NULL
> 3 South
> Nabeel
>
> [XML OUTPUT]
>
> <root>
> <Customer>
> <Id>1</Id>
> <Region>
> <CustomerName>Nabeel</CustomerName>
> <RegionName>North</RegionName>
> </Region>
> <Region>
> <CustomerName>Nabeel</CustomerName>
> <RegionName xsi:nil="true"/>
> </Region>
> </Customer>
> <Customer>
> <Id>2</Id>
> <Region>
> <CustomerName xsi:nil="true"/>
> <RegionName>North</RegionName>
> </Region>
> </Customer>
> <Customer>
> <Id>3</Id>
> <Region>
> <CustomerName xsi:nil="true"/>
> <RegionName xsi:nil="true"/>
> </Region>
> <Region>
> <CustomerName>Nabeel</CustomerName>
> <RegionName>South</RegionName>
> </Region>
> </Customer>
> </root>
> "Michael Rys [MSFT]" wrote:
>
Wednesday, March 7, 2012
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment