XSD Error: This element is not expected
XSD Error: This element is not expected
我正在写一个 XSD 来验证一个 XML,但是当我验证这个错误时出现了:
输出 - 错误
使用XML架构验证当前文件:
ERROR: Element '{http://www.w3.org/2001/XMLSchema-instance}Gasto':
This element is not expected. Expected is ( Gasto )
...我不理解错误
这是我的示例 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>
这是我的示例 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>
观察:
- 元素
quantidade
的类型应该是xsd:integer
,不是
xs:integer
(仅仅因为 xsd
被定义为
在本例中为名称空间前缀)。
- 为
http://www.w3.org/2001/XMLSchema-instance
使用 artGasto
命名空间前缀充其量是非常规的,并且可能是对命名空间的误解的标志。这里使用xsi
。
- 如果您想为某些元素使用命名空间,则不会使用特殊的
http://www.w3.org/2001/XMLSchema-instance
。由于您的命名空间意图如此不清楚,我暂时为您删除了它们。
进行上述更改后,以下 XML 对以下 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>
我正在写一个 XSD 来验证一个 XML,但是当我验证这个错误时出现了:
输出 - 错误
使用XML架构验证当前文件:
ERROR: Element '{http://www.w3.org/2001/XMLSchema-instance}Gasto': This element is not expected. Expected is ( Gasto )
...我不理解错误
这是我的示例 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>
这是我的示例 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>
观察:
- 元素
quantidade
的类型应该是xsd:integer
,不是xs:integer
(仅仅因为xsd
被定义为 在本例中为名称空间前缀)。 - 为
http://www.w3.org/2001/XMLSchema-instance
使用artGasto
命名空间前缀充其量是非常规的,并且可能是对命名空间的误解的标志。这里使用xsi
。 - 如果您想为某些元素使用命名空间,则不会使用特殊的
http://www.w3.org/2001/XMLSchema-instance
。由于您的命名空间意图如此不清楚,我暂时为您删除了它们。
进行上述更改后,以下 XML 对以下 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>