var xs, xd;
errorParameters();
function errorParameters()
{
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");
xd.schemas = xs;
xd.async = false;
xd.validateOnParse = false;
}
catch (e) {
alert("Failed to add to schema cache: "+e.description);
return;
}
try {
xd.load("books.xml");
}
catch (e) {
alert("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";
}
alert(msg);
}
function alert(msg)
{
WScript.echo(msg);
}
Try It!
Note Under 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.