This code requires a project reference to
Microsoft XML 4.0 available from Microsoft's
Download Center
This is a very simple example to demostrate the validation of a fragment of XML.
We want a validate than a element called sampleRoot has an attribute called myAttribute that can have a value of either 'animal', 'vegatable' or 'mineral'.
Sample XSD
This XSD defines our schema. We have to give it a namespace, for this example we're using urn:sample
<!-- two namespaces used, xsd schema and the one we're defining (targetNamespace) -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:n="urn:sample"
targetNamespace="urn:sample">
<!-- define root element -->
<xs:element name="sampleRoot" type="n:sampleRootType"/>
<!-- define a datatype: sampleRootType -->
<xs:complexType name="sampleRootType">
<xs:attribute name="myAttribute" type="n:AnimalVegMineral" use="optional"/>
</xs:complexType>
<!-- define a Animal/Veg/Mineral data type -->
<xs:simpleType name="AnimalVegMineral">
<xs:restriction base="xs:string">
<xs:enumeration value="animal"/>
<xs:enumeration value="vegatable"/>
<xs:enumeration value="mineral"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
Samples of XML that Reference Our Schema Definition
XML documents that should conform to this schema must declare their namespace. The following fragments conform to the bove schema definition:
<x:sampleRoot xmlns:x="urn:sample" myAttribute="animal"/>
The following fragments do not.
<x:sampleRoot xmlns:x="urn:sample" myAttribute="piano"/>
Visual Basic Code to Validate an XML File against an XSD File
This function accepts the paths of two datafiles. An XML Schema Definition and an XML file.
Private Function Validate(ByVal strXMLPath As String, _
ByVal strXSDPath As String) As Boolean
Dim objSchemas As MSXML2.XMLSchemaCache40
Dim objXML As MSXML2.DOMDocument40
Dim objXSD As MSXML2.DOMDocument40
Dim strNamespace As String
Dim objErr As MSXML2.IXMLDOMParseError
' load XSD as DOM to populate in Schema Cache
Set objXSD = New MSXML2.DOMDocument40
objXSD.async = False
If Not objXSD.Load(strXSDPath) Then
Err.Raise 1, "Validate", "Load XSD failed: " & objXSD.parseError.reason
Else
' get namespace name from XSD targetNamespace attribute
strNamespace = objXSD.documentElement.getAttribute("targetNamespace")
End If
' populate schema cache
Set objSchemas = New MSXML2.XMLSchemaCache40
objSchemas.Add strNamespace, objXSD
' load XML file (without validation - that comes later)
Set objXML = New MSXML2.DOMDocument40
objXML.async = False
objXML.validateOnParse = False
objXML.resolveExternals = False
' load XML, without any validation
If Not objXML.Load(strXMLPath) Then
Err.Raise 1, "Validate", "Load XML failed: " & objXML.parseError.reason
End If
' bind Schema Cache to DOM
Set objXML.schemas = objSchemas
' does this XML measure up?
Set objErr = objXML.Validate()
' any good?
Validate = (objErr.errorCode = 0)
If objErr.errorCode <> 0 Then
Err.Raise 1, "Validate", objErr.reason
End If
End Function
The above function could easily be adapted to load XML and XSD from string variables as opposed to data files.
For situations where multiple XML validations are required, repeatedly populating and destroying the schema cache is clearly ineffecient and could be optimised.
Examples Calling this Function
Validate App.Path & "\sc-valid-tjh.xml", App.Path & "\sc-tjh-min.xsd"
Validate App.Path & "\sc-valid.xml", App.Path & "\sc-tjh-min.xsd"
<script type="text/javascript">// <![CDATA[ google_ad_client = "pub-0974635012543471"; google_ad_width = 728; google_ad_height = 90; google_ad_format = "728x90_as"; google_ad_type = "text"; google_ad_channel =""; google_color_border = "DCDCDC"; google_color_bg = "F5F5F5"; google_color_link = "000080"; google_color_url = "008000"; google_color_text = "000000"; // ]]></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>