XML从element开始发现无效内容。预计会有一个'{}'

时间:2016-05-26 01:46:51

标签: xml xsd xsd-validation xml-validation

我正在针对XML文档验证XSD,我收到此错误:

  

cvc-complex-type.2.4.a:找到无效的内容   元素'artSpent:name'。之一   预计会'{"http://www.dei.isep.ipp.pt/lprog":name}'。 [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>   

1 个答案:

答案 0 :(得分:2)

您的XSD和XML文件存在多个问题,但导致您在问题中引发的即时错误的特定问题是由于您未在name中正确建立targetNamespace="http://www.dei.isep.ipp.pt/lprog"元素治理XSD。请参阅下面的工作示例中我是如何做到的......

以下更正的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>