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>
Observations:
quantidade
should be xsd:integer
, not
xs:integer
(merely because it is xsd
that is defined as the
namespace prefix in this case).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.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 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>
<?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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With