I try to create a XML with sqlserver that must contain multiple namespaces. The XML should look something like this:
<ns1:Message xmlns:ns1="http://Something/A"
xmlns:ns2="http://Something/B"
xmlns:ns3="http://Something/C">
<ns1:A>
<ns1:A1>201608111003201</ns1:A1>
<ns1:A2>Some text</ns1:A2>
<ns1:A3>More text</ns1:A3>
</ns1:A>
<ns2:B>
<ns2:B1>123456788</ns2:B1>
<ns2:B2>Even more text</B2>
</ns2:B>
<ns3:C>
<ns3:C1>E232323</ns3:C1>
<ns3:C2>P</ns3:C2>
</ns3:C>
</ns1:Message
My query does look something like this now with only one namespace.
WITH XMLNAMESPACES ('http://Something/A' as ns3,
'http://Something/B' as ns2,
'http://Something/C' as ns1)
SELECT COLUMN1 as 'ns1:A1',
COLUMN2 as 'ns1:A2',
COLUMN3 as 'ns1:A3'
FROM MYTABLE
FOR XML PATH ('ns1:A'), ROOT('ns1:Message'), ELEMENTS
This query works fine, but when I try to add the ns2 or ns3 namespace in the query nothing seems to work. How must this be done. Thanks in advance!