XML parsing semicolon expected - only when replacing amp; with amp;amp(预期 XML 解析分号 - 仅在替换 amp; 时与 amp;amp)
问题描述
我有以下查询,它有效:
I have the following query, which works:
DECLARE @Combined VARCHAR(MAX)
SET @Combined = 'mac cheese';
DECLARE @KeyTable TABLE (Keyword VARCHAR(MAX))
INSERT INTO @KeyTable
SELECT @Combined
IF OBJECT_ID('tempdb..#Temp') IS NOT NULL
DROP TABLE #Temp
SELECT LTRIM(RTRIM(m.n.value('.[1]','varchar(8000)'))) AS Keyword
INTO #Temp
FROM
(
SELECT CAST('<XMLRoot><RowData>' + REPLACE(Keyword,',','</RowData><RowData>') + '</RowData></XMLRoot>' AS XML) AS x
FROM @KeyTable
)t
CROSS APPLY x.nodes('/XMLRoot/RowData')m(n);
SELECT * FROM #Temp
当我尝试向 @Combined
添加一个&符号时,它给出了一个特殊字符错误:
When I try to add an ampersand to the @Combined
it gives a special character error:
DECLARE @Combined VARCHAR(MAX)
SET @Combined = 'mac & cheese';
DECLARE @KeyTable TABLE (Keyword VARCHAR(MAX))
INSERT INTO @KeyTable
SELECT @Combined
IF OBJECT_ID('tempdb..#Temp') IS NOT NULL
DROP TABLE #Temp
SELECT LTRIM(RTRIM(m.n.value('.[1]','varchar(8000)'))) AS Keyword
INTO #Temp
FROM
(
SELECT CAST('<XMLRoot><RowData>' + REPLACE(Keyword,',','</RowData><RowData>') + '</RowData></XMLRoot>' AS XML) AS x
FROM @KeyTable
)t
CROSS APPLY x.nodes('/XMLRoot/RowData')m(n);
SELECT * FROM #Temp
所以我添加了一些代码来用 &
替换 & 号,但我得到了一个分号错误:
So I added some code to replace the ampersand with &
and I get a semicolon error:
DECLARE @Combined VARCHAR(MAX)
SET @Combined = 'mac & cheese'
SET @Combined = REPLACE(@Combined, '&', '&')
DECLARE @KeyTable TABLE (Keyword VARCHAR(MAX))
INSERT INTO @KeyTable
SELECT @Combined;
IF OBJECT_ID('tempdb..#Temp') IS NOT NULL
DROP TABLE #Temp
SELECT LTRIM(RTRIM(m.n.value('.[1]','varchar(8000)'))) AS Keyword
INTO #Temp
FROM
(
SELECT CAST('<XMLRoot><RowData>' + REPLACE(Keyword,',','</RowData><RowData>') + '</RowData></XMLRoot>' AS XML) AS x
FROM @KeyTable
)t
CROSS APPLY x.nodes('/XMLRoot/RowData')m(n);
SELECT * FROM #Temp
我尝试将分号移动到不同的位置,甚至添加更多,但它不起作用.任何人都可以提供的任何帮助将不胜感激!
I've attempted moving the semicolon to various locations and even adding more but it just won't work. Any help anyone can provide would be appreciated!
推荐答案
不,你不应该开始自己替换特殊字符!
No, you should not start to replace special characters on your own!
下次您的字符串包含 <
或 ->
并且会再次中断.
Next time your string contains a <
or a ->
and will break again.
让 FOR XML PATH()
为您完成繁重的工作:
Let FOR XML PATH()
do the hard work for you:
SELECT 'This is pure text: ' + 'mac & cheese,<test1>,"test2"';
SELECT 'This is encoded: ' + (SELECT 'mac & cheese,<test1>,"test2"' AS [*] FOR XML PATH(''))
第二个结果:这是编码:mac &奶酪,<test1>,<test2>
你唯一需要改变的是使用 (SELECT ... FOR XML PATH())
而不是 naked Keyword
>
The only thing you have to change, is to use (SELECT ... FOR XML PATH())
instead of the naked Keyword
DECLARE @KeyTable TABLE (Keyword VARCHAR(MAX))
INSERT INTO @KeyTable VALUES('mac & cheese,<test1>,"test2"');
SELECT LTRIM(RTRIM(m.n.value('.[1]','varchar(8000)'))) AS Keyword
FROM
(
SELECT CAST('<XMLRoot><RowData>' + REPLACE((SELECT Keyword AS [*] FOR XML PATH('')),',','</RowData><RowData>') + '</RowData></XMLRoot>' AS XML) AS x
FROM @KeyTable
)t
CROSS APPLY x.nodes('/XMLRoot/RowData')m(n);
这将隐式替换所有禁用字符...
This will replace all forbidden characters implicitly...
结果
mac & cheese
<test1>
"test2"
这篇关于预期 XML 解析分号 - 仅在替换 & 时与 &amp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:预期 XML 解析分号 - 仅在替换 & 时与 &


- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 更改自动增量起始编号? 2021-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- SQL 临时表问题 2022-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01