1

I am trying to validate my xml against a schema but when I run the code, I am getting an error. The xml (sample.xml) is:

<?xml version="1.0" encoding="utf-8"?>
<Catalog xmlns:dt="urn:schemas-microsoft-com:datatypes">
  <Rec>
    <ITEMS dt:dt="string"/>
    <QTY dt:dt="string">1</QTY>
    <CATALOG dt:dt="string">1492</CATALOG>
    <MFG dt:dt="string">BA</MFG>
    <ASSYCODE dt:dt="string"/>
    <DESC dt:dt="string">MCB 1 POLE 2A TYPE C</DESC>
    <QUERY2 dt:dt="string">1MCB</QUERY2>
    <QUERY3 dt:dt="string">2A</QUERY3>
    <MISC1 dt:dt="string"/>
    <MISC2 dt:dt="string"/>
    <USER1 dt:dt="string">EAMCB122</USER1>
    <USER2 dt:dt="string"/>
    <USER3 dt:dt="string"/>
    <TABNAM dt:dt="string">CB</TABNAM>
    <TAGS dt:dt="string">Q2-4D</TAGS>
    <DESC1 dt:dt="string"/>
    <DESC2 dt:dt="string"/>
    <DESC3 dt:dt="string"/>
    <INST dt:dt="string"/>
    <LOC dt:dt="string"/>
    <UM dt:dt="string"/>
    <HDL dt:dt="string">h=6706</HDL>
    <DWGIX dt:dt="string">7</DWGIX>
    <REF dt:dt="string">-4D</REF>
    <SH dt:dt="string">2</SH>
    <SOURCE dt:dt="string">ELECTRICAL</SOURCE>
  </Rec>
</Catalog>

and the schema (sample.xsd) is:

