title: XSLT transformToXml runtime errors
body: <p>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 <code>index.php</code> script loads the XSL template, builds the XML and captures it within the output buffer. The XML is then passed to the <code>XSLTProcessor</code> and where the parameters are set and the HTML is printed.</p>
  
  <pre><code>$xsl = new DomDocument();
  $xsl-&gt;load('xsl/homepage.xsl');
  ...
  ob_start();
  // echo XML here
  $xml = ob_get_contents();
  ob_end_clean();
  $dom = new DomDocument();
  $dom-&gt;loadXML($xml);
  $dom-&gt;xinclude();
  $xslt = new XSLTProcessor();
  $xslt-&gt;importStylesheet($xsl);
  $xslt-&gt;setParameter(null, $params);
  echo $xslt-&gt;transformToXml($dom);
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>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
  ...
  </code></pre>
  
  <p>and the HTML generation terminates there. Now, the PHP documentation (<a href="http://php.net/manual/en/xsltprocessor.transformtoxml.php" rel="nofollow noreferrer">http://php.net/manual/en/xsltprocessor.transformtoxml.php</a>) for <code>transformToXml</code> states that this function should return FALSE on error. However, instead it returns the partially transformed HTML.</p>
  
  <p>On inspection I find that there are a number of variables defined in the XSLT template that are not present in <code>$params</code>, for example:</p>
  
  <pre><code>156:    &lt;xsl:if test="$image_source!=''"&gt;
  157:        &lt;a class="slideShow"&gt;
  158:            &lt;xsl:attribute name="href"&gt;&lt;xsl:value-of select="/root/homepage/attributes/featured_link" /&gt;&lt;/xsl:attribute&gt;
  159:            &lt;xsl:for-each select="/root/gallery/files/file"&gt;
  160:                &lt;img class="centralColumnImage noDisplay" src="./image.php?w=460&amp;amp;keepAspect=y&amp;amp;img={./source}"&gt;
  161:                    &lt;xsl:if test="position() ='1'"&gt;
  162:                      &lt;xsl:attribute name="class"&gt;centralColumnImage&lt;/xsl:attribute&gt;
  163:                    &lt;/xsl:if&gt;
  164:                &lt;/img&gt;
  165:            &lt;/xsl:for-each&gt;
  166:        &lt;/a&gt;
  167:    &lt;/xsl:if&gt;
  </code></pre>
  
  <p>The answer to <a href="https://stackoverflow.com/questions/1299808/in-xslt-how-do-you-test-to-see-if-a-variable-exists">In XSLT how do you test to see if a variable exists?</a> 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 <code>$params</code> variable on the PHP 5.2 server and their contents are identical: that is, both do not have <code>image_source</code> nor any other variables defined that are referenced in the XSL file.</p>
  
  <p>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.</p>