xml - XSD 错误 : This element is not expected

标签 xml xsd xsd-validation xml-validation

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

输出 - 错误

使用 XML 模式验证当前文件:

ERROR: Element '{http://www.w3.org/2001/XMLSchema-instance}Gasto': This element is not expected. Expected is ( Gasto )



......我不明白这个错误

这是我的 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> 

最佳答案

观察:

  • 元素类型 quantidade应该是 xsd:integer , 不是xs:integer (仅仅因为 xsd 被定义为
    在这种情况下命名空间前缀)。
  • 使用 artGasto http://www.w3.org/2001/XMLSchema-instance 的命名空间前缀充其量是非常规的,可能是对命名空间的误解的迹象。使用 xsi这里。
  • 如果你想为你的一些元素使用命名空间,你不会使用特殊的 http://www.w3.org/2001/XMLSchema-instance .由于您的命名空间意图非常不清楚,我暂时为您删除了它们。

  • 进行上述更改后,以下 XML 对以下 XSD 有效:

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

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

    关于xml - XSD 错误 : This element is not expected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37443066/

    相关文章:

    c# - 使用Linq过滤掉特定元素内容

    java - 从 org.eclipse.xsd.XSDSchema 对象获取 XSD 源?

    android - 在Android上使用XStream时如何注释order字段?

    java - 如何将 catalog.xml 实体与数据库匹配?

    web-services - 为什么 xsd 模式无法验证带有限制 maxLength 的字符串长度

    xml - “||” (OR) 在XML中怎么写?

    c# - 如何使用 Monodevelop 为 iPhone 动态创建 UI

    C#:从 XML 架构 (XSD) 文件自动生成 DDL 和 ORM 类

    validation - 使用Camel中的Apache Validator组件进行XML验证

    xml - 如何使用属性值作为 XML 多态类型选择的鉴别器?