如何在 T-SQL 中的 XML 字符串的属性中转义双引号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/650821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I escape double quotes in attributes in an XML String in T-SQL?
提问by Tom Ritter
Pretty simple question - I have an attribute that I would like to have double quotes in. How do I escape them? I've tried
非常简单的问题 - 我有一个属性,我想在其中添加双引号。我该如何转义它们?我试过了
- \"
- ""
- \\"
- \"
- “”
- \\"
And I've made the @xml variable both xml type and varchar(max) for all of them.
我已经为所有这些变量设置了 xml 类型和 varchar(max) 变量。
declare @xml xml --(or varchar(max) tried both)
set @xml = '<transaction><item value="hi "mom" lol"
ItemId="106" ItemType="2" instanceId="215923801" dataSetId="1" /></transaction>'
declare @xh int
exec sp_xml_preparedocument @xh OUTPUT, @xml
insert into @commits --I declare the table, just removed it for brevity
select
x.*
from openxml(@xh,'/transaction/item')
WITH (
dataItemId int,
dataItemType int,
instanceId int,
dataSetId int,
value varchar(max)
) x
回答by Marc Gravell
Wouldn't that be "in xml? i.e.
那不是"在 xml 中吗?IE
"hi "mom" lol"
**edit: ** tested; works fine:
**编辑:**测试;工作正常:
declare @xml xml
set @xml = '<transaction><item value="hi "mom" lol"
ItemId="106" ItemType="2" instanceId="215923801" dataSetId="1" /></transaction>'
select @xml.value('(//item/@value)[1]','varchar(50)')
回答by pulkitsinghal
Cannot comment anymore but voted it up and wanted to let folks know that "works very well for the xml config files when forming regex expressions for RegexTransformer in Solr like so: regex=".*img src="(.*)".*"using the escaped version instead of double-quotes.
不能再发表评论了,但投票了,想让人们知道"在 Solr 中为 RegexTransformer 形成正则表达式时,xml 配置文件非常有效,如下所示:regex=".*img src="(.*)".*"使用转义版本而不是双引号。
回答by Joel Coehoorn
tSql escapes a double quote with another double quote. So if you wanted it to be part of your sql string literal you would do this:
tSql 用另一个双引号转义双引号。因此,如果您希望它成为 sql 字符串文字的一部分,您可以这样做:
declare @xml xml
set @xml = "<transaction><item value=""hi"" /></transaction>"
If you want to include a quote inside a valuein the xml itself, you use an entity, which would look like this:
如果您想在 xml 本身的值中包含引号,您可以使用一个实体,它看起来像这样:
declare @xml xml
set @xml = "<transaction><item value=""hi "mom" lol"" /></transaction>"
回答by Mark
In Jelly.core to test a literal string one would use:
在 Jelly.core 中测试一个文字字符串会使用:
<core:when test="${ name == 'ABC' }">
But if I have to check for string "Toy's R Us":
但是,如果我必须检查字符串“Toy's R Us”:
<core:when test="${ name == &quot;Toy's R Us&quot; }">
It would be like this, if the double quotes were allowed inside:
如果里面允许双引号,那就是这样:
<core:when test="${ name == "Toy's R Us" }">

