title: Xslt produce some <name> tags with CDATA and some <name> tags without?
body: <p>Summary: I'm using xslt to convert data, and need to produce some  tags with CDATA inside and some  tags without. Is escaping the CDATA sections my only option?</p>
  
  <p>I'm attempting to convert data I already have in xml to <a href="http://docs.moodle.org/en/Moodle_XML" rel="nofollow noreferrer">Moodle Xml</a> for importing. The final product needs to include some Html, which the <a href="http://docs.moodle.org/en/Moodle_XML" rel="nofollow noreferrer">Moodle Xml doc</a> specifically says needs to be contained in CDATA.</p>
  
  <p><strong>Desired Output:</strong></p>
  
  <pre><code>&lt;question&gt;
    &lt;name&gt;
      &lt;text&gt;FooName&lt;/text&gt;
    &lt;/name&gt;
    &lt;questiontext format="html"&gt;
      &lt;text&gt;&lt;![CDATA[&lt;img src="1.png"&gt;]]&gt;&lt;/text&gt;
    &lt;/questiontext&gt;
  &lt;/question&gt;
  </code></pre>
  
  <p>I gave this a try using the following code (trimmed down, but will include the data from my input xml file):</p>
  
  <p><strong>Method 1, nothing special</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
    &lt;xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/&gt;
  
    &lt;xsl:template match="/"&gt;
      &lt;question&gt;
        &lt;name&gt;
          &lt;text&gt;FooName&lt;/text&gt;
        &lt;/name&gt;
        &lt;questiontext format="html"&gt;
          &lt;text&gt;&lt;![CDATA[&lt;img src="1.png"&gt;]]&gt;&lt;/text&gt;
        &lt;/questiontext&gt;
     &lt;/xsl:template&gt;
  
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>And got...</p>
  
  <p><strong>Bad Output from Method 1</strong></p>
  
  <pre><code>&lt;question&gt;
    &lt;name&gt;
      &lt;text&gt;FooName&lt;/text&gt;
    &lt;/name&gt;
    &lt;questiontext format="html"&gt;
      &lt;text&gt;&amp;lt;img src="1.png"&amp;gt;&lt;/text&gt;
    &lt;/questiontext&gt;
  &lt;/question&gt;
  </code></pre>
  
  <p>So I look up the <a href="http://www.w3.org/TR/xslt#section-XML-Output-Method" rel="nofollow noreferrer">xslt documentation</a> and <a href="https://stackoverflow.com/questions/2265334/how-to-access-variable-in-cdata-from-xslt">some SO questions</a>, which seem to say I have 2 options:</p>
  
  <ol>
  <li>Do nothing, CDATA gets escaped.</li>
  <li>use <code>cdata-section-elements ="text"</code> to auto-generate cdata sections inside  tags</li>
  <li>Generate CDATA sections by hand, using <code>disable-output-escaping="yes"</code></li>
  </ol>
  
  <p>Ok, autogeneration sounds good. Lets try that:</p>
  
  <p><strong>Method 2 adding <code>cdata-section-elements="text"</code></strong></p>
  
  <pre><code>&lt;xsl:output method="xml" indent="yes" omit-xml-declaration="yes" cdata-section-elements="text"/&gt;
  </code></pre>
  
  <p><strong>Bad Output from <code>cdata-section-elements ="text"</code></strong>:</p>
  
  <pre><code>&lt;question&gt;
    &lt;name&gt;
      &lt;text&gt;&lt;![CDATA[FooName]]&gt;&lt;/text&gt;
    &lt;/name&gt;
    &lt;questiontext format="html"&gt;
      &lt;text&gt;&lt;![CDATA[&lt;img src="1.png"&gt;]]&gt;&lt;/text&gt;
    &lt;/questiontext&gt;
  &lt;/question&gt;
  </code></pre>
  
  <p>So 2 isn't an option because there are other  elements I do NOT want containing CDATA, in a schema I don't control. </p>
  
  <p>This leaves me with option 3, escaping it by hand. My question then is: Is option 3 my only option? Is there anything else I can do to get my desired output using XSLT? not using XSLT?</p>