CascadiaPHP 2024

Error handling with libxml error handling functions

libxml offers a number of functions for handling errors, which can be employed to capture and deal with errors in XSLT processing.

Example #1 fruits.xml

A valid XML file.


Example #2 fruits.xsl

Contains an invalid select expression.

<xsl:stylesheet version="1.0" xmlns:xsl="">
 <xsl:output method="html" encoding="utf-8" indent="no"/>
 <xsl:template match="fruits">
 <xsl:template match="fruit">
  <li><xsl:value-of select="THIS IS A DELIBERATE ERROR!"/></li>

Example #3 Collating and printing errors

The example below captures and displays libxml errors raised when calling XSLTProcessor::importStyleSheet() with a stylesheet containing an error.


= new DOMDocument();
$xsldoc = new DOMDocument();
$xsl = new XSLTProcessor();


$result = $xsl->importStyleSheet($xsldoc);
if (!
$result) {
foreach (
libxml_get_errors() as $error) {
"Libxml error: {$error->message}\n";

if (
$result) {


The above example will output something similar to:

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!'

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top