Testing your DTD XML Schema Validation | 2 | WebReference

Testing your DTD XML Schema Validation | 2


Testing your DTD XML Schema Validation [con't]

Creating the XML Parser Object

There are several versions of XML parsers available, version six being the most recent. The original and most basic one is the Microsoft.XMLDOM library. It points to the msxml version 2 dll. To create it, use the following code in JavaScript:

An equivalent way of referencing version 2 of the msxml library is to use the MSXML.DOMDocument library:

The following lines will both invoke version 3:

The latest three versions have followed the same format of appending the version number to the end of the library name:

Note that MSXML can exist with many different versions on a single computer. Some upgrades are overwrites and some are side-by-side. The following page on the Microsoft website explains the differences between the various msxml libraries. When in doubt as to which version you want to use, stick with MSXML2.DOMDocument as recommended in JavaScript : The Definitive Guide (5th ed.) by David Flanagan.

Figure 1 shows an example of an XML file that passed the validation test:

Figure 1

Figure 2 shows an XML document which did not pass because the case_list element did not have any children. According to our DTD spec, it must contain at least one:

Figure 2


Validating multiple XML Documents in a Batch Process

When validating a small XML document as we are here, it isn't too time intensive to validate each test scenario using the validateCaseSummary.html file. However, it can become a burdensome task when testing larger documents. For those, I recommend using a batch process. The Windows operating systems can execute scripts using the Windows Script Host (WSH). WSH provides an environment for scripts to run; it invokes the appropriate script engine and provides a set of services and objects for the script to work with. These scripts may be run in either GUI mode (WScript.exe) or command line mode (CScript.exe), offering flexibility to the user for interactive or non-interactive scripts. Windows scripting is used for a variety of purposes, including logon scripts, administration and general automation.

WSH supports scripts written in Microsoft Visual Basic Scripting Edition (VBScript) or JavaScript. We'll use VBScript because we need to access the file system. The idea is simply to iterate through all the files in the directory and parse the XML documents. Results of the process are written to a file called "Batch DTD Test Results.txt". The following code would be saved as a .vbs file and can be run directly from Explorer by double-clicking the file:

This script makes use of three other objects, in addition to the Msxml2.DOMDocument parser: Wscript.Shell, Scripting.FileSystemObject, and Shell.Application.

The Wscript.Shell is used to set the working directory, which saves us from constantly including the full path to directories and folders. The WScript.ScriptFullName property holds the full path to our script. From it, we can obtain the path without the file name, using the InStrRev() function. It returns the character position of the last backslash (\) in the path.

The Scripting.FileSystemObject serves multiple purposes, including:

  • obtaining a reference to the CurrentDirectory (line 17)
  • creating the results file (line 20)
  • returning the extension of each file in the directory (line 23)

Finally, the Shell.Application object launches the associated program to view the results text file.

Unlike the validateCaseSummary.html file, which displays the results for one XML document in the browser, the batch process outputs a report for all the XML files in the directory:

The Microsoft XML parser libraries offer a painless and quick way to test DTD validation of XML documents outside of an application. Using either VBScript or JavaScript, you can test one or many files against their associated DTD specifications. Simplifying the testing process is not just beneficial to you, the developer, but it also makes it easy for non-technical people to conduct QA testing, as they don't need to know about the scripting code to run the tests.

Rob Gravelle combined his love of programming and music to become a software guru and accomplished guitar player. He created systems that are used by Canada Border Services, CSIS and other Intelligence-related organizations. As a software consultant, Rob has developed Web applications for many businesses and recently created a MooTools version of PHPFreechat for ViziMetrics. Musically, Rob recently embarked on a solo music career, after playing with Ivory Knight since 2000. That band was rated as one Canada's top bands by Brave Words magazine (issue #92) and released two CDs. Rob's latest, entitled KNIGHTFALL, was a collaboration between himself, the former Ivory Knight vocalist, and legendary guitarist/producer, Jeff Waters of Annihilator fame. Rob is available for short-term software projects and recording session work. to inquire, but note that, due to the volume of emails received, he cannot respond to every email. Potential jobs and praise receive highest priority!

Original: June 3, 2009