XML 发现以元素开头的内容无效。应为“{}”之一

XML Invalid content was found starting with element. One of '{}' is expected

我正在根据 XML 文档验证 XSD,但出现此错误:

cvc-complex-type.2.4.a: Invalid content was found starting with element 'artSpent:name'. One of '{"http://www.dei.isep.ipp.pt/lprog":name}' is expected. [467]

这是一个XSD样本

<?xml version="1.0" encoding="utf-8"?>
<xsd:schema version="1.0"
            xmlns:lprog="http://www.dei.isep.ipp.pt/lprog"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://www.dei.isep.ipp.pt/lprog" 
            elementFormDefault="qualified">

    <xsd:element name="warehouse">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="ListSpent" 
                             type="lprog:TListSpent" 
                             maxOccurs="unbounded"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>       

       <xsd:complexType name="ListSpent">
        <xsd:sequence >
            <xsd:element name="Spent" 
                         type="lprog:TSpent" 
                         maxOccurs="unbounded"/>
        </xsd:sequence>
    </xsd:complexType>  

       <xsd:complexType name="TSpent">
        <xsd:sequence >
            <xsd:element name="name" type="xsd:string" />
            <xsd:element name="stock" type="lprog:TQtdArtigo" />
        </xsd:sequence>
    </xsd:complexType>     
</xsd:schema>

还有我的XML样本

 <?xml version="1.0" encoding="UTF-8"?>
   <warehouse xmlns="http://www.dei.isep.ipp.pt/lprog"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://www.dei.isep.ipp.pt/lprog 
                             TraXSD.xsd">

     <ListSpent xmlns:artSpent="http://www.w3.org/2001/XMLSchema-instance" 
                artSpent:noNamespaceSchemaLocation="TraXSD.xsd">
        <Spent>
            <artSpent:name>Meat</artSpent:name>
            <artSpent:stock>2</artSpent:stock>
        </Spent>    
    </ListSpent>
</warehouse>   

您的 XSD 和 XML 文件都存在一些问题,但导致您在问题中引用的直接错误的特定问题是由于您没有建立 name 元素正确地位于管理 XSD 的 targetNamespace="http://www.dei.isep.ipp.pt/lprog" 中。在下面的工作示例中了解我是如何做到的...

以下更正的 XSD 将成功验证以下更正的 XML 文件。

XSD

<?xml version="1.0" encoding="utf-8"?>
<xsd:schema version="1.0"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:lprog="http://www.dei.isep.ipp.pt/lprog"
            targetNamespace="http://www.dei.isep.ipp.pt/lprog" 
            elementFormDefault="qualified">

  <xsd:element name="warehouse">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="ListSpent" type="lprog:ListSpent"
                     maxOccurs="unbounded"/>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>       

  <xsd:complexType name="ListSpent">
    <xsd:sequence >
      <xsd:element name="Spent" type="lprog:TSpent" maxOccurs="unbounded"/>
    </xsd:sequence>
  </xsd:complexType>  

  <xsd:complexType name="TSpent">
    <xsd:sequence >
      <xsd:element name="name" type="xsd:string" />
      <xsd:element name="stock" type="xsd:integer" />
    </xsd:sequence>
  </xsd:complexType>

</xsd:schema>

XML

使用默认命名空间声明:

<?xml version="1.0" encoding="UTF-8"?>
<warehouse xmlns="http://www.dei.isep.ipp.pt/lprog"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.dei.isep.ipp.pt/lprog TraXSD.xsd">
  <ListSpent>
    <Spent>
      <name>Meat</name>
      <stock>2</stock>
    </Spent>    
  </ListSpent>
</warehouse>

使用显式命名空间前缀:

<?xml version="1.0" encoding="UTF-8"?>
<lprog:warehouse xmlns:lprog="http://www.dei.isep.ipp.pt/lprog"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:schemaLocation="http://www.dei.isep.ipp.pt/lprog TraXSD.xsd">
  <lprog:ListSpent>
    <lprog:Spent>
      <lprog:name>Meat</lprog:name>
      <lprog:stock>2</lprog:stock>
    </lprog:Spent>    
  </lprog:ListSpent>
</lprog:warehouse>