Sql server 使用T-SQL语法生成复杂的XML
我正在尝试构建一个查询,该查询将返回或多或少复杂的XML结构。这是我想要的预期输出:Sql server 使用T-SQL语法生成复杂的XML,sql-server,xml,sql-server-2008,sql-server-2008-r2,sql-server-2012,Sql Server,Xml,Sql Server 2008,Sql Server 2008 R2,Sql Server 2012,我正在尝试构建一个查询,该查询将返回或多或少复杂的XML结构。这是我想要的预期输出: <s:Envelope xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:s="http://www.w3.org/2003/05/soap-envelope"> <s:Header> <a:Action s:mustUnderstand="1">http://tempuri.org/IGeoco
<s:Envelope xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:s="http://www.w3.org/2003/05/soap-envelope">
<s:Header>
<a:Action s:mustUnderstand="1">http://tempuri.org/IGeocodeService/HereRouteMatchExtension</a:Action>
<a:MessageID>urn:uuid: some_messageID</a:MessageID>
<a:ReplyTo>
<a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>
</a:ReplyTo>
<a:To s:mustUnderstand="1">ServiceURL</a:To>
</s:Header>
<s:Body>
<HereRouteMatchExtension xmlns="http://tempuri.org/">
<vehicleTrace xmlns:b="http://schemas.datacontract.org/2004/07/FMCommonTypes.WCF" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<s:Latitude>2</s:Latitude>
<s:Longitude>2</s:Longitude>
<s:PositionGuid>577AF773-C7A8-4D65-82DA-37A15CC7611D</s:PositionGuid>
</vehicleTrace>
</HereRouteMatchExtension>
</s:Body>
</s:Envelope>
我生成的代码中有两个问题:
1) 参考网址是在每个小节,我想只有一次
2) 车身标签在车头内侧,应该在车头后面一直走
我怎样才能做到这一点?这就是我得到的结果:
<s:Envelope xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:s="http://www.w3.org/2003/05/soap-envelope">
<s:Header>
<a:Action xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:s="http://www.w3.org/2003/05/soap-envelope" mustUnderstand="1">http://tempuri.org/IGeocodeService/HereRouteMatchExtension</a:Action>
<a:MessageID>urn:uuid: some_messageID</a:MessageID>
<a:ReplyTo xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:s="http://www.w3.org/2003/05/soap-envelope">
<a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>
</a:ReplyTo>
<a:To xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:s="http://www.w3.org/2003/05/soap-envelope" mustUnderstand="1">ServiceURL</a:To>
<a:Action xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:s="http://www.w3.org/2003/05/soap-envelope" mustUnderstand="1" />
<s:Body xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:s="http://www.w3.org/2003/05/soap-envelope">
<s:Latitude>1</s:Latitude>
<s:Longitude>1</s:Longitude>
<s:PositionGuid>34BD8E91-8567-4D58-A18E-61C3FBBF5C8F</s:PositionGuid>
</s:Body>
</s:Header>
</s:Envelope>
http://tempuri.org/IGeocodeService/HereRouteMatchExtension
urn:uuid:some\u messageID
http://www.w3.org/2005/08/addressing/anonymous
服务URL
1.
1.
34BD8E91-8567-4D58-A18E-61C3FBBF5C8F
试试看
DECLARE @test TABLE
(
Latitude INT,
Longitude INT,
PositionGuid UNIQUEIDENTIFIER
)
INSERT INTO @test VALUES (1,1,NEWID());
INSERT INTO @test VALUES (2,2,NEWID());
WITH XMLNAMESPACES ('http://www.w3.org/2003/05/soap-envelope' AS s, 'http://www.w3.org/2005/08/addressing' AS a)
SELECT
1 AS [s:Envelope/s:Header/a:Action/@s:mustUnderstand],
'http://tempuri.org/IGeocodeService/HereRouteMatchExtension' AS [s:Envelope/s:Header/a:Action],
'urn:uuid: some_messageID' AS [s:Envelope/s:Header/a:MessageID],
'http://www.w3.org/2005/08/addressing/anonymous' [s:Envelope/s:Header/a:ReplyTo/a:Address],
1 as [s:Envelope/s:Header/To/@s:mustUnderstand],
'ServiceURL' as [s:Envelope/s:Header/To],
Latitude as [s:Envelope/s:Body/s:Latitude],
Longitude as [s:Envelope/s:Body/s:Longitude],
PositionGuid as [s:Envelope/s:Body/s:PositionGuid]
FROM @test
FOR XML PATH('')
这样,您可以为@table的每一行生成两个信封元素
编辑:
在使用FOR XML PATH的子查询中使用TYPE生成时,我找不到从子元素中删除额外名称空间的方法
我提出的解决方案并不能完全满足我的要求:
DECLARE @test TABLE
(
Latitude INT,
Longitude INT,
PositionGuid UNIQUEIDENTIFIER,
x xml
)
DECLARE @x xml =
'<s:Envelope xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:s="http://www.w3.org/2003/05/soap-envelope">
<s:Header>
<a:Action s:mustUnderstand="1">http://tempuri.org/IGeocodeService/HereRouteMatchExtension</a:Action>
<a:MessageID>urn:uuid: some_messageID</a:MessageID>
<a:ReplyTo>
<a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>
</a:ReplyTo>
<a:To s:mustUnderstand="1">ServiceURL</a:To>
</s:Header>
<s:Body>
<HereRouteMatchExtension xmlns="http://tempuri.org/">
<vehicleTrace xmlns:b="http://schemas.datacontract.org/2004/07/FMCommonTypes.WCF" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<s:Latitude>2</s:Latitude>
<s:Longitude>2</s:Longitude>
<s:PositionGuid>577AF773-C7A8-4D65-82DA-37A15CC7611D</s:PositionGuid>
</vehicleTrace>
</HereRouteMatchExtension>
</s:Body>
</s:Envelope>'
INSERT INTO @test VALUES (10,10,NEWID(),@x);
INSERT INTO @test VALUES (20,20,NEWID(),@x);
UPDATE @test
SET x.modify('declare namespace s="http://www.w3.org/2003/05/soap-envelope";declare default element namespace "http://tempuri.org/";
replace value of (/s:Envelope/s:Body/HereRouteMatchExtension/vehicleTrace/s:Latitude/text())[1] with sql:column("Latitude")')
UPDATE @test
SET x.modify('declare namespace s="http://www.w3.org/2003/05/soap-envelope";declare default element namespace "http://tempuri.org/";
replace value of (/s:Envelope/s:Body/HereRouteMatchExtension/vehicleTrace/s:Longitude/text())[1] with sql:column("Longitude")')
UPDATE @test
SET x.modify('declare namespace s="http://www.w3.org/2003/05/soap-envelope";declare default element namespace "http://tempuri.org/";
replace value of (/s:Envelope/s:Body/HereRouteMatchExtension/vehicleTrace/s:PositionGuid/text())[1] with sql:column("PositionGuid")')
SELECT * FROM @test
DECLARE@testtable
(
纬度INT,
经度INT,
位置GUID唯一标识符,
xml
)
声明@x xml=
'
http://tempuri.org/IGeocodeService/HereRouteMatchExtension
urn:uuid:some\u messageID
http://www.w3.org/2005/08/addressing/anonymous
服务URL
2.
2.
577AF773-C7A8-4D65-82DA-37A15CC7611D
'
插入@test VALUES(10,10,NEWID(),@x);
插入@test VALUES(20,20,NEWID(),@x);
更新@test
SET x.modify('声明命名空间s='http://www.w3.org/2003/05/soap-envelope“声明默认元素命名空间”http://tempuri.org/";
将(/s:Envelope/s:Body/HereRouteMatcheExtension/vehicleTrace/s:Latitude/text())[1]的值替换为sql:column(“Latitude”))
更新@test
SET x.modify('声明命名空间s='http://www.w3.org/2003/05/soap-envelope“声明默认元素命名空间”http://tempuri.org/";
将(/s:Envelope/s:Body/hereRouteMatcheExtension/vehicleTrace/s:Longitude/text())[1]的值替换为sql:column(“Longitude”))
更新@test
SET x.modify('声明命名空间s='http://www.w3.org/2003/05/soap-envelope“声明默认元素命名空间”http://tempuri.org/";
将(/s:Envelope/s:Body/HereRouteMatcheExtension/vehicleTrace/s:PositionGuid/text())[1]的值替换为sql:column(“PositionGuid”))
从@test中选择*
同时插入以下内容:
DECLARE @test TABLE
(
Latitude INT,
Longitude INT,
PositionGuid UNIQUEIDENTIFIER,
x xml
)
DECLARE @x xml =
'<s:Envelope xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:s="http://www.w3.org/2003/05/soap-envelope">
<s:Header>
<a:Action s:mustUnderstand="1">http://tempuri.org/IGeocodeService/HereRouteMatchExtension</a:Action>
<a:MessageID>urn:uuid: some_messageID</a:MessageID>
<a:ReplyTo>
<a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>
</a:ReplyTo>
<a:To s:mustUnderstand="1">ServiceURL</a:To>
</s:Header>
<s:Body>
<HereRouteMatchExtension xmlns="http://tempuri.org/">
</HereRouteMatchExtension>
</s:Body>
</s:Envelope>'
INSERT INTO @test VALUES (10,10,NEWID(),@x);
INSERT INTO @test VALUES (20,20,NEWID(),@x);
UPDATE @test
SET x.modify('declare namespace s="http://www.w3.org/2003/05/soap-envelope";declare default element namespace "http://tempuri.org/";
insert <vehicleTrace xmlns:b="http://schemas.datacontract.org/2004/07/FMCommonTypes.WCF" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><s:Latitude>{sql:column("Latitude")}</s:Latitude>
<s:Longitude>{sql:column("Longitude")}</s:Longitude>
<s:PositionGuid>{sql:column("PositionGuid")}</s:PositionGuid></vehicleTrace> into (/s:Envelope/s:Body/HereRouteMatchExtension)[1]')
SELECT * FROM @test
DECLARE@testtable
(
纬度INT,
经度INT,
位置GUID唯一标识符,
xml
)
声明@x xml=
'
http://tempuri.org/IGeocodeService/HereRouteMatchExtension
urn:uuid:some\u messageID
http://www.w3.org/2005/08/addressing/anonymous
服务URL
'
插入@test VALUES(10,10,NEWID(),@x);
插入@test VALUES(20,20,NEWID(),@x);
更新@test
SET x.modify('声明命名空间s='http://www.w3.org/2003/05/soap-envelope“声明默认元素命名空间”http://tempuri.org/";
插入{sql:column(“Latitude”)}
{sql:column(“经度”)}
{sql:column(“PositionGuid”)}插入(/s:Envelope/s:Body/HereRouteMatchExtension)[1]'))
从@test中选择*
所说的“参考URL”是指xmlns:a
etc命名空间声明吗?谢谢。这正是我要问的。不过我把问题修改了一点,你能看一下。。。。我对代码做了一点修改。我认为也可以通过一次插入而不是三次替换来完成,因为第二段和第三段代码没有生成。它已经在那里了。在第一种情况下,代码只是替换经度、纬度和位置GUID元素的值。在第二个例子中,它插入了一个构造好的vehicleTrace元素。我的意思是,我修改了问号(内部标记)。你能告诉我怎么做吗?第二个和第三个是基于修改后的问题,你试过运行它们吗?
DECLARE @test TABLE
(
Latitude INT,
Longitude INT,
PositionGuid UNIQUEIDENTIFIER,
x xml
)
DECLARE @x xml =
'<s:Envelope xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:s="http://www.w3.org/2003/05/soap-envelope">
<s:Header>
<a:Action s:mustUnderstand="1">http://tempuri.org/IGeocodeService/HereRouteMatchExtension</a:Action>
<a:MessageID>urn:uuid: some_messageID</a:MessageID>
<a:ReplyTo>
<a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>
</a:ReplyTo>
<a:To s:mustUnderstand="1">ServiceURL</a:To>
</s:Header>
<s:Body>
<HereRouteMatchExtension xmlns="http://tempuri.org/">
</HereRouteMatchExtension>
</s:Body>
</s:Envelope>'
INSERT INTO @test VALUES (10,10,NEWID(),@x);
INSERT INTO @test VALUES (20,20,NEWID(),@x);
UPDATE @test
SET x.modify('declare namespace s="http://www.w3.org/2003/05/soap-envelope";declare default element namespace "http://tempuri.org/";
insert <vehicleTrace xmlns:b="http://schemas.datacontract.org/2004/07/FMCommonTypes.WCF" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><s:Latitude>{sql:column("Latitude")}</s:Latitude>
<s:Longitude>{sql:column("Longitude")}</s:Longitude>
<s:PositionGuid>{sql:column("PositionGuid")}</s:PositionGuid></vehicleTrace> into (/s:Envelope/s:Body/HereRouteMatchExtension)[1]')
SELECT * FROM @test