问题
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:
- The type of element
quantidade
should bexsd:integer
, notxs:integer
(merely because it isxsd
that is defined as the namespace prefix in this case). - Using
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. - 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