<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/xsl.examples.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'xsl.examples-errors.php',
    1 => 'Error handling with libxml error handling functions',
    2 => 'Error handling with libxml error handling functions',
  ),
  'up' => 
  array (
    0 => 'xsl.examples.php',
    1 => 'Examples',
  ),
  'prev' => 
  array (
    0 => 'xsl.examples-collection.php',
    1 => 'Example collection.xml and collection.xsl files',
  ),
  'next' => 
  array (
    0 => 'class.xsltprocessor.php',
    1 => 'XSLTProcessor',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/xsl/examples.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<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-1">
    <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">&lt;fruits&gt;
 &lt;fruit&gt;Apple&lt;/fruit&gt;
 &lt;fruit&gt;Banana&lt;/fruit&gt;
 &lt;fruit&gt;Cherry&lt;/fruit&gt;
&lt;/fruits&gt;</pre>
</div>
    </div>

   </div>
   <div class="example" id="example-2">
    <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">&lt;xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;&gt;
 &lt;xsl:output method=&quot;html&quot; encoding=&quot;utf-8&quot; indent=&quot;no&quot;/&gt;
 &lt;xsl:template match=&quot;fruits&quot;&gt;
  &lt;ul&gt;
   &lt;xsl:apply-templates/&gt;
  &lt;/ul&gt;
 &lt;/xsl:template&gt;
 &lt;xsl:template match=&quot;fruit&quot;&gt;
  &lt;li&gt;&lt;xsl:value-of select=&quot;THIS IS A DELIBERATE ERROR!&quot;/&gt;&lt;/li&gt;
 &lt;/xsl:template&gt;
&lt;/xsl:stylesheet&gt;</pre>
</div>
    </div>

   </div>
   <div class="example" id="xsl.examples-errors.capture">
    <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.php" 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">&lt;?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">-&gt;</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">-&gt;</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">-&gt;</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">-&gt;</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">-&gt;</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">?&gt;</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 &#039;THIS IS A DELIBERATE ERROR!&#039;
</pre></div>
    </div>
   </div>
  </p>
 </div><?php manual_footer($setup); ?>