1

I need to wrap some descriptions into <![CDATA[...]]>.

What I'm doing:

public function cdata() {
    $description = 'Den snabba bruna räven hoppade över den lata hunden';
    $channel = array();
    $channel['item'] = array(
        'g:description' => '<![CDATA['.$description.']]>',
    );
    $this->RequestHandler->renderAs($this, 'xml');
    $this->set('_rootNode', 'rss');
    $this->set('xmlns:g', 'http://base.google.com/ns/1.0');
    $this->set([
        'channel' => $channel,
    ]);
    $this->set('_serialize', ['xmlns:g','channel']);
}

What I'm getting:

<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:g="http://base.google.com/ns/1.0">
  <channel>
    <item>
      <g:description>&lt;![CDATA[Den snabba bruna räven hoppade över den lata hunden]]&gt;</g:description>
    </item>
  </channel>
</rss>

What I want:

<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:g="http://base.google.com/ns/1.0">
  <channel>
    <item>
      <g:description><![CDATA[Den snabba bruna räven hoppade över den lata hunden]]></g:description>
    </item>
  </channel>
</rss>

Problem:

The CDATA gets htmlspecialchars'd, with < and > converted to &lt; and &gt;. Here's an example of what I need (source), see the Second example demonstrates the use of CDATA section.

I googled all kinds of combinations of cakephp 3, xml and cdata, but found nothing, particularly, checked the documentation and here. Please help!

ᴍᴇʜᴏᴠ
  • 4,804
  • 4
  • 44
  • 57

1 Answers1

1

It's not (yet) supported, internally the XML view uses Xml::fromArray() for serialization, which uses DOMText nodes for all non-numeric content.

For now, if you need CDATA sections, you'll have to ditch using serializing, and build the XML yourself (in a view template).

ndm
  • 59,784
  • 9
  • 71
  • 110
  • Thanks for clarifying. I'm guessing the problem is caused by some wild nordic UTF-8 chars, and I was trying to deal with them using `CDATA`, is there any way to do [this](https://stackoverflow.com/a/12265956/722036) instead? – ᴍᴇʜᴏᴠ Aug 09 '18 at 20:45
  • Well, you can apply it on controller level to the data where needed, alternatively you would have to extend the `XmlView` and iterate over all serializable view variables to apply it to everything. If this is really the problem, and it's a general problem actually, then maybe you should report it as an issue, so that it can be intergrated into the core. Adding CDATA support should also be relatively simple. – ndm Aug 09 '18 at 22:44
  • @aexl Here's a quick and dirty patch for CDATA support if you're interested: **https://gist.github.com/ndm2/ba4fdd444eb9753e23e4bbff53eccf88** – ndm Aug 09 '18 at 22:58
  • Thank you so much, I'm on it. Managed to find the problematic character: meet `–`, which looks like a regular dash, but becomes a question mark once pasted into Coda 2. Unsure how do I get more info about it, if needed. – ᴍᴇʜᴏᴠ Aug 10 '18 at 11:31
  • I confirm applying [this function](https://stackoverflow.com/a/12265956/722036) to the input string fixes the problem. I will create an issue now. – ᴍᴇʜᴏᴠ Aug 10 '18 at 11:43