title: How to disable text escape on xsl:copy-of
body: <p>I build a mechanism that takes all the <code>&lt;script /&gt;</code> tags and put them at the end of the page.</p>
  <p>It works good except that Ampersant <code>&amp;</code> characters are encode to <code>&amp;amp;</code> even those in JavaScript code which is not what I want.</p>
  <p>How can I solve this?</p>
  <p>XML</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;root&gt;&lt;/root&gt;
  </code></pre>
  <p>XSLT</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  
  &lt;xsl:stylesheet version=&quot;2.0&quot; xmlns=&quot;http://www.w3.org/1999/xhtml&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;&gt;
      &lt;xsl:output method=&quot;html&quot; /&gt;
      
      &lt;xsl:template match=&quot;*&quot;&gt;
          &lt;xsl:variable name=&quot;body&quot;&gt;
              &lt;xsl:apply-templates select=&quot;.&quot; mode=&quot;body&quot;&gt;&lt;/xsl:apply-templates&gt;
          &lt;/xsl:variable&gt;
          
          &lt;xsl:apply-templates select=&quot;$body&quot; mode=&quot;no-script&quot; /&gt;
          
          &lt;xsl:copy-of select=&quot;$body//script&quot; xpath-default-namespace=&quot;http://www.w3.org/1999/xhtml&quot; /&gt;
      &lt;/xsl:template&gt;
      
      &lt;xsl:template match=&quot;script&quot; mode=&quot;no-script&quot;&gt;
          
      &lt;/xsl:template&gt;
      
      &lt;xsl:template match=&quot;*[not(self::script)] | @* |comment()&quot; mode=&quot;no-script&quot;&gt;
          &lt;xsl:if test=&quot;name() != 'script'&quot;&gt;
              &lt;xsl:copy xpath-default-namespace=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
                  &lt;xsl:apply-templates select=&quot;node()[not(self::script)] | @*&quot; mode=&quot;no-script&quot; /&gt;
              &lt;/xsl:copy&gt;
          &lt;/xsl:if&gt;
      &lt;/xsl:template&gt;
  
      &lt;xsl:template match=&quot;*&quot; mode=&quot;body&quot;&gt;
          &lt;script type=&quot;text/javascript&quot;&gt;
              // Ampersand &lt;xsl:text disable-output-escaping=&quot;yes&quot;&gt;&lt;![CDATA[&amp;]]&gt;&lt;/xsl:text&gt;
              var a = 'a';
          &lt;/script&gt;
          &lt;div&gt;Hello World&lt;/div&gt;
      &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  
  </code></pre>
  <p>It outputs:</p>
  <pre><code>&lt;div xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;Hello World&lt;/div&gt;
  &lt;script xmlns=&quot;http://www.w3.org/1999/xhtml&quot; type=&quot;text/javascript&quot;&gt;
              // Ampersand &amp;amp;
              var a = 'a';
          &lt;/script&gt;
  </code></pre>
  <p>I tried and it works but I wunder if there is a way to keep the <code>&lt;script&gt;</code> tags inside the variable <code>$body</code>.</p>
  <pre><code>&lt;script type=&quot;text/javascript&quot;&gt;
    &lt;xsl:value-of select=&quot;$body//script&quot; xpath-default-namespace=&quot;http://www.w3.org/1999/xhtml&quot; disable-output-escaping='yes'/&gt;
  &lt;/script&gt;
  </code></pre>