Microsoft HomeproductssearchsupportshopWrite Us   Microsoft Home
Magazine
 |  Community
 |  Workshop
 |  Tools & Samples
 |  Training
 |  Site Info

Workshop  |  XML (Extensible Markup Language)

XSL Tutorial
Lesson 14: Handling XSL Errors


November 4, 1998

What is an XSL error?

An XSL stylesheet can generate two types of errors: parse errors and run-time errors. There are ways to check for these errors and report them.

Here is a simple HTML page that uses XML data islands to load an XML file and an XSL stylesheet and display the results:

<HTML>
  <XML id="data" src="simple.xml"></XML>
  <XML id="style" src="simple.xsl"></XML>
  <SCRIPT event="onload" for="window">
    showResult.innerHTML = data.transformNode(style.XMLDocument);
  </SCRIPT>
  <BODY>
    <DIV id="showResult"></DIV>
  </BODY>
</HTML>

The call to transformNode invokes the XSL processor, and the results are inserted into the "result" DIV. But what if nothing appears? An error occurred somewhere, so we need to check and see where it happened.

How can I handle XSL errors?

The parseError object should first be checked to see whether there was a parsing error in the XML file or in the stylesheet. If an error occurred, you can put that into the page instead, so you can see what went wrong.

<HTML>
  <XML id="data" src="simple.xml"></XML>
  <XML id="style" src="simple.xsl"></XML>
  <SCRIPT event="onload" for="window">
    if (data.parseError.errorCode != 0)
      result = "Parsing data failed!";
    else
    {
      if (style.parseError.errorCode != 0)
        result = "Parsing stylesheet failed!";
      else
      {
        result = data.transformNode(style.XMLDocument);
      }
    }
    showResult.innerHTML = result;
  </SCRIPT>
  <BODY>
    <DIV id="showResult"></DIV>
  </BODY>
</HTML>

XSL run-time errors (occurring inside calls to transformNode) will be reported to the user through the standard Internet Explorer 5 error dialog. However, it is often useful to suppress this dialog and do your own handling of the error message. In this case, the error information is formatted to make it more readable and inserted into the page. In addition, a custom function, reportError, is used to display a little more information.

<HTML>
  <XML id="data" src="simple.xml">
  <XML id="style" src="simple.xsl">
  <SCRIPT>
    function reportError(error)
    {
      var s = "";
      for (var i=1; i 0)
        r += "<font size=3><XMP>" +
        "at line " + error.line + ", character " + error.linepos +
        "\n" + error.srcText +
        "\n" + s + "^" +
        "</XMP></font>";
      return r;
    }
  </SCRIPT>

  <SCRIPT event="onload" for="window">
    if (data.parseError.errorCode != 0)
      result = reportError(data.parseError);
    else
    {
      if (style.parseError.errorCode != 0)
        result = reportError(style.parseError);
      else
      {
        try {
          result = data.transformNode(style.XMLDocument);
        }
        catch (exception) {
          result = "<font face=Verdana size=2><font size=4>XSL Runtime Error</font>" +
          "<P><B>" + exception.description + "</B></P></font>";
        }
      }
    }
    // insert the results into the page
    showResult.innerHTML = result;
  </SCRIPT>
  <BODY>
    <DIV id="showResult"></DIV>
  </BODY>
</HTML>

Reporting errors using this code is also useful in an ASP page, Here is the equivalent code for processing an XML file on the server:

<% @LANGUAGE = JScript %>
<%
  // Error formatting function:
  function reportError(error)
  {
    var s = "";
    for (var i=1; i 0)
      r += "<font size=3><XMP>" +
      "at line " + error.line + ", character " + error.linepos +
      "\n" + error.srcText +
      "\n" + s + "^" +
      "</XMP></font>";
    return r;
  }
    
  // Load the XML 
  var data = Server.CreateObject("Microsoft.XMLDOM");
  data.load(Server.MapPath("simple.xml"));
  data.async = false;
  if (data.parseError.errorCode != 0)
  {
    result = reportError(data.parseError);
  }
  else
  {

    // Load the XSL
    var style = Server.CreateObject("Microsoft.XMLDOM");
    style.async = false;
    style.load(Server.MapPath("simple.xml"));
    if (style.parseError.errorCode != 0)
    {
       result = reportError(style.parseError);
    }
    else
    {
      try {
        result = data.transformNode(style);
      }
      catch (exception) {
        result = "<font face=Verdana size=2><font size=4>XSL Runtime Error</font>" +
        "<P><B>" + exception.description + "</B></P></font>";
      }
    }
  }
  Response.Write(result);
%>

Try it!

Using the above code, load a document with an error (a non-well-formed document or an invalid document) and see what happens. Once you've retrieved the error information, see if you can get even more error information by calling the other properties on the parseError object.


Did you find this article useful? Gripes? Compliments? Suggestions for other articles? Write us!

Back to topBack to top

© 1998 Microsoft Corporation. All rights reserved. Terms of use.

 

Magazine Home
Ask Jane
DHTML Dude
Extreme XML
For Starters
More or Hess
Servin' It Up
Site Lights
Web Men Talking
Member Community Home
Benefits: Freebies & Discounts
Benefits: Promote Your Site
Benefits: Connect with Your Peers
Benefits at a Glance
Online Special-Interest Groups
Your Membership
SBN Stores
Join Now
Workshop Home
Essentials
Content & Component Delivery
Component Development
Data Access & Databases
Design
DHTML, HTML & CSS
Extensible Markup Language (XML)
Languages & Development Tools
Messaging & Collaboration
Networking, Protocols & Data Formats
Reusing Browser Technology
Security & Cryptography
Server Technologies
Streaming & Interactive Media
Web Content Management
Workshop Index
Tools & Samples Home
Tools
Samples, Headers, Libs
Images
Sounds
Style Sheets
Web Fonts
Training Home
SBN Live Seminars
SBN Live Chats
Courses
Peer Support
CD-ROM Training
Books & Training Kits
Certification
SBN Home
New to SBN?
What's New on SBN
Site Map
Site Search
Glossary
Write Us
About This Site