Jeg tror ikke, det er muligt at forhindre denne adfærd med underforespørgsler ved hjælp af for xml path
. Der er et lignende problem rapporteret her. https ://connect.microsoft.com/SQLServer/feedback/details/265956/suppress-namespace-attributes-in-nested-select-for-xml-statements
Du kan få det output, du ønsker, hvis du bruger for xml explicit
i stedet.
declare @T table(Level1 int, Level2 int)
insert into @T values(null, 2)
select 1 as Tag,
null as Parent,
null as [root!1],
null as [nested1!2!level1!ELEMENTXSINIL],
null as [nested1!2!level2!ELEMENTXSINIL],
null as [nested2!3!level1!ELEMENTXSINIL],
null as [nested2!3!level2!ELEMENTXSINIL]
union all
select 2 as Tag,
1 as Parent,
null,
Level1,
Level2,
null,
null
from @T
union all
select 3 as Tag,
1 as Parent,
null,
null,
null,
Level1,
Level2
from @T
for xml explicit
Resultat:
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<nested1>
<level1 xsi:nil="true" />
<level2>2</level2>
</nested1>
<nested2>
<level1 xsi:nil="true" />
<level2>2</level2>
</nested2>
</root>