本文介绍了XSD 错误:不需要此元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写 XSD 来验证 XML,但是当我验证时出现此错误:

I was writing an XSD to validate a XML, but when I was validating this error appeared:

输出 - 错误

使用 XML 模式验证当前文件:

Validation of current file using XML Schema:

错误:元素 '{http://www.w3.org/2001/XMLSchema-instance}加斯托':不需要此元素.预期是(加斯托)

...我不明白错误

这是我的 XML 示例:

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>

这是我的 XSD 示例:

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. 元素quantidade的类型应该是xsd:integer,而不是xs:integer(仅仅因为 xsd 被定义为在这种情况下,命名空间前缀).
  2. http://www.w3.org/2001/XMLSchema-instance 使用 artGasto 命名空间前缀充其量是非常规的,并且可能是对命名空间.在这里使用 xsi.
  3. 如果您想为某些元素使用命名空间,则不会使用特殊的 http://www.w3.org/2001/XMLSchema-instance.由于您的命名空间意图非常不清楚,我暂时为您删除了它们.
  1. The type of element quantidade should be xsd:integer, notxs:integer (merely because it is xsd that is defined as thenamespace 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.

进行上述更改后,以下 XML 对以下 XSD 有效:

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>

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 错误:不需要此元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 08:45