EDN Admin
Well-known member
Hi folks,
VBA in Excel 2007, MSXML 6 - Im trying to test that validation against an XSD I developed will fail if there are problems in documents associated with the schema. Ive been able to trigger validation errors against this schema using the Eclipse
(Helios) XML tools, but I cant seem to cause MSXML 6 validation to fail unless its something like basic well-formedness of the data document. The schema document itself loads and validates without problems in MSXML 6. Ive tried 1) setting parseOnLoad
true for the data document, 2)doing a manual validate() on the data document, and also adding the schema to a schemaCache and validating against that. None of them will trip, again, unless its a basic well-formedness problem.
Its been a while since Ive had to do this, and Im guessing Im somehow not getting the namespaces wired correctly. Would appreciate it if someone could let me know if the cause of the problem jumps out at them.
Heres the test code:
<pre class="prettyprint lang-vb Sub checkSchema()
Dim vMyErr As Variant
Dim oXSDDoc As DOMDocument60
Set oXSDDoc = New DOMDocument60
oXSDDoc.async = False
Load XML Schema from a file
oXSDDoc.validateOnParse = True
oXSDDoc.Load (ThisWorkbook.Path & "PSTTLTD2.xsd")
If oXSDDoc.parseError.ErrorCode <> 0 Then
Set vMyErr = oXSDDoc.parseError
MsgBox "XML parsing error " + vMyErr.reason
End If
oXSDDoc.Validate
If oXSDDoc.parseError.ErrorCode <> 0 Then
Set vMyErr = oXSDDoc.parseError
MsgBox "XML parsing error " + vMyErr.reason
End If
Load XML document from a file
Dim oSourceDoc As DOMDocument60
Set oSourceDoc = New DOMDocument60
oSourceDoc.async = False
oSourceDoc.validateOnParse = True
oSourceDoc.Load (ThisWorkbook.Path & "XMLFinalSchemaTestDoc.xml")
If oSourceDoc.parseError.ErrorCode <> 0 Then
Set vMyErr = oSourceDoc.parseError
MsgBox "XML parsing error " + vMyErr.reason
End If
oSourceDoc.Validate
If oSourceDoc.parseError.ErrorCode <> 0 Then
Set vMyErr = oSourceDoc.parseError
MsgBox "XML parsing error " + vMyErr.reason
End If
Validate with Schema Cache
Reload schema into cache.
Set oXSDDoc = New DOMDocument60
oXSDDoc.async = False
oXSDDoc.Load (ThisWorkbook.Path & "PSTTLTD2.xsd")
Set oSchemaCache = New XMLSchemaCache60
oSchemaCache.Add "urn:histogramList", oXSDDoc
Set oSourceDoc.Schemas = oSchemaCache
oSourceDoc.Validate
If oSourceDoc.parseError.ErrorCode <> 0 Then
Set vMyErr = oSourceDoc.parseError
MsgBox "XML parsing error " + vMyErr.reason
End If
End Sub[/code]
<br/>
Heres the schema:
<pre class="prettyprint <?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="urn:histogramList"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:hL="urn:histogramList"
elementFormDefault="qualified
<xs:element name="HistogramList
<xs:complexType>
<xs:sequence>
<xs:element name="Histogram" minOccurs="1" maxOccurs="unbounded
<xs:complexType>
<xs:sequence>
<xs:element name="DataRecord" minOccurs="1" maxOccurs="unbounded
<xs:complexType>
<xs:sequence>
<xs:element name="Start" type="xs:integer" minOccurs="1" maxOccurs="1"/>
<xs:element name="Stop" type="xs:integer" minOccurs="1" maxOccurs="1"/>
<xs:element name="NumberOf" type="xs:integer" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="ResourceName" use="required
<xs:simpleType>
<xs:restriction base="xs:string
<xsattern value="([a-zA-Z]([a-zA-Z0-9]|_|-)*){1}(.([a-zA-Z]([a-zA-Z0-9]|_|-)*){1})*"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="HistogramType" type="xs:NMTOKENS" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>[/code]
Heres a snippet of the XML document that I cant get to fail validation in MSXML - note that there is a missing required attribute on the <Histogram> element, and the first <DataRecord> element is actually misnamed "xDataRecord":
<pre class="prettyprint <?xml version="1.0"?>
<HistogramList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:histogramList PSTTLTD2.xsd"
xmlns="urn:histogramList
<Histogram HistogramType="Net Usage
<xDataRecord><Start>7632000</Start><Stop>7660800</Stop><NumberOf>0</NumberOf></xDataRecord>
<DataRecord><Start>7660800</Start><Stop>7689600</Stop><NumberOf>0</NumberOf></DataRecord>
<DataRecord><Start>7689600</Start><Stop>7718400</Stop><NumberOf>0</NumberOf></DataRecord>
<DataRecord><Start>7718400</Start><Stop>7747200</Stop><NumberOf>-6</NumberOf></DataRecord> [/code]
The XSD file is co-located with both the spreadsheet containing the VBA code, and the XML data document, so, as near as I can tell, the filename without a path should be valid (in any event, nothing complains that it cant locate the XSD, and the same simple
location format reference also works in Eclipse when the XSD is in the same workspace). However, note that I also tried a file reference in the following (example) format for the schema location:
<p style="margin:0in; font-family:Calibri; font-size:11pt urn:MyData file:///Cocuments
file://C://Documents and Settings//All Users//Application Data//My Application//MyData.xsd
<p style="margin:0in; font-family:Calibri; font-size:11pt
<p style="margin:0in; font-family:Calibri; font-size:11pt The longer format didnt seem to make any difference.)
Thanks for any help!<br/>
<br/>
View the full article
VBA in Excel 2007, MSXML 6 - Im trying to test that validation against an XSD I developed will fail if there are problems in documents associated with the schema. Ive been able to trigger validation errors against this schema using the Eclipse
(Helios) XML tools, but I cant seem to cause MSXML 6 validation to fail unless its something like basic well-formedness of the data document. The schema document itself loads and validates without problems in MSXML 6. Ive tried 1) setting parseOnLoad
true for the data document, 2)doing a manual validate() on the data document, and also adding the schema to a schemaCache and validating against that. None of them will trip, again, unless its a basic well-formedness problem.
Its been a while since Ive had to do this, and Im guessing Im somehow not getting the namespaces wired correctly. Would appreciate it if someone could let me know if the cause of the problem jumps out at them.
Heres the test code:
<pre class="prettyprint lang-vb Sub checkSchema()
Dim vMyErr As Variant
Dim oXSDDoc As DOMDocument60
Set oXSDDoc = New DOMDocument60
oXSDDoc.async = False
Load XML Schema from a file
oXSDDoc.validateOnParse = True
oXSDDoc.Load (ThisWorkbook.Path & "PSTTLTD2.xsd")
If oXSDDoc.parseError.ErrorCode <> 0 Then
Set vMyErr = oXSDDoc.parseError
MsgBox "XML parsing error " + vMyErr.reason
End If
oXSDDoc.Validate
If oXSDDoc.parseError.ErrorCode <> 0 Then
Set vMyErr = oXSDDoc.parseError
MsgBox "XML parsing error " + vMyErr.reason
End If
Load XML document from a file
Dim oSourceDoc As DOMDocument60
Set oSourceDoc = New DOMDocument60
oSourceDoc.async = False
oSourceDoc.validateOnParse = True
oSourceDoc.Load (ThisWorkbook.Path & "XMLFinalSchemaTestDoc.xml")
If oSourceDoc.parseError.ErrorCode <> 0 Then
Set vMyErr = oSourceDoc.parseError
MsgBox "XML parsing error " + vMyErr.reason
End If
oSourceDoc.Validate
If oSourceDoc.parseError.ErrorCode <> 0 Then
Set vMyErr = oSourceDoc.parseError
MsgBox "XML parsing error " + vMyErr.reason
End If
Validate with Schema Cache
Reload schema into cache.
Set oXSDDoc = New DOMDocument60
oXSDDoc.async = False
oXSDDoc.Load (ThisWorkbook.Path & "PSTTLTD2.xsd")
Set oSchemaCache = New XMLSchemaCache60
oSchemaCache.Add "urn:histogramList", oXSDDoc
Set oSourceDoc.Schemas = oSchemaCache
oSourceDoc.Validate
If oSourceDoc.parseError.ErrorCode <> 0 Then
Set vMyErr = oSourceDoc.parseError
MsgBox "XML parsing error " + vMyErr.reason
End If
End Sub[/code]
<br/>
Heres the schema:
<pre class="prettyprint <?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="urn:histogramList"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:hL="urn:histogramList"
elementFormDefault="qualified
<xs:element name="HistogramList
<xs:complexType>
<xs:sequence>
<xs:element name="Histogram" minOccurs="1" maxOccurs="unbounded
<xs:complexType>
<xs:sequence>
<xs:element name="DataRecord" minOccurs="1" maxOccurs="unbounded
<xs:complexType>
<xs:sequence>
<xs:element name="Start" type="xs:integer" minOccurs="1" maxOccurs="1"/>
<xs:element name="Stop" type="xs:integer" minOccurs="1" maxOccurs="1"/>
<xs:element name="NumberOf" type="xs:integer" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="ResourceName" use="required
<xs:simpleType>
<xs:restriction base="xs:string
<xsattern value="([a-zA-Z]([a-zA-Z0-9]|_|-)*){1}(.([a-zA-Z]([a-zA-Z0-9]|_|-)*){1})*"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="HistogramType" type="xs:NMTOKENS" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>[/code]
Heres a snippet of the XML document that I cant get to fail validation in MSXML - note that there is a missing required attribute on the <Histogram> element, and the first <DataRecord> element is actually misnamed "xDataRecord":
<pre class="prettyprint <?xml version="1.0"?>
<HistogramList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:histogramList PSTTLTD2.xsd"
xmlns="urn:histogramList
<Histogram HistogramType="Net Usage
<xDataRecord><Start>7632000</Start><Stop>7660800</Stop><NumberOf>0</NumberOf></xDataRecord>
<DataRecord><Start>7660800</Start><Stop>7689600</Stop><NumberOf>0</NumberOf></DataRecord>
<DataRecord><Start>7689600</Start><Stop>7718400</Stop><NumberOf>0</NumberOf></DataRecord>
<DataRecord><Start>7718400</Start><Stop>7747200</Stop><NumberOf>-6</NumberOf></DataRecord> [/code]
The XSD file is co-located with both the spreadsheet containing the VBA code, and the XML data document, so, as near as I can tell, the filename without a path should be valid (in any event, nothing complains that it cant locate the XSD, and the same simple
location format reference also works in Eclipse when the XSD is in the same workspace). However, note that I also tried a file reference in the following (example) format for the schema location:
<p style="margin:0in; font-family:Calibri; font-size:11pt urn:MyData file:///Cocuments
file://C://Documents and Settings//All Users//Application Data//My Application//MyData.xsd
<p style="margin:0in; font-family:Calibri; font-size:11pt
<p style="margin:0in; font-family:Calibri; font-size:11pt The longer format didnt seem to make any difference.)
Thanks for any help!<br/>
<br/>
View the full article