Search code examples
c#xmlxsdxsd-validationxml-validation

Element http://www.w3.org/2001/XMLSchema:complexType is invalid in this context


I'm trying to validate this XML file

<session>
    <mic id="1" posname="T1" x="0.0" y="0.0" z="0.0" />
</session>

using this XSD file

<?xml version="1.0" encoding="utf-8"?>
<xs:schema 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    attributeFormDefault="unqualified" 
    elementFormDefault="qualified">

    <xs:element name="session">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="mic" type="micType" minOccurs="1" maxOccurs="4">
                </xs:element>

                <xs:complexType name="micType">
                    <xs:simpleContent>
                        <xs:extension base="xs:string">
                            <xs:attribute type="xs:int" name="id"/>
                            <xs:attribute type="xs:string" name="posname"/>
                            <xs:attribute type="xs:float" name="x"/>
                            <xs:attribute type="xs:float" name="y"/>
                            <xs:attribute type="xs:float" name="z"/>
                        </xs:extension>
                    </xs:simpleContent>
                </xs:complexType> 
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

but I get this error message:

XmlSchema error: Element http://www.w3.org/2001/XMLSchema:complexType is invalid in this context.

If I just have the attribute definition for the mic element the program runs fine. I don't know what I'm doing wrong. I'm trying to have the XSD validate the data types for the mic element. Can anybody please tell me what I'm doing wrong?


Solution

  • What you have is a non-viable hybrid of a local and global declaration of a complex type. Either define a micType globally (as Michael Kay mentioned) or locally:

    Global complexType declaration

    <?xml version="1.0" encoding="utf-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
               attributeFormDefault="unqualified" 
               elementFormDefault="qualified">
      <xs:element name="session">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="mic" type="micType" minOccurs="1" maxOccurs="4"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
      <xs:complexType name="micType">
        <xs:simpleContent>
          <xs:extension base="xs:string">
            <xs:attribute type="xs:int" name="id"/>
            <xs:attribute type="xs:string" name="posname"/>
            <xs:attribute type="xs:float" name="x"/>
            <xs:attribute type="xs:float" name="y"/>
            <xs:attribute type="xs:float" name="z"/>
          </xs:extension>
        </xs:simpleContent>
      </xs:complexType>
    </xs:schema>
    

    Local complexType declaration

    <?xml version="1.0" encoding="utf-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
               attributeFormDefault="unqualified" 
               elementFormDefault="qualified">
      <xs:element name="session">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="mic" minOccurs="1" maxOccurs="4">
              <xs:complexType>
                <xs:simpleContent>
                  <xs:extension base="xs:string">
                    <xs:attribute type="xs:int" name="id"/>
                    <xs:attribute type="xs:string" name="posname"/>
                    <xs:attribute type="xs:float" name="x"/>
                    <xs:attribute type="xs:float" name="y"/>
                    <xs:attribute type="xs:float" name="z"/>
                  </xs:extension>
                </xs:simpleContent>
              </xs:complexType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:schema>
    

    Either way is ok and will successfully validate your XML.