CDATA not included in my XML when using xsl

Viewed 120

I am currently working on a small project to export data from MS Access into an XML format via VBA. I have a section where I am supposed to add code with the CDATA tag.
However, when I try to implement it, the CDATA part is missing in my code. This is what I've got so far:

 Dim doc As New MSXML2.DOMDocument60
 Dim rulescript As IXMLDOMElement
 Dim code As IXMLDOMElement
 Dim cdata As IXMLDOMCDATASection

 'Append ruleScript 
 Set rulescript = doc.createElement("ruleScript")
 doc.appendChild rulescript

 'Append code
 Set code = doc.createElement("code")
 rulescript.appendChild code
        
 'Create code and append it as CDATA section
 Set cdata = doc.createCDATASection("code")
 cdata.Data = "this is a dummy code."
 code.appendChild cdata

XLS:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"
cdata-section-elements="code" encoding="UTF-8"/>
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

And this is how it's supposed to look like:

<ruleScript>
     <code><![CDATA[this is a dummy code.]]></code>
</ruleScript>

But unfortunately, it turned out like this, without the CDATA:

<ruleScript>
     <code>this is a dummy code.</code>
</ruleScript>

I looked around a lot and could not find my solution, so I would appreciate any kinds of help.

EDIT: After looking for a while, I realized that it wasn't the implementation of the code that was the problem. The problem is the xsl I used to save the document:

For some reason, if I only use

Debug.Print doc.XML

,it works just fine. I haven't figured out why exactly that is the case.

1 Answers

You need to add a cdata-section-elements attribute that controls XML elements value when there is a need for a CDATA section. It is a space separated list, so you can add as many as needed element names.

Check it out.

<xsl:output method="xml" indent="yes" cdata-section-elements="code" encoding="utf-8" />
Related