title: XSLT - Removing CDATA without disabling output escaping
body: <p>Having a bit of an issue with CDATA. For my current project, I had to convert a number of HTML elements into CDATA in order to be imported to a test case management system via XML. I am now tasked with exporting that data and translating it into DITA. The following is an excerpt from the data I am working with that I think provides enough context:</p>
  
  
  
  <pre><code>&lt;tr:testopia&gt;&lt;tr:testplan&gt;&lt;tr:testcase&gt;
      &lt;tr:text version="1"&gt;
          &lt;tr:author&gt;bugzilla&lt;/tr:author&gt;
          &lt;tr:action&gt;&lt;![CDATA[&lt;ol xmlns="http://www.w3.org/1999/xhtml"&gt;
                &lt;li&gt;
                   &lt;p xmlns:xh="http://www.w3.org/1999/xhtml" 
                      xmlns:cln="http://clean/"     class="Table"&gt;Refer to 
                      &lt;span style="color:red"&gt;CHM795_Workflow_DITA_Test_plan.doc
                      &lt;/span&gt;
                   &lt;/p&gt;
                &lt;/li&gt;
             &lt;/ol&gt;]]&gt;&lt;/tr:action&gt;
          &lt;tr:expected_result&gt;&lt;![CDATA[&lt;ol xmlns="http://www.w3.org/1999/xhtml"&gt;
                &lt;li&gt;
                   &lt;p xmlns:xh="http://www.w3.org/1999/xhtml" 
                      xmlns:cln="http://clean/"     
                      class="Table"&gt;All requirements are met &amp; example example&lt;/p&gt;
                &lt;/li&gt;
             &lt;/ol&gt;]]&gt;&lt;/tr:expected_result&gt;
          &lt;tr:setup&gt;&lt;![CDATA[]]&gt;&lt;/tr:setup&gt;
          &lt;tr:breakdown&gt;&lt;![CDATA[]]&gt;&lt;/tr:breakdown&gt;
       &lt;/tr:text&gt;
  &lt;/tr:testcase&gt;&lt;/tr:testplan&gt;&lt;/tr:testopia&gt;
  </code></pre>
  
  <p>To do this, I need to get the elements previously kept in CDATA out again, and I've found from a small amount of research that a line such as the following will achieve this:</p>
  
  <pre><code>&lt;xsl:template match="tr:text"&gt;
          &lt;prerequisites&gt;
              &lt;xsl:apply-templates select="tr:setup"/&gt;
          &lt;/prerequisites&gt;
          &lt;postrequisites&gt;
              &lt;xsl:apply-templates select="tr:breakdown"/&gt;
          &lt;/postrequisites&gt;
      &lt;process&gt;
          &lt;actions&gt;
              &lt;xsl:apply-templates select="tr:action"/&gt;
          &lt;/actions&gt;
          &lt;results&gt;
              &lt;xsl:apply-templates select="tr:expected_result"/&gt;
          &lt;/results&gt;
      &lt;/process&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="text()"&gt;
      &lt;xsl:value-of select="normalize-space(.)" disable-output-escaping="yes"/&gt;
  &lt;/xsl:template&gt;
  </code></pre>
  
  <p>This will provide me with the following:</p>
  
  <pre><code>&lt;case&gt;
    &lt;prerequisites/&gt;
    &lt;postrequisites/&gt;
    &lt;process&gt;
       &lt;actions&gt;&lt;ol xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;li&gt; &lt;p xmlns:xh="http://www.w3.org/1999/xhtml" xmlns:cln="http://clean/" class="Table"&gt;Perform &lt;span style="color:red"&gt;ASM Test Document.docx&lt;/span&gt; &lt;/p&gt; &lt;/li&gt; &lt;/ol&gt;&lt;/actions&gt;
       &lt;results&gt;&lt;ol xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;li&gt; &lt;p xmlns:xh="http://www.w3.org/1999/xhtml" xmlns:cln="http://clean/" class="Table"&gt;Test plan successfully completed&lt;/p&gt; &lt;/li&gt; &lt;/ol&gt;&lt;/results&gt;
    &lt;/process&gt;
  </code></pre>
  
  <p>
  The issue I currently face is this: disabling output escaping gets me the format I need, translation has worked fine and the DITA file validates. However, in some of these test cases, the content of the HTML elements sometimes contain characters such as <code>&amp;</code>. When I try to translate these cases into their final form after this stage, I receive an error like the following when running with SAXON:</p>
  
  <pre><code>Error reported by XML parser: The entity name must immediately follow 
  the '&amp;' in the entity reference.
  </code></pre>
  
  <p>Is there a way I can keep the internal text of the nodes in their current "harmless" state and still wrap it in the nodes also held in CDATA? Could probably phrase that better, but the words aren't coming to mind.</p>