<?xml version="1.0" encoding="UTF-8"?>
   <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
         <!-- XML Schema Generated from XML Document on Thu Nov 30 2017 16:39:38 GMT+0000 (GMT Standard Time) -->
         <!-- with XmlGrid.net Free Online Service http://xmlgrid.net -->
         <xs:element name="Catalog">
               <xs:complexType>
                     <xs:sequence>
                           <xs:element name="Rec" maxOccurs="unbounded">
                                 <xs:complexType>
                                       <xs:sequence>
                                             <xs:element name="ITEMS">
                                                   <xs:complexType>
                                                         <xs:attribute name="dt:dt" type="xs:string"></xs:attribute>
                                                   </xs:complexType>
                                             </xs:element>
                                             <xs:element name="QTY" type="xs:int">
                                                   <xs:complexType>
                                                         <xs:attribute name="dt:dt" type="xs:string"></xs:attribute>
                                                   </xs:complexType>
                                             </xs:element>
                                             <xs:element name="CATALOG" type="xs:int">
                                                   <xs:complexType>
                                                         <xs:attribute name="dt:dt" type="xs:string"></xs:attribute>
                                                   </xs:complexType>
                                             </xs:element>
                                             <xs:element name="MFG" type="xs:string">
                                                   <xs:complexType>
                                                         <xs:attribute name="dt:dt" type="xs:string"></xs:attribute>
                                                   </xs:complexType>
                                             </xs:element>
                                             <xs:element name="ASSYCODE">
                                                   <xs:complexType>
                                                         <xs:attribute name="dt:dt" type="xs:string"></xs:attribute>
                                                   </xs:complexType>
                                             </xs:element>
                                             <xs:element name="DESC" type="xs:string">
                                                   <xs:complexType>
                                                         <xs:attribute name="dt:dt" type="xs:string"></xs:attribute>
                                                   </xs:complexType>
                                             </xs:element>
                                             <xs:element name="QUERY2" type="xs:string">
                                                   <xs:complexType>
                                                         <xs:attribute name="dt:dt" type="xs:string"></xs:attribute>
                                                   </xs:complexType>
                                             </xs:element>
                                             <xs:element name="QUERY3" type="xs:string">
                                                   <xs:complexType>
                                                         <xs:attribute name="dt:dt" type="xs:string"></xs:attribute>
                                                   </xs:complexType>
                                             </xs:element>
                                             <xs:element name="MISC1">
                                                   <xs:complexType>
                                                         <xs:attribute name="dt:dt" type="xs:string"></xs:attribute>
                                                   </xs:complexType>
                                             </xs:element>
                                             <xs:element name="MISC2">
                                                   <xs:complexType>
                                                         <xs:attribute name="dt:dt" type="xs:string"></xs:attribute>
                                                   </xs:complexType>
                                             </xs:element>
                                             <xs:element name="USER1" type="xs:string">
                                                   <xs:complexType>
                                                         <xs:attribute name="dt:dt" type="xs:string"></xs:attribute>
                                                   </xs:complexType>
                                             </xs:element>
                                             <xs:element name="USER2">
                                                   <xs:complexType>
                                                         <xs:attribute name="dt:dt" type="xs:string"></xs:attribute>
                                                   </xs:complexType>
                                             </xs:element>
                                             <xs:element name="USER3">
                                                   <xs:complexType>
                                                         <xs:attribute name="dt:dt" type="xs:string"></xs:attribute>
                                                   </xs:complexType>
                                             </xs:element>
                                             <xs:element name="TABNAM" type="xs:string">
                                                   <xs:complexType>
                                                         <xs:attribute name="dt:dt" type="xs:string"></xs:attribute>
                                                   </xs:complexType>
                                             </xs:element>
                                             <xs:element name="TAGS" type="xs:string">
                                                   <xs:complexType>
                                                         <xs:attribute name="dt:dt" type="xs:string"></xs:attribute>
                                                   </xs:complexType>
                                             </xs:element>
                                             <xs:element name="DESC1">
                                                   <xs:complexType>
                                                         <xs:attribute name="dt:dt" type="xs:string"></xs:attribute>
                                                   </xs:complexType>
                                             </xs:element>
                                             <xs:element name="DESC2">
                                                   <xs:complexType>
                                                         <xs:attribute name="dt:dt" type="xs:string"></xs:attribute>
                                                   </xs:complexType>
                                             </xs:element>
                                             <xs:element name="DESC3">
                                                   <xs:complexType>
                                                         <xs:attribute name="dt:dt" type="xs:string"></xs:attribute>
                                                   </xs:complexType>
                                             </xs:element>
                                             <xs:element name="INST">
                                                   <xs:complexType>
                                                         <xs:attribute name="dt:dt" type="xs:string"></xs:attribute>
                                                   </xs:complexType>
                                             </xs:element>
                                             <xs:element name="LOC">
                                                   <xs:complexType>
                                                         <xs:attribute name="dt:dt" type="xs:string"></xs:attribute>
                                                   </xs:complexType>
                                             </xs:element>
                                             <xs:element name="UM">
                                                   <xs:complexType>
                                                         <xs:attribute name="dt:dt" type="xs:string"></xs:attribute>
                                                   </xs:complexType>
                                             </xs:element>
                                             <xs:element name="HDL" type="xs:string">
                                                   <xs:complexType>
                                                         <xs:attribute name="dt:dt" type="xs:string"></xs:attribute>
                                                   </xs:complexType>
                                             </xs:element>
                                             <xs:element name="DWGIX" type="xs:int">
                                                   <xs:complexType>
                                                         <xs:attribute name="dt:dt" type="xs:string"></xs:attribute>
                                                   </xs:complexType>
                                             </xs:element>
                                             <xs:element name="REF" type="xs:string">
                                                   <xs:complexType>
                                                         <xs:attribute name="dt:dt" type="xs:string"></xs:attribute>
                                                   </xs:complexType>
                                             </xs:element>
                                             <xs:element name="SH" type="xs:int">
                                                   <xs:complexType>
                                                         <xs:attribute name="dt:dt" type="xs:string"></xs:attribute>
                                                   </xs:complexType>
                                             </xs:element>
                                             <xs:element name="SOURCE" type="xs:string">
                                                   <xs:complexType>
                                                         <xs:attribute name="dt:dt" type="xs:string"></xs:attribute>
                                                   </xs:complexType>
                                             </xs:element>
                                       </xs:sequence>
                                 </xs:complexType>
                           </xs:element>
                     </xs:sequence>
                     <xs:attribute name="xmlns:dt" type="xs:string"></xs:attribute>
               </xs:complexType>
         </xs:element>
   </xs:schema>

