XSD Error: This element is not expected

情到浓时终转凉″ 提交于 2021-01-27 05:30:31

问题


I was writing an XSD to validate a XML, but when I was validating this error appeared:

Output - error

Validation of current file using XML Schema:

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

... and I'm not understanding the error


Here is a sample of my 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>

And here is a sample of my 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:


Observations:

  1. The type of element quantidade should be xsd:integer, not xs:integer (merely because it is xsd that is defined as the namespace prefix in this case).
  2. Using artGasto namespace prefix for http://www.w3.org/2001/XMLSchema-instance is unconvetional at best, and probably a sign of a misunderstanding of namespaces. Use xsi here.
  3. If you wanted to use namespaces for some of your elements, you wouldn't use the special http://www.w3.org/2001/XMLSchema-instance. Since your namespace intentions are so unclear, I've removed them for you for now.

After making the above changes, the following XML is valid against the following 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>


来源:https://stackoverflow.com/questions/37443066/xsd-error-this-element-is-not-expected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!