sql server - How to remove :ns from xmlns:ns using sql -


i want create xml in sql

<root xmlns="http://www.example.com" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://www.example.com http://www.example.com /media/xsd/123.xsd">   <header>     <node1>test</node1>   </header> </root> 

for have used code

declare @xml xml ;with xmlnamespaces ('http://www.w3.org/2001/xmlschema-instance' xsi,  'http://www.example.com ' ns) select      @xml = ((select 'test' node1                 xml path('header'), root('root')));  set @xml.modify('insert(attribute xsi:schemalocation {"http://www.example.com  http://www.example.com /media/xsd/123.xsd"}) (/root)[1]')                                select @xml 

but output this:

<root xmlns:ns="http://www.example.com " xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://www.example.com  http://www.example.com /media/xsd/123.xsd">   <header>     <node1>test</node1>   </header> </root> 

how can remove :ns xmlns:ns?

thanks help

you need use xml namespace default namespace (instead of specifying ns prefix):

declare @xml xml  ;with xmlnamespaces('http://www.w3.org/2001/xmlschema-instance' xsi,                     default 'http://www.example.com') select     @xml = ((select 'test' node1                 xml path('header'), root('root')));  set @xml.modify('insert(attribute xsi:schemalocation {"http://www.example.com  http://www.example.com /media/xsd/123.xsd"}) (/root)[1]');                                

this gives desired output of:

<root xmlns="http://www.example.com" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance">   <header>     <node1>test</node1>   </header> </root> 

Comments