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>
Best Answer
Observations:
quantidade
should bexsd:integer
, notxs:integer
(merely because it isxsd
that is defined as the namespace prefix in this case).artGasto
namespace prefix forhttp://www.w3.org/2001/XMLSchema-instance
is unconvetional at best, and probably a sign of a misunderstanding of namespaces. Usexsi
here.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
XSD