problem adding CDATA using PHP
Posted: Wed Sep 24, 2008 10:07 am
Hi everyone,
Posted this over in miscellaneous and didn't get any replies, and thinking about it it probably belongs here anyway (although please feel free to tell me otherwise!):
I'm having a few problems with using PHP to handle XML data. Basically, I'm getting data from a database, wrapping this in an XML document then performing an XSL transformation on it. This is all fine, until I want to include some HTML code snippets. I'm doing this using createCDATASection, but this seems to produce "<!--[CDATA[...]]-->" rather than "<![CDATA[...]]>", meaning that the XSL processor doesn't "see" the section at all.
Here's some example code to explain better:
produces "<div></div>", whereas transforming on data.xml containing gives "<div><b>This</b> is an HTML <br /> snippet</div>" as expected
view.xsl:
Any ideas on how to get around this problem?
Thanks,
Gethin
Posted this over in miscellaneous and didn't get any replies, and thinking about it it probably belongs here anyway (although please feel free to tell me otherwise!):
I'm having a few problems with using PHP to handle XML data. Basically, I'm getting data from a database, wrapping this in an XML document then performing an XSL transformation on it. This is all fine, until I want to include some HTML code snippets. I'm doing this using createCDATASection, but this seems to produce "<!--[CDATA[...]]-->" rather than "<![CDATA[...]]>", meaning that the XSL processor doesn't "see" the section at all.
Here's some example code to explain better:
Code: Select all
$xmlDoc = new DOMDocument("1.0","ISO-8859-1");
$datasection = $xmlDoc->createElement("data");
$datasection = $xmlDoc->appendChild($datasection);
$data = $xmlDoc->createCDATASection("<b>This</b> is an HTML <br /> snippet");
$data = $datasection->appendChild($data);
//print $xmlDoc->saveXML(); produces "<data><!--[CDATA[<b>This</b> is an HTML <br /> snippet]]--></data>"
$xslDoc = newDOMDocument();
$xslDoc->load("view.xsl");
$xslProc = new XsltProcessor();
$xslProc->importStylesheet($xslDoc);
$output = $xslProc->transformToDoc($xmlDoc);
print $output->saveXML();
Code: Select all
<data><![CDATA[<b>This</b> is an HTML <br /> snippet]]></data>view.xsl:
Code: Select all
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<div>
<xsl:value-of select="data"/>
</div>
</xsl:template>
</xsl:stylesheet>
Thanks,
Gethin