Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
[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:
Creates a DOM instance (
oXMLDoc) to hold the XML data.Creates a DOM instance (
oXSDDoc) to hold the XML Schema definition.Creates an
IXMLSchemaCollectionorIXMLSchemaCollection2object (oSCache). This object is also called a schema cache. The application then adds the XML Schema definition (oXSDDoc) to theoSCache.Associates
oSCachewith theschemasproperty of the DOM object for the XML data (oXMLDoc).Calls the following validation methods on the DOM object for XML data (
oXMLDoc):Calls the
validatemethod onoXMLDocto validate the data set as a wholeand/or
Calls the
validateNode(oNode)method onoXMLDocto validate a node object (oNode) selected fromoXMLDoc.
Checks the error returned from
validatemethod and/or thevalidateNode(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
- 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.