Handling Parsing Errors

Handling Parsing Errors

Now that were using JavaScript to load XML and XSL documents and work on them, its useful to know how to report parsing errors. In the preceding example, I reported errors by displaying the message Error! in the target <DIV> element of the HTML document, but thats not too helpful. How can I get more information?

In the following example, I create a deliberate parsing error by changing the first <PLANET> tag in planets.xml to a <PLANETS> tag:

 <?xml version="1.0"?>  <?xml-stylesheet type="text/xml" href="planets.xsl"?>  <PLANETS>      <PLANETS>          <NAME>Mercury</NAME>          <MASS UNITS="(Earth = 1)">.0553</MASS>          <DAY UNITS="days">58.65</DAY>          <RADIUS UNITS="miles">1516</RADIUS>          <DENSITY UNITS="(Earth = 1)">.983</DENSITY>          <DISTANCE UNITS="million miles">43.4</DISTANCE><!--At perihelion-->      </PLANET>          .          .          . 

I have set the XMLDocument objects validateOnParse property to true (the default is false , which means no validation is performed) so the MSXML processor is sure to catch this parsing error. The XMLDocument object contains a parseError object, and if the parseError objects errorCode property is not zero, an error has occurred. Rather than just display the message Error! in this case, however, I decipher that error to get more details in a new function named getError , which returns a string explaining the errors position and nature.

To get this additional information, I use the parseError objects url , line , linepos , and reason properties to pinpoint the file that caused the problem, the line and position of the error, and the errors description:

Listing 10.1 Creating an XSLT Transformation and Displaying Parse Errors
 <HTML>      <HEAD>          <TITLE>XSLT Using JavaScript</TITLE>          <SCRIPT LANGUAGE="JavaScript">          function xslt()          {             var XMLDocument = new ActiveXObject('MSXML2.DOMDocument.3.0');              var XSLDocument = new ActiveXObject('MSXML2.DOMDocument.3.0');              var HTMLtarget = document.all['targetDIV'];              XMLDocument.validateOnParse = true;              XMLDocument.load('planets.xml');              if (XMLDocument.parseError.errorCode != 0) {                 HTMLtarget.innerHTML = getError(XMLDocument)                  return false;              }              XSLDocument.validateOnParse = true;              XSLDocument.load('planets.xsl');              if (XSLDocument.parseError.errorCode != 0) {                 HTMLtarget.innerHTML = getError(XSLDocument)                  return false;              }              HTMLtarget.innerHTML = XMLDocument.transformNode(XSLDocument);              return true;          }          function getError(errorObject)          {             var Error = new String;              Error = "Error. " + errorObject.parseError.url + "<BR>"                  + "Line: " + errorObject.parseError.line + "<BR>"                  + "Character: " + errorObject.parseError.linepos + "<BR>"                  + "Description: " + errorObject.parseError.reason;              return Error;          }          </SCRIPT>      </HEAD>      <BODY onload="xslt()">          <DIV ID="targetDIV">          </DIV>      </BODY>  </HTML> 

You can see the results in Figure 10.2, where you see the file that caused the error, the location of the error, and the MSXML processors explanation of the error. If youre going to handle XSLT transformations in the Internet Explorer when dealing with people browsing to your documents casually, handling parse errors such as this one is crucial.

Figure 10.2. Handling a parsing error.
graphics/10fig02.gif

So far, Ive only transformed an entire document using the MSXML processor, but theres considerably more control available to you here. For example, in the next section, I use XSLT in the Internet Explorer to enable the user to sort the HTML table that displays planetary data just by clicking buttons . And I do that by accessing the individual nodes inside the stylesheet.



Inside XSLT
Inside Xslt
ISBN: B0031W8M4K
EAN: N/A
Year: 2005
Pages: 196

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net