PEAR is discontinued

This is a static archive preserving historical releases and metadata. No new packages or updates will be published. For updates to existing packages, or to publish new packages, please use Composer and Packagist.

Home » XML » XML_Util » Bug #4950

Incorrect CDATA serializing

Details

Submitted2005-07-30 00:09 UTC
Fromja dot doma at gmail dot com
Assignedashnazg
StatusClosed
PackageXML_Util
PHP Version4.3.11
Roadmaps1.2.0a1

Comments

[2005-07-30 00:09 UTC] ja dot doma at gmail dot com

Description:
------------
This code:

require_once "XML/Util.php";

// creating an XML tag
$tag = XML_Util::createTag("test", array(), "Content ]]></test> here!",null, XML_UTIL_CDATA_SECTION);

Will result in:

<test><![CDATA[Content ]]></test> here!]]></test>

As you can see it is wrong bechavior. I'm not sure, but the only one solution is to create two CDATA sections there:

<test><![CDATA[Content ]]>]]><![CDATA[</test> here!]]></test>

Note: the ]]> sequence is outside CDATA sections

Normal(DOM) parser may merge sequence of CDATA and text into one DomNode.text field.

Sorry for my badly english =)

Test script:
---------------
require_once "XML/Util.php";

// creating an XML tag
$tag = XML_Util::createTag("test", array(), "Content ]]></test> here!",null, XML_UTIL_CDATA_SECTION);
echo $tag;

Expected result:
----------------
<test><![CDATA[Content ]]>]]><![CDATA[</test> here!]]></test>

Actual result:
--------------
<test><![CDATA[Content ]]></test> here!]]></test>

[2005-07-30 00:20 UTC] ja dot doma at gmail dot com

The solution might be(line 642):

function createCDataSection($data) {
return sprintf("<![CDATA[%s]]>", preg_replace('/\]\]>/', "]]>]]><![CDATA[", strval($data)));
}

[2005-07-30 00:34 UTC] ja dot doma at gmail dot com

Oops! Since XML can not contain > symbol it must be escaped with >

So the proper fix to bug is:

function createCDataSection($data)
{
return sprintf("<![CDATA[%s]]>", preg_replace('/\]\]>/', "]]>]]><![CDATA[", strval($data)));
}