Validating XML Documents


As mentioned in the beginning of this chapter, you can specify the syntax of an XML document yourself. You can do that typically with a DTD or an XML schema (which we won't cover here; see www.w3.org/TR/xmlschema-0/ for more information). The Internet Explorer does try to validate XML documents as it loads them, checking them against the syntax you've specified; but you won't see any validation errors unless you check the parseError object. Let's take a look at how that works.

Tip

You can also turn validation on or off with the document object's validateOnParse property. This property is set to true by default, but if you set it to false , validation is turned off.


Here's an example where I'll purposely introduce an error into our sample XML document, 22-01.xml. That document has a DTD that specifies the syntax for the document's contents; for example, the DTD entry for the <EVENT> element indicates that this element should contain an <EVENT_TITLE> element, an <EVENT_NUMBER> element, a <SUBJECT> element, and a <DATE> element, followed by zero or more <PEOPLE> elements, in that order:

 <?xml version="1.0"?>  <!DOCTYPE EVENTS [  <!ELEMENT EVENTS (EVENT*)>  <!ELEMENT EVENT (EVENT_TITLE, EVENT_NUMBER, SUBJECT, DATE, PEOPLE*)>  <!ELEMENT EVENT_TITLE (#PCDATA)>  <!ELEMENT EVENT_NUMBER (#PCDATA)>  <!ELEMENT SUBJECT (#PCDATA)>  <!ELEMENT DATE (#PCDATA)>  <!ELEMENT FIRST_NAME (#PCDATA)>  <!ELEMENT LAST_NAME (#PCDATA)>  <!ELEMENT PEOPLE (PERSON*)>  <!ELEMENT PERSON (FIRST_NAME,LAST_NAME)>  <!ATTLIST EVENT      TYPE CDATA #IMPLIED>  <!ATTLIST PERSON      ATTENDENCE CDATA #IMPLIED>  ]>  <EVENTS>      <EVENT TYPE="informal">         <EVENT_TITLE>15th Award Ceremony</EVENT_TITLE>         <EVENT_NUMBER>1207</EVENT_NUMBER>         <SUBJECT>Gala Event</SUBJECT>          .          .          . 

Now I'll alter the DTD by removing the <EVENT_NUMBER> element from the <EVENT> element's specification, creating this new XML document, 22-09.xml:

(Listing 22-09.xml on the web site)
 <?xml version="1.0"?>  <!DOCTYPE EVENTS [  <!ELEMENT EVENTS (EVENT*)>  <!ELEMENT EVENT (EVENT_TITLE, SUBJECT, DATE, PEOPLE*)>  <!ELEMENT EVENT_TITLE (#PCDATA)>  <!ELEMENT EVENT_NUMBER (#PCDATA)>  <!ELEMENT SUBJECT (#PCDATA)>  <!ELEMENT DATE (#PCDATA)>  <!ELEMENT FIRST_NAME (#PCDATA)>  <!ELEMENT LAST_NAME (#PCDATA)>  <!ELEMENT PEOPLE (PERSON*)>  <!ELEMENT PERSON (FIRST_NAME,LAST_NAME)>  <!ATTLIST EVENT      TYPE CDATA #IMPLIED>  <!ATTLIST PERSON      ATTENDENCE CDATA #IMPLIED>  ]>  <EVENTS>        <EVENT TYPE="informal">          <EVENT_TITLE>15th Award Ceremony</EVENT_TITLE>          <EVENT_NUMBER>1207</EVENT_NUMBER>          <SUBJECT>Gala Event</SUBJECT>          <DATE>7/4/2003</DATE>          <PEOPLE>              <PERSON ATTENDENCE="present">                  <FIRST_NAME>Sam</FIRST_NAME>                  <LAST_NAME>Edwards</LAST_NAME>              </PERSON>              <PERSON ATTENDENCE="absent">                  <FIRST_NAME>Sally</FIRST_NAME>                  <LAST_NAME>Jackson</LAST_NAME>              </PERSON>              <PERSON ATTENDENCE="present">                  <FIRST_NAME>Cary</FIRST_NAME>                  <LAST_NAME>Grant</LAST_NAME>              </PERSON>          </PEOPLE>      </EVENT>  </EVENTS> 

With this new XML document, the Internet Explorer will expect a <SUBJECT> element to follow <EVENT_TITLE> elements, not an <EVENT_NUMBER> element, as is actually the case in the data.

So how can we get all the details of the validation error that will occur? We can check whether there's been an error by checking whether the readyState property is set to 4, and if so, we can get all the details of the error with the parseError object's errorCode , url , line , linepos , errorString , and reason properties:

 if(document1.readyState == 4){      var errorString = document1.parseError.srcText      if (document1.parseError.errorCode != 0) {          div1.innerText = "Error in " +          document1.parseError.url +          " line " + document1.parseError.line +          " position " + document1.parseError.linepos +          ".\nError source: " + errorString +          "\n" + document1.parseError.reason +          "\n" +  "Error: " +          document1.parseError.errorCode      }      else {          div1.innerText = "The document loaded OK.\n"      }  } 

Here's how we can use this code in an example that reads in the XML document with the DTD error, 22-09.xml:

(Listing 22-10.xml on the web site)
 <HTML>      <HEAD>          <TITLE>              Validating an XML Document          </TITLE>          <SCRIPT LANGUAGE="JavaScript">              <!--             var document1              function loader()              {                  document1 = new ActiveXObject("microsoft.XMLDOM")                  document1.onreadystatechange = stateChange                  document1.ondataavailable = dataAvailable                  document1.load('22-09.xml')              }              function dataAvailable()              {                  div1.innerHTML += "The data is available.<BR>"              }              function stateChange()              {  if(document1.readyState == 4){   var errorString = document1.parseError.srcText   if (document1.parseError.errorCode != 0) {   div1.innerText = "Error in " +   document1.parseError.url +   " line " + document1.parseError.line +   " position " + document1.parseError.linepos +   ".\nError source: " + errorString +   "\n" + document1.parseError.reason +   "\n" +  "Error: " +   document1.parseError.errorCode   }   else {   div1.innerText = "The document loaded OK.\n"   }   }  }              //-->          </SCRIPT>      </HEAD>       <BODY>          <H1>              Validating an XML Document          </H1>          <INPUT TYPE="BUTTON" VALUE="Check the Document"              ONCLICK="loader()">          <DIV ID="div1"></DIV>      </BODY>  </HTML> 

You can see the results of in Figure 22.6, where the validation error is reported in detail.

Figure 22.6. Displaying XML validation errors in the Internet Explorer.

graphics/22fig06.gif

That completes our look at standard XML in this chapter; I'll take a look at XSLT next .



Inside Javascript
Inside JavaScript
ISBN: 0735712859
EAN: 2147483647
Year: 2005
Pages: 492
Authors: Steve Holzner

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