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

后端 未结 4 562
予麋鹿
予麋鹿 2020-12-23 13:17

Pretty simple question - I have an attribute that I would like to have double quotes in. How do I escape them? I\'ve tried

  • \\\"
  • \"\"
  • \\\\\"
相关标签:
4条回答
  • 2020-12-23 13:17

    Wouldn't that be " in xml? i.e.

    "hi "mom" 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)')
    
    0 讨论(0)
  • 2020-12-23 13:20

    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:

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

    If you want to include a quote inside a value in the xml itself, you use an entity, which would look like this:

    declare @xml xml
    set @xml = "<transaction><item value=""hi &quot;mom&quot; lol"" /></transaction>"
    
    0 讨论(0)
  • 2020-12-23 13:32

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

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

    But if I have to check for string "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; 
    
    0 讨论(0)
  • 2020-12-23 13:40

    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.

    0 讨论(0)
提交回复
热议问题