You can use Visual Basic to validate an XML document against an XML Schema definition language (XSD) schema. To validate a document, add the schema to the XMLSchemaCache object, set the schemas property of a DOMDocument object to reference the schema in the XMLSchemaCache object, and then load the XML file you want to validate into the DOMDocument object. Validation is performed when the document is loaded into the DOMDocument object. Validation errors are handled using the parseError property of the DOMDocument object. This topic provides a quick example of how to validate a document against a schema and consists of the following.
To get started
The books.xml file used in this example, shown below, is a modified version of the books.xml file used throughout the MSXML SDK. Notice that "urn:books" is declared as the default namespace for this document.
If you want to run the example in this topic, copy the following code to a text editor, such as Notepad, and then save the file as books.xml.
<?xml version="1.0"?> <x:catalog xmlns:x="urn:books"> <book id="bk101"> <author>Gambardella, Matthew</author> <title>XML Developer's Guide</title> <genre>Computer</genre> <price>44.95</price> <publish_date>2000-10-01</publish_date> <description>An in-depth look at creating applications with XML.</description> </book> </x:catalog>
The following document shows the books.xsd schema used in this example.
If you want to run the example in this topic, copy the following code to a text editor, such as Notepad, and then save the file as books.xsd.
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:books" xmlns:b="urn:books"> <xs:element name="catalog" type="b:CatalogData"/> <xs:complexType name="CatalogData"> <xs:sequence> <xs:element name="book" type="b:bookdata" minOccurs="0" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> <xs:complexType name="bookdata"> <xs:sequence> <xs:element name="author" type="xs:string"/> <xs:element name="title" type="xs:string"/> <xs:element name="genre" type="xs:string"/> <xs:element name="price" type="xs:float"/> <xs:element name="publish_date" type="xs:date"/> <xs:element name="description" type="xs:string"/> </xs:sequence> <xs:attribute name="id" type="xs:string"/> </xs:complexType> </xs:schema>
The following code snippet shows how you can perform validation on the books.xml document. This code creates an XMLSchemaCache50 object, adds the namespace URI declaration ("urn:books") and the schema (books.xsd) to the object, and then references the schema using the schemas property of the DOMDocument object. Validation is performed when the books.xml file is loaded into the DOMDocument object. Validation errors are returned using the parseError property of the DOMDocument object.
To run the example
Private Sub Command1_Click() 'Create a schema cache and add books.xsd to it. Dim xmlschema As MSXML2.XMLSchemaCache50 Set xmlschema = New MSXML2.XMLSchemaCache50 xmlschema.Add "urn:books", App.Path & "\books.xsd" 'Create an XML DOMDocument object. Dim xmldom As MSXML2.DOMDocument50 Set xmldom = New MSXML2.DOMDocument50 'Assign the schema cache to the DOM document. 'schemas collection. Set xmldom.schemas = xmlschema 'Load books.xml as the DOM document. xmldom.async = False xmldom.Load App.Path & "\books.xml" 'Return validation results in message to the user. If xmldom.parseError.errorCode <> 0 Then MsgBox xmldom.parseError.errorCode & " " & _ xmldom.parseError.reason Else MsgBox "No Error" End If End Sub
When you run the example, it returns a "No Errors" message box.
To generate an error message for the validation code, change the books.xml document so the title element appears directly after the catalog element, as shown in bold in the following example. This makes the document invalid according to the books.xsd schema. Save the books.xml file and then run the Visual Basic code. The code returns the following error message:
"1072898028 Element content is invalid according to DTD/Schema. Expecting: book."
<?xml version="1.0"?>
<x:catalog xmlns:x="urn:books">
<title>2000-10-01</title>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with
XML.</description>
</book>
</x:catalog>
XML Schema Examples | XML Schema Element Map | XML Schema Elements | XML Data Types Reference | Primitive XML Data Types | Derived XML Data Types | Data Type Facets | IXMLDOMSchemaCollection/XMLSchemaCache
World Wide Web Consortium (W3C) XML Schema Part 2: Datatypes | World Wide Web Consortium (W3C) XML Schema Part 1: Structures