Share via


JScript Code (noNamespace.js)

 

This topic provides JScript example code.

Try It!

  1. Copy the first XML data file from the resource files, and paste it into a text file. Save the file as nn-valid.xml.

  2. Copy the second XML data file from the resource files, and paste it into a text file. Save the file as nn-notValid.xml, in the same directory where you saved nn-valid.xml.

  3. Copy the XSD schema file from the resource files, and paste it into a text file. Save the file as nn.xsd, in the same directory you used in previous steps.

  4. Copy the JScript listing above, and paste it into a text file. Save the file as noNamespace.js, in the same directory you used in previous steps.

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

    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.

  6. Verify that your output is the same as that listed in the output for this example.

noNamespace.js

var sOutput = validateFile("nn-valid.xml");
sOutput = sOutput + validateFile("nn-notValid.xml");
WScript.Echo(sOutput);

function validateFile(strFile)
{
    // Create an XML DOMDocument object.
    var x = new ActiveXObject("MSXML2.DOMDocument.6.0");

    //Load and validate the specified file into the DOM.
    x.async = false;
    x.validateOnParse = true;
    x.resolveExternals = true;
    x.load(strFile);

    // Return validation results in message to the user.
    if (x.parseError.errorCode != 0)
    {
        return("Validation failed on " + strFile + 
               "\n=====================" +
               "\nReason: " + x.parseError.reason + 
               "\nSource: " + x.parseError.srcText + 
               "\nLine: " + x.parseError.line + "\n");
    }
    else 
        return("Validation succeeded for " + strFile + 
               "\n======================\n" +
               x.xml + "\n");
}