JScript Code (errorParams.js)

 

[This sample code uses features that were implemented only in MSXML 6.0.]

var xs, xd;

errorParameters();

function errorParameters() 
{
  try {
    xs = new ActiveXObject("MSXML2.XMLSchemaCache.6.0");
    xd = new ActiveXObject("MSXML2.DOMDocument.6.0");
  }
  catch (e) {
    WScript.Echo("Microsoft XML Core Services (MSXML) 6.0 is not installed.\n"
          +"Install MSXML 6.0 or later before continuing.");
    return;
  }

  try {
    xs.add("urn:books", "books.xsd");
    xd.schemas = xs;
    xd.async = false;
    xd.validateOnParse = false;
  }
  catch (e) {
    WScript.Echo("Failed to add to schema cache: "+e.description);
    return;
  }

  try {
    xd.load("books.xml");
  }
  catch (e) {
    WScript.Echo("can't load books.xml : " + e.description);
    return;
  }

  msg = "Validating DOM...\n";
  var err = xd.validate();
  if (err.errorCode != 0 ) {
    msg += "invalid dom:\n\treason:\n" + err.reason 
        + "\terrorXPath:\n" + err.errorXPath
        + "\nParameters Count: "+err.errorParametersCount
        + "\n";
    for (var i=0; i<err.errorParametersCount; i++)
    {
       msg += "\terrorParameters("+i+"): "
           +  err.errorParameters(i)
           + "\n";
    }
  }
  else
    msg +="\tDOM is valid:\n" + xd.xml+"\n";


  msg += "\n\nValidating nodes...\n";
  var nlist = xd.selectNodes("//book");
  for (var node = nlist.nextNode(); node!=null; node=nlist.nextNode())
  {
    var err = xd.validateNode(node);

    if (err.errorCode != 0) 
    {
      msg += "\nNode is invalid:\n\treason:\n" + err.reason
          +  "\terrorXPath: \n" + err.errorXPath
        + "\nParameters Count: "+err.errorParametersCount
          +  "\n";
      for (var i=0; i<err.errorParametersCount; i++)
      {
         msg += "\terrorParameters("+i+"): "
             +  err.errorParameters(i)
             + "\n";
      }
    }
    else
      msg += "\nNode is valid:\n"+ node.xml+"\n";
  }

  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 errorParams.js, in the same directory where you saved books.xml and books.xsd.

  4. Double click the errorParams.js file from Windows Explorer to launch the application. Alternatively, you can type "errorParams.js" from a command prompt.

    System_CAPS_ICON_note.jpg Note

    On operating systems other than Windows 2000 or Windows XP, you might 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 errorParams Example.

Show: