174

非常简单的问题 - 我有一个属性,我想在其中添加双引号。如何转义它们?我试过了

  • \"
  • “”
  • \\"

我已经为所有这些都设置了 xml 类型和 varchar(max) 的 @xml 变量。

 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
4

4 回答 4

273

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

"hi &quot;mom&quot; lol" 

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

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)')
于 2009-03-16T15:10:12.037 回答
4

tSql 用另一个双引号转义一个双引号。因此,如果您希望它成为您的 sql 字符串文字的一部分,您可以这样做:

declare @xml xml 
set @xml = "<transaction><item value=""hi"" /></transaction>"

如果要在 xml 本身的中包含引号,请使用实体,如下所示:

declare @xml xml
set @xml = "<transaction><item value=""hi &quot;mom&quot; lol"" /></transaction>"
于 2009-03-16T15:14:37.363 回答
4

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

于 2011-09-14T19:52:41.497 回答
2

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

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

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

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

如果内部允许使用双引号,它将是这样的:

&lt;core:when test="${ name == "Toy's R Us" }"&gt; 
于 2010-11-18T19:54:48.687 回答