Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - XSLT Developer's Guide

Implementing Error Handling with XSLT and the DOM

This topic adds error handling functionality to the script in the previous topic, Using XSLT with the DOM from an HTML Page.

XSLT does not provide a native error-detecting and error-reporting mechanism for determining whether a transformation works as intended, or for handling exceptions when they occur. To implement error-handling while processing XSLT or XML, use the following DOM facilities:

Error Handling with the Region.xsl Example

To validate the region.xsl style sheet, add the following JScript fragments to the transformIt() function within the HTML file, qsalesregion.html. For more information about the files for this example, see Using XSLT with the DOM from an HTML Page.

To add the following code to the transformIt() function

  1. Insert the following line immediately after the declaration of the objXSLT object variable.
    objXSLT.validateOnParse = true;
  2. Insert the following lines immediately after the objXSLT.load('region.xsl') statement.
    if (objXSLT.parseError.errorCode != 0) 
    {
      var strErrMsg = "Problem Parsing Style Sheet:<br />"
        + "  Error #: "             + objXSLT.parseError.errorCode + "<br />"
        + "  Description: "         + objXSLT.parseError.reason + "<br />"
        + "  In file: "             + objXSLT.parseError.url + "<br />"
        + "  Line #: "              + objXSLT.parseError.line + "<br />"
        + "  Character # in line: " + objXSLT.parseError.linepos + "<br />"
        + "  Character # in file: " + objXSLT.parseError.filepos + "<br />"
        + "  Source line: "         + objXSLT.parseError.srcText;
      objResTree.innerHTML = strErrMsg;
      return false;
    }

The resulting file is shown in XSLT HTML DOM File with Error Handling.

Similar logic for validating the XML document, region.xml, could be provided at analogous locations in the script.

See Also

Performing Error Handling with XSLT