Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSD Error: This element is not expected

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> 
like image 255
Joseph Avatar asked Nov 09 '22 14:11

Joseph


1 Answers

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>
like image 55
kjhughes Avatar answered Dec 18 '22 08:12

kjhughes