嗨,我是xml的新手
我有这样的查询
SELECT ProjectId,
ProjectCode,
ProjectName,
TechId,
-- LocationId,
( SELECT GeoId,PoliticalDivisionId ,GeographicLocationName,IsoCode,Longitude,Latitude,ParentLocationId,
t2.CreatedBy,t2.CreatedOn,t2.LastUpdatedBy,t2.LastUpdatedOn
FROM GeographicLocation t2
WHERE GeoId = t1.LocationId
FOR XML PATH('Location') ),
RtoId,
CreatedBy,
CreatedOn,
LastUpdatedBy,
LastUpdatedOn
FROM Project t1
where ProjectId=1
FOR XML PATH('ProjectInfo')
将xml返回为
<ProjectInfo>
<ProjectId>1</ProjectId>
<ProjectCode>US-W1-00001</ProjectCode>
<ProjectName>Rees</ProjectName>
<TechId>1</TechId>
<Location><GeoId>235</GeoId><PoliticalDivisionId>2</PoliticalDivisionId><GeographicLocationName>UNITED STATES</GeographicLocationName><IsoCode>US</IsoCode></Location>
<RtoId>3</RtoId>
<CreatedBy>1</CreatedBy>
<CreatedOn>2013-06-30T20:55:21.587</CreatedOn>
<LastUpdatedBy>1</LastUpdatedBy>
<LastUpdatedOn>2013-06-30T20:55:21.587</LastUpdatedOn>
prject标签以&lt;和&gt; 。但是,位置的内部标记显示为“&lt;”和“&gt;”如何将其替换为&lt;和&gt;
更新:问题中出现了一个小错误。内部xml不适用于rtoid,它适用于位置
我将查询更新为
SELECT ProjectId,
ProjectCode,
ProjectName,
TechId,
-- LocationId,
replace(replace( ( SELECT GeoId,PoliticalDivisionId ,GeographicLocationName,IsoCode,Longitude,Latitude,ParentLocationId,
t2.CreatedBy,t2.CreatedOn,t2.LastUpdatedBy,t2.LastUpdatedOn
FROM GeographicLocation t2
WHERE GeoId = t1.LocationId
FOR XML PATH('Location') ), '<', '<'), '>', '>'),
RtoId,
CreatedBy,
CreatedOn,
LastUpdatedBy,
LastUpdatedOn
FROM Project t1
where ProjectId=1
FOR XML PATH('ProjectInfo')
但仍然是相同的
答案 0 :(得分:13)
我认为正确的方法是使用TYPE Directive
SELECT ProjectId,
...,
( SELECT Geo, ...
FROM GeographicLocation t2
WHERE GeoId = t1.LocationId
FOR XML PATH('Location'), TYPE),
RtoId, ^^^^
...
FROM Project t1
where ProjectId=1
FOR XML PATH('ProjectInfo')
答案 1 :(得分:9)
我发现的方式是明确替换它们:
select ProjectId, ProjectCode, ProjectName, TechId,
replace(replace(RtoId, '<', '<'), '>', '>') as RtoId,
. . .
from (<your query here>)
答案 2 :(得分:3)
SELECT ProjectId,
ProjectCode,
ProjectName,
TechId,
-- LocationId,
replace(replace(( SELECT GeoId,PoliticalDivisionId ,GeographicLocationName,IsoCode,Longitude,Latitude,ParentLocationId,
t2.CreatedBy,t2.CreatedOn,t2.LastUpdatedBy,t2.LastUpdatedOn
FROM GeographicLocation t2
WHERE GeoId = t1.LocationId
FOR XML PATH('Location') ), '<', '<'), '>', '>')
RtoId,
CreatedBy,
CreatedOn,
LastUpdatedBy,
LastUpdatedOn
FROM Project t1
where ProjectId=1
FOR XML PATH('ProjectInfo')
答案 3 :(得分:1)
将数据格式化为xml,使用cast(@xml作为xml)。
答案 4 :(得分:0)
请尝试:
(SELECT GeoId,PoliticalDivisionId ,GeographicLocationName,IsoCode,
Longitude,Latitude,ParentLocationId,
t2.CreatedBy,t2.CreatedOn,t2.LastUpdatedBy,t2.LastUpdatedOn
FROM GeographicLocation t2
WHERE GeoId = t1.LocationId
FOR XML PATH('Location'), type
).value('(./text())[1]','varchar(max)')
答案 5 :(得分:0)
SELECT ProjectId,
ProjectCode,
ProjectName,
TechId,
-- LocationId,
cast(( SELECT GeoId,PoliticalDivisionId ,GeographicLocationName,IsoCode,Longitude,Latitude,ParentLocationId,
t2.CreatedBy,t2.CreatedOn,t2.LastUpdatedBy,t2.LastUpdatedOn
FROM GeographicLocation t2
WHERE GeoId = t1.LocationId
FOR XML PATH('Location') ) as xml),
RtoId,
CreatedBy,
CreatedOn,
LastUpdatedBy,
LastUpdatedOn
FROM Project t1
where ProjectId=1
FOR XML PATH('ProjectInfo')
答案 6 :(得分:0)
使用 FOR XML
SELECT customerid
, name
, SUBSTRING( x, 4, LEN( x) - 7) AS name2
, IIF( LEN(name) <> LEN(x) - 7, 1, 0) AS residual
FROM (SELECT customerid
, name
, (SELECT kundnamn AS n FOR XML PATH('')) as x
FROM customers
) s
或
declare @s varchar(MAX) = 'asd& < > " '' ='
PRINT @s
declare @xml varchar(MAX)
SELECT @xml = SUBSTRING( x, 4, LEN( x) - 7)
FROM (SELECT (SELECT @s AS x FOR XML PATH('')) AS x ) x
PRINT @xml
给予
asd& < > " ' =
asd& < > " ' =