If you use Microsoft Internet Explorer (IE) 5.0 or later as your favorite XML-enabled browser, you might have noticed an odd feature. IE provides a nice graphical layout of your XML document, but it seems unable to detect whether the XML document complies with the specified XML schema or Document Type Definition (DTD). That isn't a scandalous matter because both validating and nonvalidating parsers populate the XML world. However, I know that Microsoft's XML parser (MSXML)—which IE 5.x is based on—is a validating parser. I remember well that beta versions of IE 5.0 were perfectly capable of validating an XML page against the referred schema or DTD. So what's going on?
Actually, it's simple. The MSXML parser IS a validating parser. It can detect any inconsistency between a schema or DTD and the layout of the submitted XML document. But you can programmatically toggle on and off such a validation engine, and—as you might have guessed—IE 5.0 turns it off on startup. Let's see how the toggling works, codewise.
According to the MSXML object model, the XML document has a Boolean property called validateOnParse that is read/write. (Several methods and properties in the MSXML object model are custom extensions of the standard Document Object Model—DOM—that World Wide Web Consortium—W3C—defines; the validateOnParse property is one of them. Don't expect to find a similar property when you use a different XML parser.) If validateOnParse is true, it enables validation during the parsing phase. If validateOnParse is false, MSXML parses only to verify how well-formed the XML file is. This property is set to true by default.
To load an XML document with MSXML, you use code similar to the following:
set xml = CreateObject("Microsoft.XMLDOM") xml.async = False xml.load "foo.xml"
Because the code doesn't explicitly reference the validateOnParse property, validateOnParse assumes its default value, which is true. If foo.xml references a schema or DTD, the parser checks the schema or DTD as soon as the test for XML well-formedness completes successfully.
If you need to load and parse an XML document regardless of its adherence to a schema or DTD, you use code similar to the following:
set xml = CreateObject("Microsoft.XMLDOM") xml.async = False xml.validateOnParse = False xml.load "foo.xml"
The validateOnParse value applies to the MSXML instance for the lifetime of the instance and is maintained across different calls to different XML files.
You can also use different validation settings at different times with the same instance of MSXML. For example,
set xml = CreateObject("Microsoft.XMLDOM") xml.async = False xml.validateOnParse = False xml.load "foo1.xml" xml.validateOnParse = True xml.load "foo2.xml"
In this case, the parser doesn't validate foo1.xml, but it does validate foo2.xml.
Of course, validateOnParse makes sense only if the XML document includes a reference to a schema or DTD file. Setting it to false when you don't care about the XML document's layout speeds up the initialization process and returns a usable XMLDOM in a shorter time.