I generated the schema online using the xml file so am reasonably confident there shouldn't be any errors there. The vba code I am using is:

Sub test()

    Dim xDoc As New MSXML2.DOMDocument60
    Dim xLists As IXMLDOMElement


    Const dir = "C:\"
    Dim xSchema As New MSXML2.XMLSchemaCache60
    Dim xSchemaErr As IXMLDOMParseError

    Call xSchema.Add("urn:schemas-microsoft-com:datatypes", LoadXML(dir & "sample.xsd"))

    Set xDoc = LoadXML(dir & "sample.xml")
    Set xDoc.Schemas = xSchema

    Set xSchemaErr = xDoc.Validate

    Set xDoc = Nothing

End Sub

Private Function LoadXML(strFilename As String) As MSXML2.DOMDocument60

    Dim xDoc As New MSXML2.DOMDocument60
    Dim bLoadSucceeded As Boolean
    Dim fso As New FileSystemObject

    With xDoc
        .async = False
        .validateOnParse = False
        .resolveExternals = False
    End With

    If (fso.FileExists(strFilename)) Then
        bLoadSucceeded = xDoc.Load(strFilename)
        If (bLoadSucceeded) Then
            Set LoadXML = xDoc
        Else
            Call MsgBox("Load XML document failed", vbCritical, "Warning")
            Set xDoc = Nothing
            Exit Function
        End If
    Else
        Call MsgBox("Unable to find specified XML file to load", vbCritical, "Warning")
        Set xDoc = Nothing
        Exit Function
    End If

    Set xDoc = Nothing

End Function

The problem occurs here

Call xSchema.Add("urn:schemas-microsoft-com:datatypes", LoadXML(dir & "sample.xsd")) 

with the error The parameter is incorrect but I cannot see which parameter is causing the problem. Looking at the documentation for the add method of XMLSchemaCache60, I cannot see any mention of this error.

Does anyone know what this could be?

Thanks

