XSD错误:不期望此元素

时间:2016-05-25 16:37:26

标签: xml xsd xsd-validation xml-validation

我正在编写一个XSD来验证XML,但是在我验证时出现了这个错误:

输出 - 错误

使用XML Schema验证当前文件:

  

错误:元素'{http://www.w3.org/2001/XMLSchema-instance}加斯托':   不期望这个元素。预计是(加斯托)

......我不理解错误

以下是我的XML示例:

 <?xml version="1.0" encoding="UTF-8"?>
 <Armazem>  
    <Lista_Gastos xmlns:artGasto="http://www.w3.org/2001/XMLSchema-instance"     
        artGasto:noNamespaceSchemaLocation="TraXSD.xsd">
        <artGasto:Gasto id="50">
           <artGasto:nome>Robalo</artGasto:nome>
           <artGasto:quantidade>1</artGasto:quantidade>
       </artGasto:Gasto> 
    </Lista_Gastos>
 </Armazem>

以下是我的XSD示例:

 <?xml version="1.0" encoding="utf-8"?>
 <xsd:schema elementFormDefault="qualified" 
       attributeFormDefault="unqualified"
       xmlns:xsd="http://www.w3.org/2001/XMLSchema"
       xmlns:artGasto="http://www.w3.org/2001/XMLSchema-instance">
   <xsd:element name="Armazem">
        <xsd:complexType>
        <xsd:sequence>
               <xsd:element name="Lista_Gastos" 
                 type="TListGastos" maxOccurs="unbounded"/>
        </xsd:sequence>
      </xsd:complexType>
  </xsd:element>   

<xsd:complexType name="TListGastos">
    <xsd:sequence >
        <xsd:element name="Gasto" type="TGasto" 
          maxOccurs="unbounded"/>
    </xsd:sequence>
</xsd:complexType> 


 <xsd:complexType name="TGasto">
    <xsd:sequence >
        <xsd:element name="nome" type="xsd:string" />
        <xsd:element name="quantidade" type="xs:integer" />
    </xsd:sequence>
    <xsd:attribute name="id" type="xsd:string" use="required"/>
</xsd:complexType> 

1 个答案:

答案 0 :(得分:1)

观察:

  1. 元素quantidade的类型应为xsd:integer,而不是xs:integer xsd(仅因为它是artGasto被定义为 在这种情况下,名称空间前缀)。
  2. http://www.w3.org/2001/XMLSchema-instance使用xsi命名空间前缀充其量是不可能的,可能是对命名空间误解的一种表现。在这里使用http://www.w3.org/2001/XMLSchema-instance
  3. 如果您想为某些元素使用命名空间,则不会使用特殊的<?xml version="1.0" encoding="UTF-8"?> <Armazem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="TraXSD.xsd"> <Lista_Gastos> <Gasto id="50"> <nome>Robalo</nome> <quantidade>1</quantidade> </Gasto> </Lista_Gastos> </Armazem> 。由于您的命名空间意图不明确,我现在已经删除了它们。
  4. 进行上述更改后,以下XML对以下XSD有效:

    XML

    <?xml version="1.0" encoding="utf-8"?>
    <xsd:schema elementFormDefault="qualified" attributeFormDefault="unqualified"
                xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <xsd:element name="Armazem">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element name="Lista_Gastos" type="TListGastos" maxOccurs="unbounded"/>
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>   
    
      <xsd:complexType name="TListGastos">
        <xsd:sequence >
          <xsd:element name="Gasto" type="TGasto" maxOccurs="unbounded"/>
        </xsd:sequence>
      </xsd:complexType> 
    
      <xsd:complexType name="TGasto">
        <xsd:sequence >
          <xsd:element name="nome" type="xsd:string" />
          <xsd:element name="quantidade" type="xsd:integer" />
        </xsd:sequence>
        <xsd:attribute name="id" type="xsd:string" use="required"/>
      </xsd:complexType> 
    </xsd:schema>
    

    XSD

    Sub ChangeDataSource()
        Dim DataSource As String
        Dim Green As Long, Red As Long
        Green = -11489280
        Red = -16776961
    
    On Error GoTo ErrHandle
    
        DataSource = Range("DataSource").Value
        ActiveSheet.PivotTables("PivotTable1").ChangePivotCache ActiveWorkbook. _
            PivotCaches.Create(SourceType:=xlDatabase, SourceData:=DataSource, Version _
            :=xlPivotTableVersion15)
    
        Range("DataSource").Font.Color = Green
        Range("CurrentDataSource").Value = DataSource
        Calculate
    
        GoTo Success
    
    ErrHandle:
        MsgBox ("DataSource is not valid.")
        Range("DataSource").Font.Color = Red
    
    Success:
    
    End Sub