I need to store content in an xml database. some data in the database looks like this:
<item>
<span class ="person">Henry 8<sup>th</sup></span>
</item>
<item>
<span class="company">Berkley & Jensen</span>
</item>
I need to load the data into a dom object with loadXML()
then pass it to a xsl stylesheet where it is further manipulated using xpath and css. When I load the data the code breaks because of the '&' and I do not want to convert all entities because I need to use css on <sup>
and the xpath on the 'class' and I suspect that encoded entities will cause them to fail. How should I store and retrieve the illegal characters?
Because of the comments I am providing a sample php script. If you add the php tags it should run. Thank you for the CDATA suggestion. I have used it to demonstrate the problem. If I try to use the 'block' tag as a target for the XPATH it works fine but if I try to use the 'span' tag it prints nothing.
$xsl = <<<XSL
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template name="doContent" match="/">
<div class="story">
<xsl:for-each select="//body/block"> <xsl:copy-of select="." />
</xsl:for-each>
</div>
</xsl:template>
</xsl:stylesheet>
XSL;
$xml = <<<XML
<?xml version="1.0" encoding="utf-8"?>
<content id="test" >
<headline>test</headline>
<author>test</author>
<body>
<block id="1"><![CDATA[<span class="normal"><p>1</p></span>]]></block>
<block id="2"><![CDATA[<span class=""><p>2</p></span>]]></block>
<block id="3"><![CDATA[<span class ="person">Henry 8<sup>th</sup></span>]]></block>
<block id="4"><![CDATA[<span class="company">Berkley & Jensen</span>]]></block>
<block id="5"><![CDATA[<span class=""><p>5</p></span>]]></block>
<block id="6"><![CDATA[<span class=""><p>6</p></span>]]></block>
</body>
</content>
XML;
$xslDoc = new DOMDocument();
$xslDoc->loadXML($xsl);
$xmlDoc = new DOMDocument();
$xmlDoc->loadXML($xml);
$proc = new XSLTProcessor();
$proc->importStylesheet($xslDoc);
echo $proc->transformToXML($xmlDoc);