本文介绍了FOR XML PATH(''):将“特殊"转义为“特殊".人物的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这段代码基本上将一个字符串中的位置的字符转换为另一字符串中相同位置的字符,并在表中的所有行中运行.

This code basically translates characters based on position in one string to the character at the same position in another string and it runs for all rows in the table.

运行此文件(简化版)时:

When I run this (simplified version):

DECLARE @R           char(40)
DECLARE @U           char(40)
SET @R=' abcdefghijklmnopqrstuvwxyz!@#$%^&*()_+'+char(181)
SET @U=REVERSE(@R)

DECLARE @TestTable TABLE (RowID int identity(1,1) primary key, Unreadable  varchar(500))
INSERT INTO @TestTable VALUES ('+µt$zw!*µsu+yt!+s$xy')
INSERT INTO @TestTable VALUES ('%*!!xµpxu!(')
INSERT INTO @TestTable VALUES ('pxpµnxrµu+yµs%$t')


    ;WITH CodeValues AS
    (
    SELECT
        Number,SUBSTRING(@R,Number,1) AS R,ASCII(SUBSTRING(@U,Number,1)) AS UA
        FROM Numbers
        WHERE Number<=LEN(@R)
    )
    SELECT
        t.RowID
            ,(SELECT
                  ''+c.R
                  FROM Numbers               n
                      INNER JOIN CodeValues  c ON ASCII(SUBSTRING(t.Unreadable,n.Number,1))=c.UA
                  WHERE n.Number<=LEN(t.Unreadable)
                  FOR XML PATH('')
             ) AS readable
        FROM @TestTable t

我得到以下信息:

RowID       readable
----------- ---------------------------------------
1           a&#x20;simple&#x20;translation
2           hello&#x20;world
3           wow&#x20;you&#x20;ran&#x20;this

但需要:

RowID       readable
----------- ---------------------------------------
1           a simple translation
2           hello world
3           wow you ran this

除了REPLACE()之外,还有其他方法可以正确显示空格吗?在我的实际代码中,这也发生在换行符上.

Is there any way, other than REPLACE(), to have the spaces show up properly? This also happens on line breaks, in my actual code.

这可以用更好的方式重写吗?我基本上只是使用FOR XML PATH('')将各个行值连接在一起.

Can this be rewritten in a better way? I basically just used the FOR XML PATH('') to concatenate the individual row values together.

推荐答案

您获得的XML是正确的.它是 XML ,不是文本,并且可以被XML解析器读取为XML.特殊字符应该正确地转义.无论您使用哪种使用该XML的客户端模块,都应将其解析为XML,而不是文本,然后它将正确显示.

The XML you get is correct. It is XML, not text, and readable as XML by an XML parser. Special characters are properly escaped, as they should be. Whatever client module you have that consumes that XML should parse it as XML, not as text, and then it will display properly.

更新:

如果不清楚,则查询中需要做的就是将XML视为XML,将文本视为文本,而不是将XML混合为文本,即:

In case is not clear, all you need to do in your query is to treat XML as XML and text as text, not mix XML as text, ie:

;WITH CodeValues AS
    (
    SELECT
        Number,SUBSTRING(@R,Number,1) AS R,ASCII(SUBSTRING(@U,Number,1)) AS UA
        FROM Numbers
        WHERE Number<=LEN(@R)
    )
, XmlValues AS (
SELECT
        t.RowID
            ,(SELECT
                  ''+c.R
                  FROM Numbers               n
                      INNER JOIN CodeValues  c ON ASCII(SUBSTRING(t.Unreadable,n.Number,1))=c.UA
                  WHERE n.Number<=LEN(t.Unreadable)
                  FOR XML PATH(''), TYPE
             ) AS readable
        FROM @TestTable t)
SELECT x.RowId,
    x.readable.value('.', 'VARCHAR(8000)') as readable
    FROM XmlValues AS x

这篇关于FOR XML PATH(''):将“特殊"转义为“特殊".人物的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 10:19