Mark
Mark

Reputation: 77

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

I was validating an XSD against a XML document, and I get this error:

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]

Here is a XSD sample

<?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>

And my XML sample

 <?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>   

Upvotes: 2

Views: 15684

Answers (1)

kjhughes
kjhughes

Reputation: 111521

There are several issues with both your XSD and your XML file, but the particular one causing the immediate error you cite in your question is due to you not establishing the name element properly in the targetNamespace="http://www.dei.isep.ipp.pt/lprog" of the governing XSD. See how I do it in the working examples below...

The following corrected XSD will successfully validate the following corrected XML file.

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

Using default namespace declaration:

<?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>

Using explicit namespace prefixes:

<?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>   

Upvotes: 3

Related Questions