12

嗨,我是 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>
&lt;Location&gt;&lt;GeoId&gt;235&lt;/GeoId&gt;&lt;PoliticalDivisionId&gt;2&lt;/PoliticalDivisionId&gt;&lt;GeographicLocationName&gt;UNITED STATES&lt;/GeographicLocationName&gt;&lt;IsoCode&gt;US&lt;/IsoCode&gt;&lt;/Location&gt;
<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>

项目标签以 < 和 > 的形式显示。但是 Location 的内部标签显示为“<” 和“>” 如何用 < 和 > 替换它们

更新:问题中有一个小错误。内部 xml 不是用于 rtoid ,而是用于 Location

我将查询更新为

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') ), '&lt;', '<'), '&gt;', '>'),
       RtoId, 
       CreatedBy,
       CreatedOn,
       LastUpdatedBy,
       LastUpdatedOn
FROM Project t1
where ProjectId=1
FOR XML PATH('ProjectInfo')

但还是一样

4

7 回答 7

17

我认为正确的方法是使用TYPE 指令

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') 
于 2014-06-26T08:15:14.170 回答
14

我发现的方法是明确替换它们:

select ProjectId, ProjectCode, ProjectName, TechId,
       replace(replace(RtoId, '&lt;', '<'), '&gt;', '>') as RtoId, 
       . . .
from (<your query here>)
于 2013-07-06T14:18:04.467 回答
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') ), '&lt;', '<'), '&gt;', '>')
       RtoId, 
       CreatedBy,
       CreatedOn,
       LastUpdatedBy,
       LastUpdatedOn
FROM Project t1
where ProjectId=1
FOR XML PATH('ProjectInfo')
于 2013-07-06T14:23:58.203 回答
3

请试试:

(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)')
于 2018-02-07T13:03:55.030 回答
2

将数据格式化为 xml,使用 cast(@xml as xml)。

于 2016-05-20T09:16:58.513 回答
1
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')
于 2018-06-21T12:38:24.213 回答
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&amp; &lt; &gt; " ' =
于 2021-02-09T07:53:40.980 回答