Consider the following SQL:
;WITH XMLNAMESPACES(DEFAULT 'http://www.mynamespace.co.uk')
,CTE_DummyData AS (
select id=1
)
select TOP 1
[@ID]=1,
(select top 1 [@ID] = 1 FROM CTE_DummyData FOR XML PATH ('Child'), TYPE)
from CTE_DummyData
FOR XML PATH ('Parent')
Thie returns the xml:
<Parent xmlns="http://www.mynamespace.co.uk" ID="1">
<Child xmlns="http://www.mynamespace.co.uk" ID="1" />
</Parent>
What I need is to return the xml with the xmlns declaration on only the root element. eg:
<Parent xmlns="http://www.mynamespace.co.uk" ID="1">
<Child ID="1" />
</Parent>
Is there a way to do this?
Note: The above SQL is an extreme simplification of the actual code, which produces a complex document, so changing from FOR XML PATH isn't really an option without giving me a couple of days of extra work to do. To be clear on the the top level of the entire document is required to have the NS, and all children should be without.
You can use the "query from hell"
You could use a UDF to generate child nodes. E.g.