title: XSLT replace double quotes in variable
body: <p>Just to clarify, I am using XSLT 1.0. Sorry for not specifying that at first.</p>
  
  <p>I have an XSLT stylesheet where I'd like to replace double quotes with something safe that's safe to go into a JSON string. I'm trying to do something like the following:</p>
  
  <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
    &lt;xsl:output method="text" omit-xml-declaration="yes" /&gt;
    &lt;xsl:strip-space elements="*" /&gt;
  
    &lt;xsl:template match="/message"&gt;
      &lt;xsl:variable name="body"&gt;&lt;xsl:value-of select="body"/&gt;&lt;/xsl:variable&gt;
  
  
      {
        "message" : 
        {
          "body":  "&lt;xsl:value-of select="normalize-space($body)"/&gt;"
        }
      }
    &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>If I have XML passed in that looks like the following, this will always work just fine:</p>
  
  <pre><code>&lt;message&gt;
   &lt;body&gt;This is a normal string that will not give you any issues&lt;/body&gt;
  &lt;/message&gt;
  </code></pre>
  
  <p>However, I'm dealing with a body that has full blown HTML in it, which isn't an issue because <code>normalize-space()</code> will take care of the HTML, but not double quotes. This breaks me:</p>
  
  <pre><code>&lt;message&gt;
  &lt;body&gt;And so he quoted: "I will break him". The end.&lt;/body&gt;
  &lt;/message&gt;
  </code></pre>
  
  <p>I really don't care if the double quotes are HTML escaped or prefixed with a backslash. I just need to make sure the end result passes a JSON parser.</p>
  
  <p>This output passes JSON Lint and would be an appropriate solution (backslashing the quotes):</p>
  
  <pre><code>{ "body" : "And so he quoted: \"I will break him\". The end." } 
  </code></pre>