Magazine |
| | Community |
| | Workshop |
| | Tools & Samples |
| | Training |
| | Site Info |
|
|
||||||||
|
November 4, 1998
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.
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; i0) 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; i0) 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); %>
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!
© 1998 Microsoft Corporation. All rights reserved. Terms of use.