¿Cómo puedo escapar de las comillas dobles en atributos en una cadena XML en T-SQL?


Pregunta bastante simple: Tengo un atributo en el que me gustaría tener comillas dobles. ¿Cómo puedo escapar de ellos? He intentado

  • \"
  • ""
  • \\"

Y he hecho la variable @xml tanto xml type como varchar(max) para todos ellos.

 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
Author: Tom Ritter, 2009-03-16

4 answers

¿No sería &quot; en xml? es decir,

"hi &quot;mom&quot; lol" 

* * editar: * * probado; funciona bien:

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)')
 269
Author: Marc Gravell,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-03-16 15:10:12

TSql escapa una comilla doble con otra comilla doble. Así que si quieres que sea parte de tu literal de cadena sql harías esto:

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

Si desea incluir una cita dentro de un valor en el propio xml, utilice una entidad, que se vería así:

declare @xml xml
set @xml = "<transaction><item value=""hi &quot;mom&quot; lol"" /></transaction>"
 4
Author: Joel Coehoorn,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-03-16 15:14:37

Ya no puede hacer comentarios, pero votó y quería que la gente supiera que &quot; funciona muy bien para los archivos de configuración xml cuando se forman expresiones regex para RegexTransformer en Solr de la siguiente manera: regex=".*img src=&quot;(.*)&quot;.*" usando la versión escapada en lugar de comillas dobles.

 4
Author: pulkitsinghal,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-09-14 19:52:41

En gelatina.core para probar una cadena literal se usaría:

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

Pero si tengo que comprobar la cadena"Toy's R Us":

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

Sería así, si se permitieran las comillas dobles dentro:

&lt;core:when test="${ name == "Toy's R Us" }"&gt; 
 2
Author: Mark,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-09-23 13:04:13