【问题标题】:Why am I getting an error "the parameter is incorrect" when I try and validate my xml?为什么我在尝试验证我的 xml 时收到错误“参数不正确”?
【发布时间】:2017-11-30 22:31:47
【问题描述】:

我正在尝试根据架构验证我的 xml,但是当我运行代码时,我收到了一个错误。 xml (sample.xml) 是:

<?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>

架构(sample.xsd)是:

<?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>

我使用 xml 文件在线生成了架构,因此我有理由相信那里不应该有任何错误。 我使用的vba代码是:

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

问题出现在这里

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

出现错误The parameter is incorrect,但我看不到是哪个参数导致了问题。查看 XMLSchemaCache60 的 add 方法的文档,我看不到任何提及此错误。

有人知道这是什么吗?

谢谢

编辑: 好吧,在过去几天仔细考虑之后,我仍然不确定我是否完全理解来自Michael Kay 的 cmets,除了建议 xsd 几乎没用的评论,因为我和 xmlgrid.net 一样没用,我尝试了另一个;这次https://devutilsonline.com/xsd-xml/generate-xsd-from-xml 这就是我得到的:

<?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>

看起来好像解决了导入架构问题??????但是我现在收到一个新错误:

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

我不明白为什么这是一个未声明的属性 - 对我来说,这看起来就像同一个文档中的其余部分,尽管名称不同!

【问题讨论】:

  • 当我尝试运行代码时,我收到一条错误消息,提示“将 'dt:dt' 解析为 NCName 数据类型时出错。” (但是,因为我对 xml 几乎一无所知,所以我不确定这与您的问题有何关系。)
  • 我强烈建议重命名 LoadXML(),因为这是 MSXML 中的一种方法。
  • Dir是一个VBA函数。

标签: xml vba domdocument


【解决方案1】:

架构确实无效(这意味着您选择的在线工具有问题 - 实际上是垃圾)。

首先,

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

非常不正确:name 属性必须是简单的 NCName(无冒号名称)。如果属性与包含元素位于不同的命名空间中,则模式需要包含该命名空间的单独模式文档的 xs:import;该属性需要在导入的架构文档中声明,并在主架构文档中使用ref 属性进行引用:

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

其次,元素声明不能同时具有type 属性和complexType 子元素。描述具有属性并具有文本内容的元素的正确方法是使用“具有简单内容的复杂类型”,如下所述:

XML Schema: Element with attributes containing only text?

我不得不说你收到的错误信息也很糟糕。 Saxon 为您提供以下形式的消息列表:

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

后跟以下形式的消息列表:

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

【讨论】:

  • 在写完这篇文章后,我注意到架构归属于 xmlgrid.net。 xmlgrid 生成的其他模式,例如在stackoverflow.com/questions/17247978/… 似乎有同样的问题。由于在 xmlgrid 网站上无法提交错误报告,因此如果错误没有得到修复,也许不足为奇。
【解决方案2】:

如果您正确错误地处理了捕获运行时异常的方法,尤其是使用 MSXML 等 API,您将收到更相关的错误消息,显示架构问题。

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

对话框消息(我将架构文件命名为 CatalogExcel.xsd)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-09
    • 1970-01-01
    • 2013-05-25
    • 2022-01-08
    • 2020-10-11
    相关资源
    最近更新 更多