JScript Code (schemaLocation.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 sl-valid.xml.

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

  3. Copy the XSD schema file from the resource files, and paste it into a text file. Save the file as sl.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 schemaLocation.js, in the same directory you used in previous steps.

  5. Double click the schemaLocation.js file from Windows Explorer to launch the application. Alternatively, you can type "cscript schemaLocation.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.

schemaLocation.js

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

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

    // Set first-level DOM properties.
    x.async = false;
    x.validateOnParse = true;
    x.resolveExternals = true;

    // Configure DOM properties for namespace selection.
    x.setProperty("SelectionLanguage", "XPath");
    ns = "xmlns:x='urn:book'";
    x.setProperty("SelectionNamespaces", ns);

    // Load and validate the specified file into the DOM.
    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");
}

Try It!

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

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

  3. Copy the XSD schema file from the resource files, and paste it into a text file. Save the file as sl.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 schemaLocation.js, in the same directory you used in previous steps.

  5. Double click the schemaLocation.js file from Windows Explorer to launch the application. Alternatively, you can type "cscript schemaLocation.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.