I need to select some columns from a table as XML with namespaces included in them along with other columns as is. For example, I have a following table layout:
ID C1 X1C1 X1C2 X2C3
1 A 1 2 3
What the query should return is:
ID C1 XmlData
1 A <xmldata1>
2 A <xmldata2>
Where <xmldata1>
would be:
<Root xmlns:xsd="w3.org/2001/XMLSchema" xmlns:xsi="w3.org/2001/XMLSchema-instance" xmlns:mst="microsoft.com/wsdl/types/">
<Child attrib="C1">
<ChildValue xsi:type="xsd:integer">1</ChildNode>
</Child>
<Child attrib="C2">
<ChildNode xsi:type="xsd:integer">2</ChildNode>
</Child>
</Root>
and <xmldata2>
would be:
<Rootxmlns:xsd="w3.org/2001/XMLSchema" xmlns:xsi="w3.org/2001/XMLSchema-instance" xmlns:mst="microsoft.com/wsdl/types/">
<Child attrib="C3">
<ChildNode xsi:type="xsd:integer">3</ChildNode>
</Child>
</Root>
I have a good reference how to build the xml from this SO question but I'm not able to put in the namespaces. If this is possible how to do it?
Edit: I've used following query attempting to get the required result: select 1 ID, 'A' C1, 1 X1C1, 2 X1C2, 3 X2C3 into #t
;with xmlnamespaces('w3.org/2001/XMLSchema' as xsd, 'w3.org/2001/XMLSchema-instance' as xsi, 'microsoft.com/wsdl/types/' as mst)
select ID, C1, (select (SELECT 'C1' "@attrib", 'xsd:integer' "ChildValue/@xsi:type",t.X1C1 as 'ChildValue' FOR XML PATH('Child'), type),(SELECT 'C2' "@name", 'xsd:integer' "ChildValue/@xsi:type", t.X1C2 as 'ChildValue' FOR XML PATH('Child'), type) FOR XML PATH('Root'), type) as property_data
FROM #t t
drop table #t
Here is the output of its xml part:
<Root xmlns:mst="microsoft.com/wsdl/types/" xmlns:xsi="w3.org/2001/XMLSchema-instance" xmlns:xsd="w3.org/2001/XMLSchema">
<Child xmlns:mst="microsoft.com/wsdl/types/" xmlns:xsi="w3.org/2001/XMLSchema-instance" xmlns:xsd="w3.org/2001/XMLSchema" attrib="C1">
<ChildValue xsi:type="xsd:integer">1</ChildValue>
</Child>
<Child xmlns:mst="microsoft.com/wsdl/types/" xmlns:xsi="w3.org/2001/XMLSchema-instance" xmlns:xsd="w3.org/2001/XMLSchema" name="C2">
<ChildValue xsi:type="xsd:integer">2</ChildValue>
</Child>
</Root>
I can't get rid of the namespaces in the Child
node.
I used this solution: TSQL for xml add schema attribute to root node
Basically, I did not put the namespace in the beginning but after generating the required xml structure I casted the xml to nvarchar(max)
and replaced the root node with the desired namespace.
I also needed to use namespace prefix in the attribute. For that I used a pseudo attribute name which I replaced with a proper xml namespace prefix.
Both operations were done using tsql REPLACE
function. Hacky but couldn't find other proper ways to do it.
you need to include WITH xmlnamespaces , example:
;with xmlnamespaces('w3.org/2001/XMLSchema' as xsd, 'w3.org/2001/XMLSchema-instance' as xsi, 'microsoft.com/wsdl/types/' as mst)
select ID, C1,
(select
(SELECT 'C1' "@name",t.C1 as 'value'FOR XML PATH('Property'), type),
(SELECT 'C2' "@name",t.C2 as 'value'FOR XML PATH('property'), type)
FOR XML PATH('data'), type) as property_data
FROM TableName t
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With