我想在SQL中创建一个像这样的XML
<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>
为此,我使用了代码
declare @xml xml
;with xmlnamespaces ('http://www.w3.org/2001/XMLSchema-instance' as xsi, 'http://www.example.com ' as ns)
select
@xml = ((SELECT 'Test' as Node1
FOR XML PATH('Header'), ROOT('Root')));
set @xml.modify('insert(attribute xsi:schemaLocation {"http://www.example.com http://www.example.com /media/XSD/123.xsd"}) into (/Root)[1]')
select @xml
但输出是这样的:
<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>
如何从:ns
移除xmlns:ns
?
感谢您的帮助
答案 0 :(得分:0)
您需要将此XML命名空间用作默认命名空间(而不是指定ns
前缀):
DECLARE @xml XML
;WITH XMLNAMESPACES('http://www.w3.org/2001/XMLSchema-instance' AS xsi,
DEFAULT 'http://www.example.com')
SELECT
@xml = ((SELECT 'Test' as Node1
FOR XML PATH('Header'), ROOT('Root')));
SET @xml.modify('insert(attribute xsi:schemaLocation {"http://www.example.com http://www.example.com /media/XSD/123.xsd"}) into (/Root)[1]');
这为您提供了所需的输出:
<Root xmlns="http://www.example.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Header>
<Node1>Test</Node1>
</Header>
</Root>