我想在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>如何从xmlns:ns中删除:ns
谢谢你的帮忙
发布于 2015-07-17 17:15:40
您需要将此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>https://stackoverflow.com/questions/31471953
复制相似问题