Edit: Well, after mulling this over for the last few days, I am still not sure I entirely understand comments from Michael Kay other than the comment which suggested the xsd was pretty much useless so, as I am just about as useless as xmlgrid.net, I tried another; this time https://devutilsonline.com/xsd-xml/generate-xsd-from-xml and this is what I got:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified">
  <xs:element name="Catalog" type="CatalogType"/>
  <xs:complexType name="ITEMSType">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute xmlns:urn="urn:schemas-microsoft-com:datatypes" ref="urn:dt"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="QTYType">
    <xs:simpleContent>
      <xs:extension base="xs:byte">
        <xs:attribute xmlns:urn="urn:schemas-microsoft-com:datatypes" ref="urn:dt"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="CATALOGType">
    <xs:simpleContent>
      <xs:extension base="xs:short">
        <xs:attribute xmlns:urn="urn:schemas-microsoft-com:datatypes" ref="urn:dt"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="MFGType">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute xmlns:urn="urn:schemas-microsoft-com:datatypes" ref="urn:dt"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="ASSYCODEType">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute xmlns:urn="urn:schemas-microsoft-com:datatypes" ref="urn:dt"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="DESCType">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute xmlns:urn="urn:schemas-microsoft-com:datatypes" ref="urn:dt"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="QUERY2Type">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute xmlns:urn="urn:schemas-microsoft-com:datatypes" ref="urn:dt"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="QUERY3Type">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute xmlns:urn="urn:schemas-microsoft-com:datatypes" ref="urn:dt"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="MISC1Type">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute xmlns:urn="urn:schemas-microsoft-com:datatypes" ref="urn:dt"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="MISC2Type">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute xmlns:urn="urn:schemas-microsoft-com:datatypes" ref="urn:dt"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="USER1Type">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute xmlns:urn="urn:schemas-microsoft-com:datatypes" ref="urn:dt"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="USER2Type">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute xmlns:urn="urn:schemas-microsoft-com:datatypes" ref="urn:dt"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="USER3Type">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute xmlns:urn="urn:schemas-microsoft-com:datatypes" ref="urn:dt"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="TABNAMType">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute xmlns:urn="urn:schemas-microsoft-com:datatypes" ref="urn:dt"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="TAGSType">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute xmlns:urn="urn:schemas-microsoft-com:datatypes" ref="urn:dt"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="DESC1Type">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute xmlns:urn="urn:schemas-microsoft-com:datatypes" ref="urn:dt"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="DESC2Type">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute xmlns:urn="urn:schemas-microsoft-com:datatypes" ref="urn:dt"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="DESC3Type">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute xmlns:urn="urn:schemas-microsoft-com:datatypes" ref="urn:dt"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="INSTType">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute xmlns:urn="urn:schemas-microsoft-com:datatypes" ref="urn:dt"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="LOCType">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute xmlns:urn="urn:schemas-microsoft-com:datatypes" ref="urn:dt"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="UMType">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute xmlns:urn="urn:schemas-microsoft-com:datatypes" ref="urn:dt"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="HDLType">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute xmlns:urn="urn:schemas-microsoft-com:datatypes" ref="urn:dt"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="DWGIXType">
    <xs:simpleContent>
      <xs:extension base="xs:byte">
        <xs:attribute xmlns:urn="urn:schemas-microsoft-com:datatypes" ref="urn:dt"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="REFType">
    <xs:simpleContent>
      <xs:extension base="xs:float">
        <xs:attribute xmlns:urn="urn:schemas-microsoft-com:datatypes" ref="urn:dt"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="SHType">
    <xs:simpleContent>
      <xs:extension base="xs:byte">
        <xs:attribute xmlns:urn="urn:schemas-microsoft-com:datatypes" ref="urn:dt"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="SOURCEType">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute xmlns:urn="urn:schemas-microsoft-com:datatypes" ref="urn:dt"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="RecType">
    <xs:sequence>
      <xs:element type="ITEMSType" name="ITEMS"/>
      <xs:element type="QTYType" name="QTY"/>
      <xs:element type="CATALOGType" name="CATALOG"/>
      <xs:element type="MFGType" name="MFG"/>
      <xs:element type="ASSYCODEType" name="ASSYCODE"/>
      <xs:element type="DESCType" name="DESC"/>
      <xs:element type="QUERY2Type" name="QUERY2"/>
      <xs:element type="QUERY3Type" name="QUERY3"/>
      <xs:element type="MISC1Type" name="MISC1"/>
      <xs:element type="MISC2Type" name="MISC2"/>
      <xs:element type="USER1Type" name="USER1"/>
      <xs:element type="USER2Type" name="USER2"/>
      <xs:element type="USER3Type" name="USER3"/>
      <xs:element type="TABNAMType" name="TABNAM"/>
      <xs:element type="TAGSType" name="TAGS"/>
      <xs:element type="DESC1Type" name="DESC1"/>
      <xs:element type="DESC2Type" name="DESC2"/>
      <xs:element type="DESC3Type" name="DESC3"/>
      <xs:element type="INSTType" name="INST"/>
      <xs:element type="LOCType" name="LOC"/>
      <xs:element type="UMType" name="UM"/>
      <xs:element type="HDLType" name="HDL"/>
      <xs:element type="DWGIXType" name="DWGIX"/>
      <xs:element type="REFType" name="REF"/>
      <xs:element type="SHType" name="SH"/>
      <xs:element type="SOURCEType" name="SOURCE"/>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="CatalogType">
    <xs:sequence>
      <xs:element type="RecType" name="Rec"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

