I need to move a PHP site I inherited that uses XSLT from a server that runs PHP 5.2 to PHP 5.4. On this site the index.php
script loads the XSL template, builds the XML and captures it within the output buffer. The XML is then passed to the XSLTProcessor
and where the parameters are set and the HTML is printed.
$xsl = new DomDocument();
$xsl->load('xsl/homepage.xsl');
...
ob_start();
// echo XML here
$xml = ob_get_contents();
ob_end_clean();
$dom = new DomDocument();
$dom->loadXML($xml);
$dom->xinclude();
$xslt = new XSLTProcessor();
$xslt->importStylesheet($xsl);
$xslt->setParameter(null, $params);
echo $xslt->transformToXml($dom);
The problem is that on the server that runs PHP 5.4 I get a number of errors for undefined variables in the XSLT template:
Warning: XSLTProcessor::transformToXml(): runtime error: file homepage.xsl line 156 element if in index.php on line 44
Warning: XSLTProcessor::transformToXml(): Variable 'image_source' has not been declared. in index.php on line 44
...
and the HTML generation terminates there. Now, the PHP documentation (http://php.net/manual/en/xsltprocessor.transformtoxml.php) for transformToXml
states that this function should return FALSE on error. However, instead it returns the partially transformed HTML.
On inspection I find that there are a number of variables defined in the XSLT template that are not present in $params
, for example:
156: <xsl:if test="$image_source!=''">
157: <a class="slideShow">
158: <xsl:attribute name="href"><xsl:value-of select="/root/homepage/attributes/featured_link" /></xsl:attribute>
159: <xsl:for-each select="/root/gallery/files/file">
160: <img class="centralColumnImage noDisplay" src="./image.php?w=460&keepAspect=y&img={./source}">
161: <xsl:if test="position() ='1'">
162: <xsl:attribute name="class">centralColumnImage</xsl:attribute>
163: </xsl:if>
164: </img>
165: </xsl:for-each>
166: </a>
167: </xsl:if>
The answer to In XSLT how do you test to see if a variable exists? states that this shouldn't work. Yet on the server that runs PHP 5.2, this code appears to at least work on that server without issue. In addition, I have checked the $params
variable on the PHP 5.2 server and their contents are identical: that is, both do not have image_source
nor any other variables defined that are referenced in the XSL file.
Could this be specific to PHP 5.2? I've looked in the migration guides for PHP 5.2 and PHP 5.3, and for PHP 5.3 to PHP 5.4 - yet I couldn't find backwards incompatible modifications to DOMDocument nor XSLTProcessor.