如何在 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 12:24:13  来源:igfitidea点击:

How do I escape double quotes in attributes in an XML String in T-SQL?

xmltsqlescaping

提问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 &quot;in xml? i.e.

那不是&quot;在 xml 中吗?IE

"hi &quot;mom&quot; lol" 

**edit: ** tested; works fine:

**编辑:**测试;工作正常:

declare @xml xml

 set @xml = '<transaction><item value="hi &quot;mom&quot; 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 &quot;works very well for the xml config files when forming regex expressions for RegexTransformer in Solr like so: regex=".*img src=&quot;(.*)&quot;.*"using the escaped version instead of double-quotes.

不能再发表评论了,但投票了,想让人们知道&quot;在 Solr 中为 RegexTransformer 形成正则表达式时,xml 配置文件非常有效,如下所示:regex=".*img src=&quot;(.*)&quot;.*"使用转义版本而不是双引号。

回答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 &quot;mom&quot; lol"" /></transaction>"

回答by Mark

In Jelly.core to test a literal string one would use:

在 Jelly.core 中测试一个文字字符串会使用:

&lt;core:when test="${ name == 'ABC' }"&gt; 

But if I have to check for string "Toy's R Us":

但是,如果我必须检查字符串“Toy's R Us”:

&lt;core:when test="${ name == &amp;quot;Toy&apos;s R Us&amp;quot; }"&gt;

It would be like this, if the double quotes were allowed inside:

如果里面允许双引号,那就是这样:

&lt;core:when test="${ name == "Toy's R Us" }"&gt;