开发者

SQL STUFF() function keeps escaping my HTML

开发者 https://www.devze.com 2023-02-22 09:15 出处:网络
Using the tutorial here: http://www.sqlservercurry.com/2008/06/combine-multiple-rows开发者_JS百科-into-one-row.html

Using the tutorial here:

http://www.sqlservercurry.com/2008/06/combine-multiple-rows开发者_JS百科-into-one-row.html

I wrote the following query:

SELECT DISTINCT STUFF( (SELECT '<li>' + CountryCode + '</li>' from Country  FOR XML PATH('')),1,1,'') as Codes FROM Country

This works great, but all my HTML is now encoded (i.e. &lt;li&gt;)

Is there a way around this? Using a SUBSTRING() approach does this also.


Change '<li>' + CountryCode + '</li>' to CountryCode as li and you will probably get what you want.


it is because XML PATH makes it xml safe so < becomes &lt;

try

DECLARE @CountryCode VARCHAR(MAX)
SELECT @CountryCode = ''
SELECT  @CountryCode = @CountryCode + '<li>' + CountryCode + '</li>' from Country 

SELECT @CountryCode

example

DECLARE @CountryCode VARCHAR(MAX)
SELECT @CountryCode = ''
SELECT   @CountryCode = @CountryCode + '<li>' + name + '</li>' 
FROM master..spt_values
WHERE type = 'a'

SELECT @CountryCode

you can also of course use the REPLACE function to replace '&lt;' to '<'

In my opinion adding li should be done at the front end and not in the database

0

精彩评论

暂无评论...
验证码 换一张
取 消