PHP's XSLT API includes some functions designed specifically to assist in tracking and displaying errors. Depending on your requirements, you can set up a primitive error handler to display error codes and messages with the xslt_errno() and xslt_error() functions, or define a custom error handler with the xslt_set_error_handler() function.
Primitive Error Handling with xslt_errno() and xslt_error()
The xslt_errno() function returns the error code of the last error encountered by the XSLT processor, whereas the xslt_error() function generates a corresponding human-readable error message. Both functions require, as argument, a handle representing the current XSLT processor, as defined by xslt_create(). If no argument is provided, the last XSLT error number/error string that occurred is returned.
To illustrate how this error handling works, let's take another look at our first example (see Listing 4.4), which demonstrates this technique.
As you can see, this is pretty basic. If an error is encountered while xslt_process() is performing the transformation, PHP will merely display an error code and message. You can verify this by deliberately introducing an error into your XML or XSLT document (mismatched tags are always a favorite) and watching how PHP reacts.
Defining a Custom Error Handler with xslt_set_error_handler()
A more professional approach to error handling would be to define a custom error handler to resolve all errors that occur during the transformation process. And PHP allows you to do just that with its xslt_set_error_handler() function, which lets you define and use your own exception handling mechanism.
The xslt_set_error_handler() function accepts two arguments: a handle representing the current XSLT processor, as returned by xslt_create(), and the name of the function to pass all errors to.
The following code snippet illustrates this by setting the user-defined function myHandler() as the handler for all errors generated during the transformation process:
The user-defined error handler specified as the second argument to xslt_set_error_handler() must be set up to accept the following four arguments:
Handle representing the XSLT processor
Error level (whether notice, warning, or fatal error)
Array containing information on the error
Of these, the last is perhaps the most interesting. It's an associative array containing detailed information on the error, such as the type of error, the line on which it occurred, and the name of the file containing the broken code. Take a look at the following sidebar (titled "Under the Microscope") for a closer look at the contents of this array.
Listing 4.5 updates the example in Listing 4.4 to use the function customErrorHandler() as the handler for any and all errors encountered during the transformation process. A while() loop is used to iterate through the array to produce a more useful and descriptive error message.
If you prefer, you can have the error handler return only the error message and the line number on which it occurred simply by accessing the relevant keys of the associative array. In Listing 4.5, this information is stored within the associative array $einfo, in the keys line and msg respectively (take a look at the sidebar entitled "Under The Microscope" for details).