2

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.

Mimi
  • 97
  • 9
  • rootNode is just another node I had created before. I did not realize it may cause confusion. I will edit it – Mimi Dec 01 '21 at 08:51
  • That works for me: `Debug.Print doc.XML` gives `<![CDATA[this is a dummy code.]]>` – Tim Williams Dec 01 '21 at 08:55
  • Same as Tim, I tried `doc.Save` and the resulting file is correct too. Do you get the same problem if you just use this block of code? – Raymond Wu Dec 01 '21 at 08:56
  • Oh, it works for me too if I only use the blockcode. I guess it must be something else that isn't working. – Mimi Dec 01 '21 at 09:20
  • I found the problem to why CDATA isn't showing and edited the question. – Mimi Dec 01 '21 at 11:05
  • 1
    I'm not familiar with XML (in fact, I hardly use them!) So a quick google gives [this](https://stackoverflow.com/questions/18700152/how-to-add-cdata-to-an-xml-file-using-xsl). You probably want to update your question title to better reflect your current problem. @Mimi – Raymond Wu Dec 01 '21 at 13:56

1 Answers1

2

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" />
Yitzhak Khabinsky
  • 18,471
  • 2
  • 15
  • 21