<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Error handling with libxml error handling functions</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="xsl.examples-collection.html">Example collection.xml and collection.xsl files</a></div> <div class="next" style="text-align: right; float: right;"><a href="class.xsltprocessor.html">XSLTProcessor</a></div> <div class="up"><a href="xsl.examples.html">Examples</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="xsl.examples-errors" class="section"> <h2 class="title">Error handling with libxml error handling functions</h2> <p class="para"> libxml offers a number of functions for handling errors, which can be employed to capture and deal with errors in XSLT processing. </p> <p class="para"> <div class="example" id="example-6761"> <p><strong>Example #1 fruits.xml</strong></p> <div class="example-contents"><p>A valid XML file.</p></div> <div class="example-contents"> <div class="xmlcode"><pre class="xmlcode"><fruits> <fruit>Apple</fruit> <fruit>Banana</fruit> <fruit>Cherry</fruit> </fruits></pre> </div> </div> </div> <div class="example" id="example-6762"> <p><strong>Example #2 fruits.xsl</strong></p> <div class="example-contents"><p>Contains an invalid select expression.</p></div> <div class="example-contents"> <div class="xmlcode"><pre class="xmlcode"><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" encoding="utf-8" indent="no"/> <xsl:template match="fruits"> <ul> <xsl:apply-templates/> </ul> </xsl:template> <xsl:template match="fruit"> <li><xsl:value-of select="THIS IS A DELIBERATE ERROR!"/></li> </xsl:template> </xsl:stylesheet></pre> </div> </div> </div> <div class="example" id="example-6763"> <p><strong>Example #3 Collating and printing errors</strong></p> <div class="example-contents"><p> The example below captures and displays libxml errors raised when calling <span class="methodname"><a href="xsltprocessor.importstylesheet.html" class="methodname">XSLTProcessor::importStyleSheet()</a></span> with a stylesheet containing an error. </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br />$xmldoc </span><span style="color: #007700">= new </span><span style="color: #0000BB">DOMDocument</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$xsldoc </span><span style="color: #007700">= new </span><span style="color: #0000BB">DOMDocument</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$xsl </span><span style="color: #007700">= new </span><span style="color: #0000BB">XSLTProcessor</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">$xmldoc</span><span style="color: #007700">-></span><span style="color: #0000BB">loadXML</span><span style="color: #007700">(</span><span style="color: #DD0000">'fruits.xml'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$xsldoc</span><span style="color: #007700">-></span><span style="color: #0000BB">loadXML</span><span style="color: #007700">(</span><span style="color: #DD0000">'fruits.xsl'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">libxml_use_internal_errors</span><span style="color: #007700">(</span><span style="color: #0000BB">true</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$result </span><span style="color: #007700">= </span><span style="color: #0000BB">$xsl</span><span style="color: #007700">-></span><span style="color: #0000BB">importStyleSheet</span><span style="color: #007700">(</span><span style="color: #0000BB">$xsldoc</span><span style="color: #007700">);<br />if (!</span><span style="color: #0000BB">$result</span><span style="color: #007700">) {<br /> foreach (</span><span style="color: #0000BB">libxml_get_errors</span><span style="color: #007700">() as </span><span style="color: #0000BB">$error</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"Libxml error: </span><span style="color: #007700">{</span><span style="color: #0000BB">$error</span><span style="color: #007700">-></span><span style="color: #0000BB">message</span><span style="color: #007700">}</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br /> }<br />}<br /></span><span style="color: #0000BB">libxml_use_internal_errors</span><span style="color: #007700">(</span><span style="color: #0000BB">false</span><span style="color: #007700">);<br /><br />if (</span><span style="color: #0000BB">$result</span><span style="color: #007700">) {<br /> echo </span><span style="color: #0000BB">$xsl</span><span style="color: #007700">-></span><span style="color: #0000BB">transformToXML</span><span style="color: #007700">(</span><span style="color: #0000BB">$xmldoc</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output something similar to:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> Libxml error: Invalid expression Libxml error: compilation error: file fruits.xsl line 9 element value-of Libxml error: xsl:value-of : could not compile select expression 'THIS IS A DELIBERATE ERROR!' </pre></div> </div> </div> </p> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="xsl.examples-collection.html">Example collection.xml and collection.xsl files</a></div> <div class="next" style="text-align: right; float: right;"><a href="class.xsltprocessor.html">XSLTProcessor</a></div> <div class="up"><a href="xsl.examples.html">Examples</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>