Source: validateDOM.js

 

[This sample code uses features that were first implemented in MSXML 5.0 for Microsoft Office Applications.]

The source code performs the following basic steps:

  1. Creates a DOM instance (oXMLDoc) to hold the XML data.

  2. Creates a DOM instance (oXSDDoc) to hold the XML Schema definition.

  3. Creates an IXMLSchemaCollection or IXMLSchemaCollection2 object (oSCache). This object is also called a schema cache. The application then adds the XML Schema definition (oXSDDoc) to the oSCache.

  4. Associates oSCache with the schemas property of the DOM object for the XML data (oXMLDoc).

  5. Calls the following validation methods on the DOM object for XML data (oXMLDoc):

    • Calls the validate method on oXMLDoc to validate the data set as a whole

      and/or

    • Calls the validateNode(oNode) method on oXMLDoc to validate a node object (oNode) selected from oXMLDoc.

  6. Checks the error returned from validate method and/or the validateNode(oNode) method, to determine if the specified XML data set is valid against the given XML Schema definition.

JScript Source File (validateDOM.js)

main();

function main()
{
  try 
  {
    // Load an XML document into a DOM instance.
    var oXMLDoc = LoadDOM("books.xml");

    // Load the schema for the xml document.
    var oXSDDoc = LoadDOM("books.xsd");
 
    // Create a schema cache instance.
    var oSCache = new ActiveXObject("msxml2.XMLSchemaCache.6.0");
    
    // Add the just-loaded schema definition to the schema collection
    oSCache.add("urn:books", oXSDDoc);

    // Assign the schema to the XML document's schema collection
    oXMLDoc.schemas = oSCache;

    // Validate the entire DOM.
    alert("Validating DOM...");
    var oError = oXMLDoc.validate();
    if (oError.errorCode != 0) 
    {
       alert("\tXMLDoc is not valid because\n"+oError.reason); 
    }
    else
    {
       alert("\tXMLDoc is validated:\n"+oXMLDoc.xml);
    }

    // Validate all //books nodes, node by node.
    alert("Validating all book nodes, '//book', one by one ...");
    var oNodes = oXMLDoc.selectNodes("//book");
    for (i=0; i<oNodes.length; i++)
    {
       oNode = oNodes.item(i);
       oError = oXMLDoc.validateNode(oNode);
       if (oError.errorCode != 0)
       {
         alert("\t<"+oNode.nodeName+"> ("+i+") is not valid because\n"+
               oError.reason);
       }
       else 
       {
         alert("\t<"+oNode.nodeName+" ("+i+") is a valid node");
       }  
    }

    // validate all children of all book node, //book/*, node by node
    oNodes = oXMLDoc.selectNodes("//book/*");
    alert("Validating all children of all book nodes, "+
          "//book/*, one by one...");
    for (i=0; i<oNodes.length; i++)
    {
       oNode = oNodes.item(i);
       oError = oXMLDoc.validateNode(oNode);
       if (oError.errorCode !=0) 
          alert("\t<"+oNode.nodeName+"> ("+i+") is not valid beacause\n"
               +oError.reason);
       else
          alert("\t<"+oNode.nodeName+"> ("+i+") is a valid node");
    }
  }
  catch(e) 
  {
    alert(e.description);
  }
}

function LoadDOM(file)
{
   var dom;
   try {
     dom = MakeDOM(null);
     dom.load(file);
   }
   catch (e) {
     alert(e.description);
   }
   return dom;
}

function MakeDOM(progID)
{
  if (progID == null) {
    progID = "msxml2.DOMDocument.6.0";
  }

  var dom;
  try {
    dom = new ActiveXObject(progID);
    dom.async = false;
    dom.validateOnParse = false;
    dom.resolveExternals = false;
  }
  catch (e) {
    alert(e.description);
  }
  return dom;
}

function alert(str)
{
  WScript.Echo(str);
}

To add validateDOM.js to the project

  1. Copy the code listing above. Paste it into the JScript code editor, and save the file as validateDom.js in the current project folder.

Next, we'll add the resource files.