It looks as if it has addressed the import schema issue????? however I now get a new error:

sample.xsd#/schema/complexType[17][@name = 'DESC2Type']/simpleContent[1]/extension[1]/attribute[1]
Undeclared XSD attribute : '{urn:schemas-microsoft-com:datatypes}dt'.

I cannot understand why this is an Undeclared attribute - to me this looks just like the rest in the same document albeit with a different name!!

cosmarchy
  • 686
  • 3
  • 9
  • 21
  • When I try to run the code, I get an error saying that there was an "Error parsing 'dt:dt' as NCName datatype." (But, because I know next to nothing about xml, I'm not sure how that relates to your issue.) – YowE3K Nov 30 '17 at 23:24
  • I highly advise renaming `LoadXML()` as that is a method in MSXML. – Parfait Nov 30 '17 at 23:39
  • And `Dir` is a VBA function. – YowE3K Dec 01 '17 at 00:00

2 Answers2

0

The schema is indeed invalid (which means the online tool you chose is buggy - in fact, it's crap).

Firstly,

<xs:attribute name="dt:dt" type="xs:string">

is quite incorrect: the name attribute must be a simple NCName (no-colon-name). Where the attribute is in a different namespace from the containing element, the schema needs to contain an xs:import of a separate schema document for that namespace; the attribute needs to be declared in the imported schema document, and referenced in the primary schema document using the ref attribute:

<xs:attribute ref="dt:dt"/> 

Secondly, an element declaration can't have both a type attribute and a complexType child element. The correct way to describe an element that has an attribute and also has text content is using a "complex type with simple content" as described here:

XML Schema: Element with attributes containing only text?

I have to say the error message you are getting is pretty lousy too. Saxon gives you a list of messages of the form:

Error at xs:attribute on line 13 column 81 of test.xsd:
  No namespace prefix is allowed in this name {dt:dt}

followed by a list of messages of the form:

Error at xs:element on line 16 column 66 of test.xsd:
  A <complexType> element must not appear as a child of an <xs:element> with a @type attribute
Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • After writing this I noticed that the schema was attributed to xmlgrid.net. Other schemas generated by xmlgrid, e.g. at https://stackoverflow.com/questions/17247978/how-to-validate-this-xml-input/17249033#17249033 seem to have the same problems. Since there's no way on the xmlgrid site of submitting bug reports, it's perhaps not surprising if bugs don't get fixed. – Michael Kay Dec 01 '17 at 09:32
0

Had you properly error handled your methods to catch runtime exceptions especially working with APIs like MSXML, you would receive a more pertinent error message showing issues with schema.

Sub test()
On Error GoTo ErrHandle
    ...


ExitHandle:
    Set xDoc = Nothing
    Exit Sub

ErrHandle:
    MsgBox Err.Number & " - " & Err.Description, vbCritical
    Resume ExitHandle
End Sub

' CHANGED FCT NAME TO AVOID NAME CLASH WITH MSXML's LOADXML()    
Private Function ProcXML(strFilename As String) As MSXML2.DOMDocument60
On Error GoTo ErrHandle
    ...

ExitHandle:
    Set xDoc = Nothing
    Exit Function

ErrHandle:
    MsgBox Err.Number & " - " & Err.Description, vbCritical
    Resume ExitHandle
End Function

Dialog Message (where I named the schema file CatalogExcel.xsd)

Error Message

Parfait
  • 104,375
  • 17
  • 94
  • 125