JScript Code (allErrors.js)

MSXML 5.0 SDK

Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - DOM Reference

JScript Code (allErrors.js)

var xs, xd;

parseError2();

function parseError2() 
{
  try {
    xs = new ActiveXObject("MSXML2.XMLSchemaCache.5.0");
    xd = new ActiveXObject("MSXML2.DOMDocument.5.0");
  }
  catch (e) {
    alert("Mirosoft XML Core Services (MSXML) 5.0 for Microsoft Office is not installed.\n"
          +"Download and install MSXML 5.0 from http://msdn.microsoft.com/xml\n"
          +"before continuing.");
    return;
  }

  try {
    xs.add("urn:books", "books.xsd");
  }
  catch (e) {
    alert("Failed to add to schema cache: "+e.description);
    return;
  }

  try {
    xd.schemas = xs;
    xd.async = false;
    xd.validateOnParse = false;
    xd.load("books.xml");
    xd.setProperty("MultipleErrorMessages", true);
  }
  catch (e) {
    alert("can't load books.xml : " + e.description);
    return;
  }

  var err = xd.validate();
  if (err.errorCode != 0 ) {
    errsCount = err.allErrors.length; 
    var errStr = "Error as returned from validate():\n"
              +"\n\tError Code    : " + err.errorCode
              +"\n\tError reason  :\n" + err.reason
              +"\tError location: \n" + err.errorXPath
              +"\n\tErrors Count  : " + errsCount
              +"\n\nError items from the allErrors collection:";

    for (var i=0; i<errsCount; i++)
    {
       var e = err.allErrors.item(i);
       errStr += "\n\nErrorItem["+i+"] : ";
       errStr += "\n\treason:\n" +e.reason; 
       errStr += "\tlocation:\n" +e.errorXPath; 
    }
    alert(errStr);
  }
  else
    alert("valid dom as follows:\n" + xd.xml);

}

function alert(msg) 
{
   WScript.echo(msg);
}

Try It!

  1. Copy the XML data (books.xml), and paste it into a text file. Save the file as books.xml.
  2. Copy the XSD listing (books.xsd), and paste it into a text file. Save the file as books.xsd, in the same directory where you saved books.xml.
  3. Copy the JScript listing above, and paste it into a text file. Save the file as allErrors.js in the same directory where you saved books.xml and books.xsd.
  4. Double click the allErrors.js file from Windows Explorer to launch the application. Alternatively, you can type "allErrors.js" from a command prompt.
    Note   Under operating systems other than Windows 2000 or Windows XP, you need to install Windows Scripting Host (wscript.exe), if it is not already installed.
  5. Verify that your output is the same as that listed in Output for the allErrors Example.