如何将命名空间添加到 SQL 服务器生成的 XML 输出
How to add namespace to SQL Server generated XML output
我使用 SQL 服务器查询生成了以下 XML 输出(添加到 rextester link):
<Main>
<ID>1001</ID>
<details>
<name>John</name>
<age>12</age>
</details>
</Main>
我想知道如何将命名空间 xmlns:json="http://www.samplenamespace.com/json"
添加到 Main
节点。
期望的输出:
<Main xmlns:json="http://www.samplenamespace.com/json">
<ID>1001</ID>
<details>
<name>John</name>
<age>12</age>
</details>
</Main>
Rextester link:http://rextester.com/OQZH6668
有帮助吗!?
您需要使用WITH XMLNAMESPACES
子句,例如:
---Fake tables in Stored procedure1
create table #Cdetails(cid int, name varchar(5), age int)
insert into #Cdetails
values(1001,'John',12),
(1002,'Rick',19),
(1003,'Diane',25),
(1004,'Kippy',26)
--Output of Stored procedure
create table #final(xml_data xml)
insert into #final
select
XML_data =
(select ID = cd1.cid,
details =
(
select cd.name,
cd.age
from #Cdetails cd
where cd.cid = cd1.cid
For XML Path(''), Type)
from #Cdetails cd1
For XML Path('Main'));
WITH XMLNAMESPACES ('http://www.samplenamespace.com/json' as json)
select * from #final
For XML Path('Main')
drop table #Cdetails,#final
请注意使用 WITH
语句时需要的额外 ;
。
Rextester link:http://rextester.com/EBLL48414
我使用 SQL 服务器查询生成了以下 XML 输出(添加到 rextester link):
<Main>
<ID>1001</ID>
<details>
<name>John</name>
<age>12</age>
</details>
</Main>
我想知道如何将命名空间 xmlns:json="http://www.samplenamespace.com/json"
添加到 Main
节点。
期望的输出:
<Main xmlns:json="http://www.samplenamespace.com/json">
<ID>1001</ID>
<details>
<name>John</name>
<age>12</age>
</details>
</Main>
Rextester link:http://rextester.com/OQZH6668
有帮助吗!?
您需要使用WITH XMLNAMESPACES
子句,例如:
---Fake tables in Stored procedure1
create table #Cdetails(cid int, name varchar(5), age int)
insert into #Cdetails
values(1001,'John',12),
(1002,'Rick',19),
(1003,'Diane',25),
(1004,'Kippy',26)
--Output of Stored procedure
create table #final(xml_data xml)
insert into #final
select
XML_data =
(select ID = cd1.cid,
details =
(
select cd.name,
cd.age
from #Cdetails cd
where cd.cid = cd1.cid
For XML Path(''), Type)
from #Cdetails cd1
For XML Path('Main'));
WITH XMLNAMESPACES ('http://www.samplenamespace.com/json' as json)
select * from #final
For XML Path('Main')
drop table #Cdetails,#final
请注意使用 WITH
语句时需要的额外 ;
。
Rextester link:http://rextester.com/EBLL48414